账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    webpack splitChunks
    • 2020-01-01 00:00
    • 11
    30
    0

    请教各大家一个问题,index.js里面的components.js和mixins.js文件在其他的js里面也引入了,但是打包的时候,这两个没有被打包到common.js里面???只有vue被打包成了vendor.js

    // webpack.prod.js
    
    const merge = require('webpack-merge')
    const commonConfig = require('./webpack.common.js')
    
    const prodConfig = {
      mode: 'production',
      // devtool: 'cheap-module-source-map',
      optimization: {
        splitChunks: {
          cacheGroups: {
            vendors: {
              name : 'vendor',
              test: /[\\/]node_modules[\\/]/,
              minChunks : 2,
              chunks : 'all',
              priority: -10,
            },
            default: {
              name: 'common',
              minChunks: 2,
              chunks : 'all',
              priority: -20,
              reuseExistingChunk: true,
            }
          }
        }
      }
    }
    
    module.exports = merge(commonConfig, prodConfig)
    // index.js
    
    import '../css/index.css'
    import Vue from 'vue'
    import '../../public/js/components.js'
    import mixins from '../../public/js/mixins.js'
    
    console.log('index red')
    
    var app = new Vue({
      mixins: [mixins],
      el: '#app',
      methods: {
        alert () {
          window.alert('index')
        }
      },
      created() {
        console.log('index created')
      }
    })
    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 核桃奶 普通会员 1楼

      Webpack is a popular module bundler that is commonly used for building web applications. It allows developers to split large JavaScript files into smaller chunks that can be loaded more efficiently, reducing the overall file size and improving the performance of the application. One way to split chunks in Webpack is by using the splitChunks plugin. The splitChunks plugin is included in Webpack 4 and higher versions. Here's an example of how to use the splitChunks plugin to split a large JavaScript file into smaller chunks:

      1. Install the splitChunks plugin using npm:

      bash npm install --save-dev webpack-plugin-cut chunks

      1. In your Webpack configuration file, add the webpack-plugin-cut and chunks plugins to the plugins array:

      javascript module.exports = { // ... plugins: [ // ... new webpack.optimize.SplitChunksPlugin({ name: 'myScript.js', chunks: 'all', minSize: 1000000, // in bytes maxSize: 50000000, // in bytes reuseExistingChunk: true, // true by default }), // ... ], // ... };

      1. In your JavaScript file, you can use the chunk property to specify which chunks to split. For example:

      ```javascript const myScript = require('./myScript.js');

      myScript.forEach((chunk) => { // chunk can be any JavaScript chunk }); ```

      1. Finally, you can load the smaller chunks in your HTML by using the import statement. For example:

      ```html

      ```

      In this example, the myScript-1.js, myScript-2.js, and myScript-3.js chunks will be split into smaller chunks and loaded from disk, improving the performance of the application.

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部