简体   繁体   English

Webpack将项目中的所有js文件构建为多个输出文件

[英]Webpack build all js files in project to multiple output files

I am trying to change my webpack config file to handle all js and jsx files in project to a multiple output files (output file name should be the entry file name). 我试图将我的webpack配置文件更改为将项目中的所有js和jsx文件处理为多个输出文件(输出文件名应为条目文件名)。 I didnt find any simple solution for that. 我没有找到任何简单的解决方案。

You can create multiple entry files, and keep the name in the output files. 您可以创建多个条目文件,并将名称保留在输出文件中。

entry: {
    src: './src/app.js',
    foo: './src/foo.js',
},
output: {
    path: __dirname + '/dist',
    filename: '[name].js',
},

From the documentation: 从文档中:

If an object is passed, each key is the name of a chunk, and the value 
describes the entrypoint for the chunk.

You can also pass a function as the entry to do more complicated things. 您还可以将函数作为条目传递,以执行更复杂的事情。

EDIT: 编辑:
For example, this script will glob all js files in the src directory and create an entry point for each. 例如,此脚本将在src目录中遍历所有js文件,并为每个文件创建一个入口点。

const glob = require('glob');

module.exports = () => {

    return {
        mode: 'development',
        entry: () => {
            return glob.sync('./src/*.js').reduce((pre, cur) => {
                pre[cur.replace(/^.*[\\\/]/, '').split('.')[0]] = cur;
                return pre;
            }, {});
        },
        output: {
            path: __dirname + '/dist',
            filename: '[name].js',
        },
    };
};

You can probably clean up the regex a bit 您可能可以清理一下正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM