在生产中使用此解决方案的修改版本已有一段时间了,我没有遇到任何问题。 (我公司的设置有一个稍微复杂的过滤器,以排除某些模拟模块,但其他方面是相同的)。
我们使用构建脚本来抓取有问题的目录(在我们的设置中,它是 src,但在您的设置中是 src/snippets。对于每个以 .js 结尾的文件,我们将其导入并以 @ 重新导出987654325@。如果你需要更健壮的东西,比如深入多层,你需要修改它以递归遍历目录结构。
完成后,我们将其输出到 index.js,并提醒该文件是自动生成的,以阻止人们手动向文件中添加条目。
const fs = require("fs");
const { EOL } = require("os");
const path = require("path");
let modules = 0;
const buffer = [
"// auto-generated file", "",
];
const emitModule = file => {
const moduleName = file.replace(".js", "");
modules += 1;
buffer.push(`exports.${moduleName} = require("./snippets/${moduleName}");`);
};
const files = fs.readdirSync(__dirname + "/snippets");
files
.filter(fname => fname !== "index.js" && !fname.startsWith("."))
.forEach(f => {
const stats = fs.statSync(path.join(__dirname, "snippets", f));
if (stats.isFile()) {
emitModule(f);
}
});
fs.writeFileSync(path.join(__dirname, "index.js"), buffer.join(EOL)+EOL);
console.info(`Built 'src/index.js' with ${modules} modules`);
然后,在 webpack.config.js 中,我们将 libraryTarget 设置为umd,如下所示:
module.exports = {
entry: path.resolve(__dirname, "src/index.js"),
output: {
path: path.resolve(__dirname, "build/"),
filename: "mylib.js",
libraryTarget: "umd"
}
};
最后,为方便起见,在package.json中,我们使用以下内容在构建之前自动运行构建脚本(你也可以在启动webpack-dev-server之前使用它,或者运行mocha测试)。
这个设置感觉很老套,但效果很好。我遇到的唯一问题是,模块的顺序偶尔会发生变化(可能是因为环境差异),并且文件的枚举会导致 git 中的误报。
我已将整个包放在 GitHub 上:https://github.com/akatechis/webpack-lib-poc
更新:
如果您不想通过将构建脚本添加到您的 package.json scripts 来手动调用构建脚本,您可以随时将其包装为 webpack 插件。你可以阅读详情here
简而言之,插件只是一个带有apply 方法的对象,它向webpack 编译器注册自身。来自文档:
作为一个聪明的 JavaScript 开发人员,您可能还记得 Function.prototype.apply 方法。由于这种方法,您可以将任何函数作为插件传递(this 将指向编译器)。您可以使用此样式在您的配置中内联自定义插件。
以下是两种设置之间的变化:
更改 webpack 配置以导入您的构建脚本并将其添加到 plugins 数组中:
const path = require("path");
const buildPlugin = require("./src/index.build");
module.exports = {
entry: path.resolve(__dirname, "src/index.js"),
output: {
path: path.resolve(__dirname, "build/"),
filename: "mylib.js",
libraryTarget: "umd"
},
plugins: [buildPlugin]
};
然后更改index.build.js 以导出一个在编译器上注册回调的函数(它以this 接收它)。把之前构建脚本的内容,放到一个build()函数中,然后导出一个插件函数如下:
module.exports = function () {
this.plugin('run', function(compiler, callback) {
console.log("Build script starting");
build();
callback();
});
};
从package.json 中的任何脚本中删除build-index 目标。现在,Webpack 将始终在运行之前调用您的构建脚本。