拓荒
拓荒
  • 发布:2020-11-21 09:25
  • 更新:2022-07-29 16:49
  • 阅读:7684

vue.config.js配置configureWebpack的optimization splitChunks页面空白

分类:uni-app

uni-app 的vue.config.js不支持 configureWebpack 配置文件分割吗?配置了页面就是空白的,也不报错。

示例:

module.exports = {  
  configureWebpack: config => {  

    config.optimization = {  
      splitChunks: {  
        chunks: "all",  
        cacheGroups: {  
          vuex_vue_moment: {  
            name: "modular",  
            test: /[\\/]node_modules[\\/]|vue|moment[\\/]/,  
            priority: -10,  
            chunks: "initial"  
          },  
          vendors: {  
            name: "chunk-vendors",  
            test: /[\\/]node_modules[\\/]/,  
            priority: -20,  
            chunks: "initial"  
          }  
        }  
      }  
    }  
  }  
}  
2020-11-21 09:25 负责人:无 分享
已邀请:
aiplat点com

aiplat点com - aiplat.com

不要chunks: "all",
splitChunks.chunks要改成 chunks: 'async',

splitChunks: {
chunks: 'async',
minSize: 20000,
maxSize: 0,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: Infinity,
automaticNameDelimiter: '~',
name: true,
cacheGroups: {
vendor: {
test: /[\/]node_modules[\/]/,
name(module) {
const packageName = module.context.match(/[\/]node_modules\/([\/]|$)/)[1];
return npm.${packageName.replace('@', '')};
},
priority: 9,
minChunks: 1,
reuseExistingChunk: true,
enforce: true,
// chunks: 'all',
},
commons: {
name: 'commons',
priority: 8,
minChunks: 2,
reuseExistingChunk: true,
enforce: true,
chunks: 'all',
},
default: {
minChunks: 2,
priority: 7,
}
}
}

4***@qq.com

4***@qq.com


// 需结合webpack-bundle-analyzer来拆解自己项目中的chunk-vendors  
// optimization.splitChunks拆分出来的chunk,需要在htmlWebpackPlulgin中的chunks里手动写入  
// 才会被引入到index.html中,否则只做了拆分,没引入,页面就会空白  

const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;  

function changeHtmlWebpackPluginChunks(config) {  
    const plugins = config.plugins;  

       // 这里要和下面的optimization.splitChunks分离出来的对应好,否则无法引入  
       // 你的项目可能和我这不一样,请按着自己项目进行拆分  
    const chunkArr = ["runtime","chunk-@dcloudio", "chunk-core-js", "chunk-elliptic", "chunk-bn", "uview-ui", "chunk-pages"];  

    plugins.forEach((item, index) => {  
        // 找到HtmlWebpackPlugin  
        if( item.options   
            && Object.prototype.toString.call(item.options) === "[object Object]"  
            && Object.hasOwnProperty.call(item.options, 'filename')  
            && Object.hasOwnProperty.call(item.options, 'chunks')  
            && item.options.filename === "index.html"  
            ) {  
                const oldChunks = item.options.chunks || [];  
                // 修改chunks引入  
                item.options.chunks = [...chunkArr, ...oldChunks];  
        }  
    });  
}  

module.exports = {  
 configureWebpack: config => {  
   config.plugins.push(new BundleAnalyzerPlugin());  

   if (process.env.NODE_ENV === 'production') {  
     changeHtmlWebpackPluginChunks(config);  
   }  
 },  
  chainWebpack: config => {  
     config.when(process.env.NODE_ENV === "production", config => {  
        config.optimization.splitChunks({  
                chunks: "all", // 必须三选一: "initial"(同步包) | "all"(推荐,同步或异步包) | "async" (默认就是async,异步包)  
                automaticNameDelimiter: '~', // 打包分隔符  
                name: function(module, chunks, cacheGroupKey) {  
                    const moduleFileName = module  
                        .identifier()  
                        .split('/')  
                        .reduceRight((item) => item);  
                    const allChunksNames = chunks.map((item) => item.name).join('~');  
                    return `${moduleFileName}`;  
                },  
                cacheGroups: {  
                    "uview-ui": { // 把uview-ui拆分出来  
                        name: "uview-ui",  
                        test: /uview-ui/,  
                        chunks: "all",  
                        reuseExistingChunk: true,  
                        enforce: true,  
                        priority: 100,   
                        minChunks: 1,    
                        minSize: 30000,  
                    },  
                    "pages": { // 把pages.json拆分出来  
                        name: "chunk-pages",  
                        test: /pages\.json$/,  
                        chunks: "all",  
                        reuseExistingChunk: true,  
                        enforce: true, // 强行分离  
                        priority: 90, // 打包优先级权重值  
                        minChunks: 1,  // 引用超过一次直接打包到chunk-pages中  
                        minSize: 10000,  
                    },  
                    "core-js": {  
                        name: "chunk-core-js",  
                        test: /core-js/,  
                        chunks: "initial",  
                        reuseExistingChunk: true,  
                        enforce: true  
                    },  
                    "@dcloudio": { // vue也在这里面  
                        name: "chunk-@dcloudio",  
                        test: /@dcloudio/,  
                        chunks: "initial",  
                        reuseExistingChunk: true,  
                        enforce: true  
                    },  
                    "elliptic": {   
                        name: "chunk-elliptic",  
                        test: /elliptic/,  
                        chunks: "initial",  
                        reuseExistingChunk: true,  
                        enforce: true  
                    },  
                    "bn": {  
                        name: "chunk-bn",  
                        test: /bn/,  
                        chunks: "initial",  
                        reuseExistingChunk: true,  
                        enforce: true  
                    }  
                }  
            });  

                    // 提取公共runtime  
            config.optimization.runtimeChunk("single");  
          })  
     }  
  }  
}  
今天回复我了吗

今天回复我了吗 - uniapp 粉丝条

同样没有效果

aiplat点com

aiplat点com - aiplat.com

解决了?

该问题目前已经被锁定, 无法添加新回复