Search code examples
javascripttypescriptwebpackwebpack-2

specifying a complete sub directory in webpack 2 entry


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...

Solution

  • 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);