How to set multiple file entry and output in project with webpack?

后端 未结 8 1484
天命终不由人
天命终不由人 2020-12-12 12:14

How to set multiple file entry/output in project with webpack?

I follow http://webpack.github.io/docs/tutorials/getting-started/ success compile if only one file in

8条回答
  •  北海茫月
    2020-12-12 12:58

    You can detect multiple entries and generate separate output files by using glob sync patterns.

    Put this into your webpack.config.js (without the ...)

    const glob = require("glob");
    ...
    module.exports = (env, options) => ({
      ...
      entry: glob.sync("./javascripts/**/*.js").reduce((acc, item) => {
        const path = item.split("/");
        path.pop();
        const name = path.join('/');
        acc[name] = item;
        return acc;
      }, {}),
      output: {
        filename: "[name]/bundle.js",
        path: path.resolve(__dirname, "")
      },
      ...
    });
    

    This "should" give you the desired output.

提交回复
热议问题