Extract plugin to module.

This commit is contained in:
Dillon Kearns 2019-08-19 20:51:53 -07:00
parent ae1f805510
commit 4f1a8cd205
2 changed files with 24 additions and 22 deletions

View File

@ -0,0 +1,23 @@
const path = require("path");
module.exports = class AddFilesPlugin {
constructor(filesList) {
this.filesList = filesList;
}
apply(compiler) {
compiler.hooks.afterCompile.tap("AddFilesPlugin", compilation => {
this.filesList.forEach(file => {
// Couldn't find this documented in the webpack docs,
// but I found the example code for it here:
// https://github.com/jantimon/html-webpack-plugin/blob/35a154186501fba3ecddb819b6f632556d37a58f/index.js#L470-L478
const filename = path.join(file.name, "content.txt");
compilation.fileDependencies.add(filename);
compilation.assets[filename] = {
source: () => file.content,
size: () => file.content.length
};
});
});
}
};

View File

@ -9,28 +9,7 @@ const merge = require("webpack-merge");
const { GenerateSW } = require("workbox-webpack-plugin");
const FaviconsWebpackPlugin = require("favicons-webpack-plugin");
const webpackDevServer = require("webpack-dev-server");
class AddFilesPlugin {
constructor(filesList) {
this.filesList = filesList;
}
apply(compiler) {
compiler.hooks.afterCompile.tap("AddFilesPlugin", compilation => {
this.filesList.forEach(file => {
// Couldn't find this documented in the webpack docs,
// but I found the example code for it here:
// https://github.com/jantimon/html-webpack-plugin/blob/35a154186501fba3ecddb819b6f632556d37a58f/index.js#L470-L478
const filename = path.join(file.name, "content.txt");
compilation.fileDependencies.add(filename);
compilation.assets[filename] = {
source: () => file.content,
size: () => file.content.length
};
});
});
}
}
const AddFilesPlugin = require("./add-files-plugin.js");
module.exports = { start, run };
function start({ routes, debug, manifestConfig }) {