specifying a complete sub directory in webpack 2 entry

半腔热情 提交于 2019-12-01 12:53:26

问题


I have a webpack 2 configuration as follows:

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' }
        ]
    }
};

This is imported into a gulp task as follows...

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

I am finding that I have to specify each component in entry[...].

How do I specify globs, they don't seem to work out of the box.

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

回答1:


You can use a glob library like globule. globule.find returns an array of the files. So you can use it as the entry:

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

If you want to include other entry points as well you can combine them by spreading the returned array:

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

Or use any other way of combining the arrays (e.g. Array.prototype.concat).

Alternatively you can use a single entry that imports everything you need with the help of require.context as shown in the question webpack require every file in directory.

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


来源:https://stackoverflow.com/questions/43076133/specifying-a-complete-sub-directory-in-webpack-2-entry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!