最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Multiple dynamic entry scripts in webpack? - Stack Overflow

programmeradmin4浏览0评论

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 mon/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 mon lib folder) into a structure like this?

e.g.

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

But without individually defining each entry file? Something like gulp does with /**/*.js syntax?

My goal is NOT to have to maintain my webpack.config.js file each time I add a new module/ponent.

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 mon/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 mon lib folder) into a structure like this?

e.g.

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

But without individually defining each entry file? Something like gulp does with /**/*.js syntax?

My goal is NOT to have to maintain my webpack.config.js file each time I add a new module/ponent.

Share Improve this question asked Jun 14, 2016 at 1:16 AgmLauncherAgmLauncher 7,27010 gold badges49 silver badges71 bronze badges 2
  • 3 A webpack config file is JavaScript. You can read the directory structure and build the desired paths. – Felix Kling Commented Jun 14, 2016 at 1:26
  • Did you ever find out how? – chh Commented Sep 3, 2017 at 7:18
Add a ment  | 

2 Answers 2

Reset to default 4

You need glob package and set entry and output in your webpack.config.js. Example when webpack.config.js in root dir.

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

node: {
    fs: "empty"
}

Also, you can bundle your files from <other files> into public script.js using other entryObject.

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

const entryName = path.parse(entry).name - extract filename from file path

entries[entryName] = entry - construct entry record

as a result you will have something like

{
    file1: 'path/to/file1.js',
    file2: 'path/to/file2.js',
    ...
}
发布评论

评论列表(0)

  1. 暂无评论