js追加html其他插件失效,html-webpack-plugin插件生成多个html页面引入不同的js文件...

webpack中html-webpack-plugin插件生成多个html页面,引入不同的js文件。只要在插件配置文件中加入:chunks:["入口文件名"]即可,如果不加的话,会在生成的html页面中引入所有的入口文件。const path = require('path');

const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {

entry:{//入口文件

one:"./src/index.js",

two:"./src/main.js"

},

output:{//输出的文件

path:path.resolve(__dirname,'dist'),

filename:'[name].boundle.js'

},

mode:"development",

plugins:[

new HtmlWebpackPlugin({

template:'./src/one.html',

filename:'one.html',//生成的html页面的名字为one.html

title:"one",//它的title为one,记得要在src/one.html中加入

hash:true,

chunks:['one']

}),

new HtmlWebpackPlugin({

template:'./src/two.html',

filename:'two.html',

title:"two",

hash:true,

chunks:['two']

})

]

折腾了一下,生成多入口对象:const path = require('path');

const fs = require('fs');

const HtmlWebpackPlugin = require('html-webpack-plugin');

let entryFiles = {};

const files = fs.readdirSync(path.resolve(process.cwd(), 'src/js/'));

files.forEach(file=>{

const fileName = path.basename(file, '.ts');

entryFiles[fileName] = `./src/js/${file}`;

});

const generateMultiplePages = [];

for (const fileName in entryFiles) {

if (entryFiles.hasOwnProperty(fileName)) {

generateMultiplePages.push(

new HtmlWebpackPlugin({

filename: `${fileName}.html`,

template: `src/${fileName}.html`,

chunks:[fileName],

minify:{

// collapseWhitespace: true,

removeComments: true,

}

})

);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值