webpack index.html空白,webpack4.x使用踩坑

webpack.config.js简单配置:

1.

module.exports={

entry:{

a:'./src/index.js'

},

output:{

path:__dirname+'/dist',

filename:'bundel.js'

}

}

2.

const path=require('path');

module.exports={

entry:{

a:'./src/index.js'

},

output:{

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

filename:'bundel.js'

}

}

3.     4.x版本可以修改配置文件名为:ltc.config.js

运行时候:webpack --config ltc.config.js

在package.json中配置script

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1",

"build":"webpack --config ltc.config.js"

},

4. 多入口,多出口,html插件的使用,

建一个src,目录,里面有index.js,然后webpack.config.js配置

const path=require('path');

module.exports={

entry:'./src/index.js',

output:{

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

filename:'bundle.js'

}

}

根目录建一个dist目录里面写个静态index.html测试如下:

9ca2710b6695f95e6c16eece416086af.png e93d6adfeb7512bb30556d4b161dff08.png

webpack4.x以后在package.json配置一下:

{

"name": "webpackdemo",

"version": "1.0.0",

"description": "",

"main": "webpack.config.js",

"scripts": {

"test": "echo \"Error: no test specified\" && exit 1",

"build":"webpack --mode development"

},

"keywords": [],

"author": "",

"license": "ISC"

}

多入口一个出口配置:建一个index2.js,目录结构如下:

bc79ef541e8e75734605dc3d9f106ae0.png

module.exports={

entry:['./src/index.js','./src/index2.js'],

output:{

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

filename:'bundle.js'

}

}

最后打包生成:bundle.js

多入口多出口:

module.exports={

entry:{

index:'./src/index.js',

index2:'./src/index2.js'

},

output:{

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

filename:'[name].bundle.js'

}

}

最后生成:

a9305af30dc686571477fe691dc13880.png

这样就比较麻烦了,你要手动修改创建的dist目录下的index.html引用的文件名

5.借用html-webpack-plugin自动生成dist文件并且index.html自动引入依赖文件,生成页面

cnpm i html-webpack-plugin -D

注意:html-webpack-plugin依赖webpack,和webpack-cli,这两个都需要本地安装一下

模版的使用:

module.exports={

entry:{

index:'./src/index.js',

index2:'./src/index2.js'

},

output:{

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

filename:'[name].bundle.js'

},

plugins:[

new HtmlWebpackplugin({

hash:true,

title:'webpack精髓',

template:'./src/index.html'

})

]

}

然后在./src/index.html配置即可(可设置hash:true清除缓存):

打包生成如下:

0f2a6cb761cab6deb740d5b721d11bd4.png

也可以设置:plugins:[

new HtmlWebpackplugin({

hash:true,

minify:{

collapseWhitespace:true

},

title:'webpack精髓',

template:'./src/index.html'

})

]

整个HTMl空白的被压缩

后续更新.................................................

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值