简体   繁体   English

单独的包形成动态导入的节点模块

[英]Separate bundles form dynamically imported node modules

I have the following configuration in webpack config for split chunks.我在 webpack 配置中有以下配置用于拆分块。 This perfectly creates two bundles vendors (node modules that were imported in initial files) and common(node modules that were imported in the lazy-loaded components)这完美地创建了两个捆绑供应商(在初始文件中导入的节点模块)和公共(在延迟加载的组件中导入的节点模块)

splitChunks: {
        name: true,
        cacheGroups: {
          default: false,
          vendors: false,
          vendor: {
            test: /[\\/]node_modules[\\/]/,
            name: "vendors",
            chunks: "initial"
          },
          common: {
            test: /[\\/]node_modules[\\/]/,
            name: "common",
            minChunks: 2,
            chunks: "async",
            priority: 10,
            reuseExistingChunk: true,
            enforce: true
          }
        }
      },

Question- But there are some node modules that I do not want to be included in any of the bundles.问题 -但有些节点模块我不想包含在任何捆绑包中。 Neither the above two nor the main bundles but to be created separately alone.既不是上述两个也不是主要的捆绑包,而是单独创建。

import('lodash') should create a lodash.chunk.js. import('lodash')应该创建一个 lodash.chunk.js。

import('underscore') should create a underscore.chunk.js. import('underscore')应该创建一个 underscore.chunk.js。

Can I do it thought the magic comments?我能想到神奇的评论吗? /* webpackIgnore: true*/ /* webpackIgnore: true*/

Here your lodash will get added to the common chunk based on the current splitchunks configuration.在这里,您的 lodash 将根据当前的 splitchunks 配置添加到公共块中。 To load it as an separate chunk, change your splitchunks configuration to要将其作为单独的块加载,请将您的 splitchunks 配置更改为

var vendorCheck = function vendorCheck(module) {
  var request = module.userRequest;
  // Specify the packages in the condition that need not be present in vendor chunk
  // Here I am excluding the package lodash from vendor chunk
  return request && request.indexOf('node_modules') >= 0 && request.indexOf('node_modules/lodash') === -1
};

var lodashCheck = function lodashCheck(module) {
  var request = module.userRequest;
  return request && request.indexOf('node_modules/lodash') >= 0
}

cacheGroups: {
    default: false,
    'lodash': {
      name: 'lodash',
      chunks: 'all',
      minChunks: 1,
      test: lodashCheck
    },
    vendor: {
      name: 'vendor',
      chunks: 'all',
      minChunks: 1,
      test: vendorCheck
    },
    common: {
        test: vendorCheck,
        name: "common",
        minChunks: 2,
        chunks: "async",
        priority: 10,
        reuseExistingChunk: true,
        enforce: true
    }
}

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

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