webpack之index.html模板配置

webpack之Index.html模板配置

1、单页面环境搭建
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
 mode:'development',
 plugin: [
  new HtmlWebpackPlugin({
   title:'多页面应用',
   template: './index.html'//模板
   inject: 'body'//将生成的script标签应该放到什么位置
  })
 ],
 
 entry: {
  main: {
   import: ['./src/app.js', './src/app.js'],
   dependOn: 'lodash'
  },
  main2: {
   import: './src/app3.js',
   dependOn: 'lodash'
  },
  lodash: 'loadsh'
 }
}

在index.html文件中

//会自动找到配置的title
<title><%= htmlWebpackPlugin.options.title %></title>
2、多页面环境搭建
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
 mode:'development',
 plugin: [
  new HtmlWebpackPlugin({
   title:'多页面应用',
   template: './index.html'//模板的含义即对应的html文件
   inject: 'body'//将生成的script标签应该放到什么位置
   filename: 'chanel1/index.html',//打包生成的文件名称
   chunks: ['main','lodash'],//在这个文件有哪些模块是需要载入的
   publicPath: 'http://www.a.com/'//载入文件添加的前缀
  }),
  new HtmlWebpackPlugin({
   template: './index2.html'//模板的含义即对应的html文件
   inject: 'body'//将生成的script标签应该放到什么位置
   filename: 'chanel2/index2.html',//打包生成的文件名称
   chunks: ['main2','lodash'],//在这个文件有哪些模块是需要载入的
   publicPath: 'http://www.b.com/',//载入文件添加的前缀
  })
 ],
 
 //入口文件
 entry: {
  main: {
   import: ['./src/app.js', './src/app.js'],
   dependOn: 'lodash',
   filename: 'chanel1/[name].js'//打包生成的文件名称
  },
  main2: {
   import: './src/app3.js',
   dependOn: 'lodash',
   filename: 'chanel2/[name].js'//打包生成的文件名称
  },
  lodash: {
   import: 'lodash',
   filename:'common/[name].js'//打包生成的文件名称
  }
 }
}

当有两个html页面的时候,在配置的时候要指定打包生成的文件名称,若不指定则会发生文件名冲突的问题

3、webpack之sideEffects

package.json文件中配置下面的一个属性

{
 "sideEffects" : ['*.css']//这个选项告诉webpack只要看到css文件都是有副作用的,你就不要删除了,sideEffects的取值有三种形式:false,true以及数组形式
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值