Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

javascript - Unpredictable behaviors of webpack code splitting

I have spent reasonable time now in understanding webpack code splitting and its splitchunksplugin. Still there are some unpredictable outputs which is frustrating for me. I believe there maybe some minor loopholes in my understanding where community can help me with.

I have two entry points with async modules

a.js

import { run } from "./app/app";
import ("lodash");
// import "bootstrap";
import "./main.scss";
import { AlertService } from "./app/alert.service";
import { ComponentService } from "./app/component.service";
const alertService = new AlertService();
const componentService = new ComponentService();
run(alertService, componentService);

b.js

import { run } from "./app/app";
import ("lodash");
// import ("bootstrap");
import "./main.scss";
import { AlertService } from "./app/alert.service";
import { ComponentService } from "./app/component.service";
const alertService = new AlertService();
const componentService = new ComponentService();
run(alertService, componentService);

My webpack config is as follows

module.exports = {
  entry: {
    a: './src/a.js',
    b: './src/b.js'
  },
  module: {
    rules: [
      {
        test: /.scss$/,
        use: [
          "style-loader", //3. Inject styles into DOM
          "css-loader", //2. Turns css into commonjs
          "sass-loader" //1. Turns sass into css
        ]
      },
      {
        test: /.html$/,
        use: ["html-loader"]
      },
      {
        test: /.(svg|png|jpg|gif)$/,
        use: {
          loader: "file-loader",
          options: {
            name: "[name].[hash].[ext]",
            outputPath: "imgs"
          }
        }
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./src/template.html"
    }),
    new BundleAnalyzerPlugin()
  ],
  optimization: {
    minimize: false,
    splitChunks: {
      chunks: 'initial',
      cacheGroups: {
        default: false,
        vendors: false
      }
    }
  }
};

Here I am targeting only initial chunks to be evaluated for optimization and also turned off every cache group. Now my understanding says that it should output two async chunks containing lodash and two sync entrypoint chunks but what it outputs is this:

enter image description here

Now if uncomment bootstrap from either of entry point chunks it outputs two async lodash chunks as I have expected it to. Output when uncomments bootstrap in a.js:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...