简体   繁体   English

Webpack:供应商捆绑包未导入到主输出中

[英]Webpack: vendor bundle not imported in main output

I'm struggling with what looks like a generic error from Webpack after trying to optimise my source code. 在尝试优化我的源代码后,我正在努力解决Webpack出现的一般错误。

Assuming I have the following files in ./src: 假设我在./src:有以下文件./src:

 ├── main.js ├── moduleA.js └── moduleB.js 
main.js imports and uses ModuleA.

moduleA.js imports and uses ModuleB

ModuleA.js and ModuleB.js both import flatten-array from node_modules

My expectation is that if I try to optimise my bundle (see below) it will output two files: 我的期望是,如果我尝试优化我的捆绑包(见下文),它将输出两个文件:

1. index.js
2. vendors~main.index.js

Trying to execute the index.js output bundle results in: 尝试执行index.js输出包会导致:

/******/        modules[moduleId].call(module.exports, module,
module.exports, __webpack_require__);
                                 ^

TypeError: Cannot read property 'call' of undefined

Although the files are generated, index.js doesn't appear to import vendors~main.index.js. 尽管已生成文件,但index.js似乎没有导入vendor〜main.index.js。 Yet it executes fine when removing the optimization (and vendors javascript) 但是,当删除优化时(和供应商javascript),它执行得很好

Is this the correct assumption? 这是正确的假设吗? How can I make it work like this? 如何使它像这样工作?

While this is a bundle for Node, there are valid reasons that I'd like to export a vendors file. 尽管这是Node的捆绑包,但出于有效的原因,我想导出供应商文件。

Accompanying git repo to reproduce available here: 随附git repo以在此处复制以重现:

https://github.com/supasympa/webpack-vendors-issue https://github.com/supasympa/webpack-vendors-issue

Files are: 文件是:

main.js main.js

const  moduleA  = require('./moduleA');

moduleA.log('log from main.js');

moduleA.js moduleA.js

const moduleB = require('./moduleB');
const flatten = require('array-flatten');

module.exports.log = function(msg){
    moduleB.log('logging from moduleA.js');
    console.log(`ModuleA logging: ${msg}`);
    console.log(`flattened: ${flatten([[1,2,3],[4,5],[6,7]])}`)
};

moduleB.js moduleB.js

const flatten = require('array-flatten');

module.exports.log = function(msg){
    console.log(`ModuleB logging: ${msg}`);
    console.log(`flattened: ${flatten([[1,2,3],[4,5],[6,7]])}`)
};

webpack.config.js webpack.config.js

const CleanWebpackPlugin = require('clean-webpack-plugin');

    module.exports = {
        module: {
            rules: [{
                include: [path.resolve(__dirname, 'src')],
                loader: 'babel-loader',

                options: {
                    plugins: ['syntax-dynamic-import'],

                    presets: [['env', {
                        'modules': 'commonjs'
                    }]]
                },

                test: /\.js$/
            }]
        },

        entry: './src/main',
        target: 'node',

        output: {
            filename: 'index.js',
            path: path.resolve(__dirname, 'dist')
        },

        mode: 'development',

        optimization: {
            splitChunks: {
                cacheGroups: {
                    vendors: {
                        priority: -10,
                        test: /[\\/]node_modules[\\/]/,
                        enforce: true
                    },
                },
                // concatenateModules: false,
                chunks: 'all',
                minChunks: 1,
                minSize: 0,
                name: true
            }
        },

        plugins: [
            new CleanWebpackPlugin(['dist']),
        ]
    };

Defining, chunks: 'all', you're explicitly taking all initial entries and async imports (by default it only on-demand async chunks) and specifying the bundler to create a new vendor chunk/file out of that. 定义chunks: 'all',您将显式地获取所有初始条目和异步导入(默认情况下,仅导入按需异步块),并指定捆绑程序以从中创建新的供应商块/文件。

So the behavior is as expected. 因此,行为是预期的。 The idea is to remove the common dependencies from entry files so they can be shared and the page has to load less vendors/common code overall. 这样做的想法是从条目文件中删除公共依赖项,以便可以共享它们,并且页面必须加载较少的供应商/通用代码。

One way to explicitly control what the entry files include is this pattern: https://github.com/webpack/webpack/tree/master/examples/two-explicit-vendor-chunks 明确控制条目文件包括的一种方法是这种模式: https : //github.com/webpack/webpack/tree/master/examples/two-explicit-vendor-chunks

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

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