在 Webpack 中执行代码分割

  • Post author:
  • Post category:其他


有三种常用的代码分离方法:

  • 入口起点:使用


    entry


    配置手动地分离代码。
  • 防止重复:使用


    CommonsChunkPlugin


    去重和分离 chunk。
  • 动态导入:通过模块的内联函数调用来分离代码。

1、入口起点

这是迄今为止最简单、最直观的分离代码的方式。不过,这种方式手动配置较多

webpack.config.js

const path = require('path');
const HTMLWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: {
    index: './src/index.js',
    another: './src/another-module.js'
  },
  plugins: [
    new HTMLWebpackPlugin({
      title: 'Code Splitting'
    })
  ],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist')
  }
};
这将生成如下构建结果
Hash: 309402710a14167f42a8
Version: webpack 2.6.1
Time: 570ms
            Asset    Size  Chunks                    Chunk Names
  index.bundle.js  544 kB       0  [emitted]  [big]  index
another.bundle.js  544 kB       1  [emitted]  [big]  another
   [0] ./~/lodash/lodash.js 540 kB {0} {1} [built]
   [1] (webpack)/buildin/global.js 509 byte



版权声明:本文为u012207345原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。