Using Webpack To Transpile ES6 as separate files

后端 未结 1 1851
离开以前
离开以前 2021-02-04 15:47

Is it possible to configure webpack to do the equivalent of:

babel src --watch --out-dir lib

So that a directory structure that looks like this

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 15:53

    If you want to output to multiple directories, you can use the path as the entry name.

    entry: {
      'foo/f.js': __dirname + '/src/foo/f.js',
      'bar/b.js': __dirname + '/src/bar/b.js',
    },
    output: {
      path: path.resolve(__dirname, 'lib'),
      filename: '[name]',
    },
    

    Therefore you can use a function to generate a list of entries for you that satisfy the above:

    const glob = require('glob');
    
    function getEntries(pattern) {
      const entries = {};
    
      glob.sync(pattern).forEach((file) => {
        entries[file.replace('src/', '')] = path.join(__dirname, file);
      });
    
      return entries;
    }
    
    module.exports = {
      entry: getEntries('src/**/*.js'),
      output: {
        path: path.resolve(__dirname, 'lib'),
        filename: '[name]',
      },
      // ...
    }
    

    0 讨论(0)
提交回复
热议问题