How to add wildcard mapping in entry of webpack

前端 未结 5 1399
长发绾君心
长发绾君心 2020-12-04 16:50

I need to webpack all the js file in the script folder.I tried this

module.exports = {
  module: {
    loaders: [
      {
        test: /\\.js$/,
        ex         


        
5条回答
  •  眼角桃花
    2020-12-04 17:18

    Just using glob.sync will result in sequential filenames, such as 0.[hash].js and 1.[hash].js, because entry expects an object with the the name of the file in the key and its location in the value, but glob.sync returns an array.

    The following method has the benefit of producing an object with keys and values based on the filenames, you can also add additional entries, such as vendor and common. Requires lodash.

    const glob = require("glob");
    const _ = require('lodash');
    
    module.exports = {
      entry: Object.assign({},
        _.reduce(glob.sync("./src/*.js"),
          (obj, val) => {
            const filenameRegex = /([\w\d_-]*)\.?[^\\\/]*$/i;
            obj[val.match(filenameRegex)[1]] = val;
            return obj;
          },
        {}),
        {
          vendor: [
            'lodash'
          ]
        }
      ),
      output: {
        filename: '[name].[chunkhash].bundle.js',
        path: path.resolve(__dirname, 'dist')
      }
    }
    

    The latter will produce the following object and pass it to entry, provided we have index.js and app.js in the ./src directory:

    {
        index: './src/index.js',
        app: './src/app.js',
        vendor: [ 'lodash' ]
    }
    

提交回复
热议问题