20
loading...
This website collects cookies to deliver better user experience
path
module.const path = require("path");
const MyPlugin = require('my-plugin');
const AnotherPlugin = require('another-plugin');
module.exports = {
entry: './src',
output: {
path: path.resolve(__dirname, "dist"),
filename: "[name].ready.js"
},
module: {
rules: [
{
test: /\.fileextension$/,
use: ["third-loader", "second-loader", "first-loader-for-this-file-type"]
},
{
test: /\.js$/,
exclude: /node_modules/,
use: ["babel-loader"]
},
{
test: /\.scss$/,
use: ["style-loader", "css-loader", "sass-loader"]
}
]
},
plugins: [
new MyPlugin(),
new AnotherPlugin(),
]
};
apply
method. Webpack will use apply
to install the plugin when it creates the compiler instance. It will also pass down a reference to the compiler instance as an argument to the plugin. Our plugin will tap into a hook on the compiler instance, and pass that hook a callback that will then be executed at a certain lifecycle event.class SimplePlugin {
apply(compiler) {
compiler.hooks.compilation.tap("SimplePlugin", (compilation) => {
compilation.hooks.succeedModule.tap("SimplePlugin", (module) => {
// What we want to happen
});
});
}
}
module.exports = SimplePlugin;
tap
on that hook and pass it a callback to run our plugin.succeedModule
hook, which is executed each time a module is successfully built during compilation. We call the tap
method once more and again pass it a second callback function. Within this second callback, we are able to execute code at this particular moment in the webpack lifecycle—when a module is successfully compiled during webpack's compilation process. (Webpack will pass the completed module as an argument to our second callback.) compilation
and succeedModule
hooks that we’ve chosen to tap in the above example are just two of a long list of lifecycle events that we can access using our apply method’s compiler
argument. The webpack docs provide a list of the names and descriptions of the events accessible from the compiler here, and we can use the tap
method to access any of these events. const SimplePlugin = require("./plugins/simple-plugin.js");
module.exports = {
...
plugins: [
new SimplePlugin(),
]
};
succeedModule
lifecycle event occurs during compilation.