A plugin for Webpack
See original GitHub issueI’d like to know when an official plugin is released for this. So, maybe provide an update in this issue when there is one?
Here’s a very rudimentary plugin for Webpack 4 based on your LintCommandRunner
:
import { Container, BindingScopeEnum, injectable } from "inversify";
import { Compiler, Plugin } from "webpack";
import {
MessageHandler,
Failure
} from "@fimbul/ymir";
import {
CachedFileSystem,
createCoreModule,
createDefaultModule,
FormatterLoader,
LintOptions,
Runner
} from "@fimbul/wotan";
export interface LintCommand extends LintOptions {
formatter: string | undefined;
}
// https://github.com/fimbullinter/wotan/blob/be4483d5885c88a9640869203e46aba58d49e8f7/packages/wotan/src/commands/lint.ts
@injectable()
class LintCommandRunner {
constructor(
private runner: Runner,
private formatterLoader: FormatterLoader,
private logger: MessageHandler,
private fs: CachedFileSystem,
) {
}
public run(options: LintCommand) {
const formatter = new (this.formatterLoader.loadFormatter(options.formatter === undefined ? 'stylish' : options.formatter))();
const result = this.runner.lintCollection(options);
let success = true;
if (formatter.prefix !== undefined)
this.logger.log(formatter.prefix);
for (const [file, summary] of result) {
if (summary.failures.some(isError))
success = false;
const formatted = formatter.format(file, summary);
if (formatted !== undefined)
this.logger.log(formatted);
if (options.fix && summary.fixes)
this.fs.writeFile(file, summary.content);
}
if (formatter.flush !== undefined) {
const formatted = formatter.flush();
if (formatted !== undefined)
this.logger.log(formatted);
}
return success;
}
}
function isError(failure: Failure) {
return failure.severity === 'error';
}
export default class WotanWebpackPlugin implements Plugin {
public constructor(private options: LintCommand) {
}
public afterCompile(): void {
const container = new Container({defaultScope: BindingScopeEnum.Request});
container.load(createCoreModule({}), createDefaultModule());
container.bind<LintCommandRunner>(LintCommandRunner)
.toSelf();
container.get(LintCommandRunner)
.run(this.options);
}
public apply(compiler: Compiler): void {
compiler.hooks
.done
.tap("WotanWebpackPlugin", this.afterCompile.bind(this));
}
}
Usage:
new WotanWebpackPlugin({
config: resolve(paths.tools, "wotan.yaml"),
project: resolve(paths.src, "tsconfig.json"),
files: [],
exclude: [],
fix: false,
extensions: undefined,
formatter: undefined
})
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Plugins - webpack
A webpack plugin is a JavaScript object that has an apply method. This apply method is called by the webpack compiler, giving access...
Read more >Writing a Plugin - webpack
Creating a Plugin · A named JavaScript function or a JavaScript class. · Defines apply method in its prototype. · Specifies an event...
Read more >Plugins - webpack
webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable...
Read more >Plugins - webpack
The plugins option is used to customize the webpack build process in a variety of ways. Webpack comes with a variety built-in plugins...
Read more >Plugin API - webpack
Plugins are a key piece of the webpack ecosystem and provide the community with a powerful way to tap into webpack's compilation process....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you for the feature request and this example code. I think a webpack plugin would be an awesome addition.
I don’t have any experience with writing webpack plugins. So here are some random thoughts after having a first look at it:
NodeFileSystem
does), there should be a way to use the already loaded files from the current webpack compilation.fimbullinter.yaml
file like the CLI to load plugin modules and use it for default valuestsconfig.json
,.wotanrc.yaml
,.fimbullinter.yaml
, …) so that a new linting run is triggered if the config changes? – I looked it up, you canI’ll look into exposing some utilities so that there’s no need to duplicate all the code.
They’re discussing
tslint
integration at https://github.com/facebook/create-react-app/issues/5641.From what I can tell at the moment, yes, the integration should be a
webpack
plugin. Currentlycreate-react-app
is aiming fortslint
support viafork-ts-checker-webpack-plugin
, which has some form oftslint
support.If the language server plugin could report warnings/errors during build, that would be ideal, but I’ve understood it’s not possible?
EDIT: To be clear, there is no support for adding arbitrary
webpack
plugins increate-react-app
withouteject
. I don’t know what would be the proper integration at the moment, but it’s likely to be related towebpack
.