Option chunks Html Webpack Plugin (ok)
https://kentrung256.blogspot.com/2021/06/html-webpack-plugin.html
Ở phần trên lúc tạo ra hai trang dist/index.html và dist/about.html thì cả hai trang này đều load chung một file dist/main.js. Để tách riêng từng trang load từng file JS ta tạo key entry và sử dụng chunks để thêm vào
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: {
homepage: ['./src/index.js', './src/slider.js'],
aboutpage: ['./src/about.js']
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist')
},
plugins: [
new HtmlWebpackPlugin({
title: 'Trang chủ',
filename: 'index.html',
template: './src/index.html',
chunks: ['homepage']
}),
new HtmlWebpackPlugin({
title: 'Về chúng tôi',
filename: 'about.html',
template: './src/about-dev.html',
chunks: ['aboutpage']
})
]
}
Với cấu hình trên thì ta có hai trang HTML load hai file JS riêng biệt. Trang dist/index.html load file dist/homepage.js là kết hợp của hai file src/index.js và src/slider.js Trang dist/about.html load file dist/aboutpage.js là code file src/about.js
PreviousWebpack từ A đến Á: Html Webpack Plugin (ok)NextWebpack từ A đến Á: Clean Webpack Plugin (ok)
Last updated