简体   繁体   English

webpack中的多个动态入口脚本?

[英]Multiple dynamic entry scripts in webpack?

Let's say I have an app structure like this假设我有一个这样的应用程序结构

app/
   modules/
      module1/
         <other files>
         script.js
      module2/
         <other files>
         script.js
      module3/
         <other files>
         script.js
   lib/
      <some common/shared scripts to import from>
public/
   js/

How can I configure webpack to bundle and output each script.js (which will be importing various libs/utilities from the common lib folder) into a structure like this?如何配置 webpack 将每个script.js (将从公共库文件夹中导入各种库/实用程序)捆绑和输出到这样的结构中?

eg例如

public/js/module1/script.js
public/js/module2/script.js

But without individually defining each entry file?但是没有单独定义每个入口文件? Something like gulp does with /**/*.js syntax?/**/*.js这样的/**/*.js语法?

My goal is NOT to have to maintain my webpack.config.js file each time I add a new module/component.我的目标不是每次添加新模块/组件时都必须维护我的webpack.config.js文件。

You need glob package and set entry and output in your webpack.config.js .您需要 glob 包并在webpack.config.js设置entryoutput Example when webpack.config.js in root dir.根目录中的webpack.config.js示例。

var webpack = require('webpack');
var glob = require('glob');

//Generate object for webpack entry
//rename './app/modules/module1/script.js' -> 'module1/script'
var entryObject = glob.sync('./app/modules/**/script.js').reduce(
    function (entries, entry) {
        var matchForRename = /^\.\/app\/modules\/([\w\d_]+\/script)\.js$/g.exec(entry);

        if (matchForRename !== null && typeof matchForRename[1] !== 'undefined') {
            entries[matchForRename[1]] = entry;
        }

        return entries;
    }, 
    {}
);

module.exports = {
    ...
    entry: entryObject,//{'moduleName/script': './app/modules/moduleName/script.js', ...}
    output: {
        path: __dirname + '/public/js',
        filename: '[name].js'//'moduleName/script.js', [name] - key from entryObject
    }
    ...
};

If you will have error with fs like " can't resolve 'fs' " add option如果您在 fs 中遇到“无法解析 'fs'”之类的错误,请添加选项

node: {
    fs: "empty"
}

Also, you can bundle your files from <other files> into public script.js using other entryObject.此外,您可以使用其他 entryObject 将<other files>文件捆绑到 public script.js 中。

var entryObject = glob.sync('./app/modules/**/*.js').reduce(
    function (entries, entry) {
        var matchForRename = /^\.\/app\/modules\/([\w\d_]+)\/.+\.js$/g.exec(entry);

        if (matchForRename !== null && typeof matchForRename[1] !== 'undefined') {
            var entryName = matchForRename[1] + '/script';

            if (typeof entries[entryName] !== 'undefined') {
                entries[entryName].push(entry);
            } else {
                entries[entryName] = [entry];
            }

        }

        return entries;
    },
    {}
);

You can use this snippet and adjust it for your needs您可以使用此代码段并根据您的需要进行调整

const path = require('path')
const glob = require('glob')

const entries = glob.sync('./src/entry/*').reduce((entries, entry) => {
    const entryName = path.parse(entry).name
    entries[entryName] = entry

    return entries
}, {});

module.exports = {
    entry: entries,
...

'./src/entry/*' - regex to find files from which your entries will be constructed './src/entry/*' - 正则表达式以查找将构建条目的文件

const entryName = path.parse(entry).name - extract filename from file path const entryName = path.parse(entry).name - 从文件路径中提取文件名

entries[entryName] = entry - construct entry record entries[entryName] = entry - 构建条目记录

as a result you will have something like结果你会得到类似的东西

{
    file1: 'path/to/file1.js',
    file2: 'path/to/file2.js',
    ...
}

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

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