在 webpack 2 条目中指定一个完整的子目录

specifying a complete sub directory in webpack 2 entry

我有一个webpack 2配置如下:

module.exports = {
    context: __dirname,
    entry: [
        "./app.ts",
        "./tab.ts",
        "./client/clientService.ts",
        "./client/clientSearchComponent.ts",
        "./infrastructure/messageComponent.ts",
        "./infrastructure/typeaheadComponent.ts",
        "./url.ts"],
    output: {
        filename: "./wwwroot/js/admin/admin.js"
    },
    devtool: "source-map",
    module: {
        rules: [
            { test: /\.ts$/, use: 'ts-loader' }
        ]
    }
};

这被导入到 gulp 任务中,如下...

gulp.task("admin:js",
    function (done) {
        var configuration = require(path.join(__dirname, config.js, "admin/webpack.config.js").toString());
        webpack(configuration).run(reportWebpackResults(done));
    });

我发现我必须在 entry[...] 中指定每个组件。

如何指定 glob,它们似乎不是开箱即用的。

entry: [
    "./client/**/*.ts", // module not found...

您可以使用像 globule 这样的 glob 库。 globule.find returns 一组文件。所以你可以用它作为入口:

entry: globule.find("./client/**/*.ts")

如果您还想包括其他入口点,您可以通过展开返回的数组来组合它们:

entry: [
    './other/entry.js'
    ...globule.find("./client/**/*.ts")
]

或使用任何其他方式组合数组(例如 Array.prototype.concat)。

或者,您可以使用单个条目,在 require.context as shown in the question 的帮助下导入您需要的所有内容。

const req = require.context('./client/', true, /\.ts$/);
req.keys().forEach(req);