The problem with webpack's splitChunks.

has recently been trying to build a general multi-page and multi-entry application using webpack4.

both the common component and the third-party node _ modules are extracted through splitChunks. Look, the results are separated.

the html file for each entry page is specified through html-webpack-plugin.

the question arises, how to dynamically add chunk js files separated from splitChunks to html? Is there a ready-made plug-in, or can you only separate the file and add it manually?

Jun.10,2021

the newly separated chunk block can specify the desired chunk block in the html template. The following figure shows my configuration

clipboard.png
common/common.js common/vendor.js chunk

clipboard.png
specify a specific block in each html template

I hope I can help you!


you can extract the entry configuration from the htmlwebpackplugins configuration by traversing.
you can refer to webpack-mutipage-tempalte
this is the entry configuration section

.
// 
const glob = require('glob')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { resolve } = require('./utils')

const entries = glob.sync(resolve('src/page/**/*.js'))
const entriesOpt = {}
const pluginsOpt = []

const faviconPath = resolve('src/image/favicon/favicon.png')

entries.forEach(item => {
  const chunkName = item.match(/src\/page\/(.+)\.js/)[1]
  const templatePath = item.replace(/.js$/, '.html')
  let fileName = templatePath.match(/src\/(page\/.+\.html$)/)[1]

  entriesOpt[chunkName] = item

  pluginsOpt.push(
    new HtmlWebpackPlugin({
      favicon: faviconPath,
      filename: fileName,
      template: templatePath,
      chunks: ['vendor', 'manifest', 'common-styles', chunkName],
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
      }
    })
  )
})

const config = {
  entry: entriesOpt,
  plugins: pluginsOpt
}

module.exports = config
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e445c7-43a23.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e445c7-43a23.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?