简体   繁体   English

webpack:带有webworker和多个块的commonchunksplugin

[英]webpack: commonchunksplugin with webworker and multiple chunks

I work on a legacy application that, for now, have to be split in a very specific way: A simplified case is : I have 4 modules: 我正在开发一个遗留应用程序,目前必须以一种非常具体的方式进行拆分:一个简化的案例是:我有4个模块:

//moduleA.js
export const moduleA = ['moduleA'];


//moduleB.js
import {moduleA} from './moduleA';
export const moduleB = ['moduleB'].concat(moduleA);


//moduleC.js
import {moduleA} from './moduleA';
import {moduleB} from './moduleB';
export const moduleC = ['moduleC'].concat(moduleA).concat(moduleB);


//webworkerModule.js
import {moduleB} from './moduleB';
import {moduleA} from './moduleA';

console.log("Webworker Module", moduleA, moduleB);

What I need to do is to have an output of 5 files: 我需要做的是输出5个文件:

  • manifest.js that contain the common runtime of webpack manifest.js包含webpack的公共运行时
  • moduleA.js that contains only the code of moduleA moduleA.js只包含moduleA.js的代码
  • moduleB.js that contains only the code of moduleB moduleB.js只包含moduleB.js的代码
  • moduleC.js that contains only the code of moduleC moduleC.js只包含moduleC.js的代码
  • webworkerModule.js that contains the code of module A and module B and the webpack runtime webworkerModule.js ,包含模块A和模块B的代码以及webpack运行时

I tried multiple configuration, but I cannot obtain what I want. 我尝试了多种配置,但我无法获得我想要的东西。

const path = require('path');
const webpack = require('webpack');

const config = {
    entry:{
        moduleA:'./src/moduleA.js',
        moduleB:'./src/moduleB.js',
        moduleC:'./src/moduleC.js',
        webworkerModule:'./src/webworkerModule.js',
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js'
    },
    resolve: {
        extensions: ['.js']
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: 'webworkerModule',
            chunks: ['webworkerModule'],
            minChunks: Infinity
        }),
        new webpack.optimize.CommonsChunkPlugin({
            name: ['moduleA', 'moduleB', 'manifest'],
            chunks: ['moduleA', 'moduleB', 'moduleC'],
            minChunks: Infinity
        })
    ]
};

module.exports = config;

The above configuration for example produce the correct webworkerModule.js, but moduleA and moduleB is duplicated in every other files. 例如,上面的配置生成了正确的webworkerModule.js,但是moduleA和moduleB在每个其他文件中都是重复的。

Has someone any insight about this ? 有人对此有任何见解吗?

I found a solution: 我找到了解决方案:

const path = require('path');
const webpack = require('webpack');

const configWW = {
    entry:{
        webworkerModule:'./src/webworkerModule.js',
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js'
    },
    resolve: {
        extensions: ['.js']
    },
};

const configOthers =  {
    entry: {
        moduleA:'./src/moduleA.js',
        moduleB:'./src/moduleB.js',
        moduleC:'./src/moduleC.js',
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js'
    },
    resolve: {
        extensions: ['.js']
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            names: ['moduleB', 'moduleA', 'manifest'],
            minChunks: Infinity
        })
    ]
};

module.exports = [configWW, configOthers];

The main point of this solution was that I didn't know that it was possible to have multiple independent configuration. 这个解决方案的要点是我不知道有多个独立配置是可能的。

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

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