webpack4.0入门学习笔记(二)

栏目: JavaScript · 发布时间: 6年前

内容简介:安装

代码下载:github

html-webpack-plugin 的使用

安装

npm i html-webpack-plugin -D

webpack4.0入门学习笔记(一) 中,我们是自己在打包目录下创建index.html对打包后js文件进行引用。

html-webpack-plugin 插件可以根据对应的模板在打包的过程中自动生成index.html,并且能够对打包的文件自动引入。

webpack.config.jsplugins 中配置如下

const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')

module.exports={
  entry: {
    main: './src/index.js'
  },
  //打包完成后文件存放位置配置
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname,'dist')
  },
  
  plugins: [
    new htmlWebpackPlugin({
      template: './index.html'
    })
  ]
}

在终端执行 npm run start ,打包完成后在 dist 目录下自动生成index.html文件,并且还自动引入所有文件。

clean-webpack-plugin 的使用

每次打包生成的dist目录,如果改一次代码,都得要删除一次dist目录,这样很麻烦,可以通过 clean-webpack-plugin 在每次打包的时候自动清空dist目录。

安装

npm i clean-webpack-plugin -D

webpack.config.jsplugins 中配置如下

const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')
const cleanWebpackPlugin = require('clean-webpack-plugin')

module.exports={
  entry: {
    main: './src/index.js'
  },
  //打包完成后文件存放位置配置
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname,'dist')
  },
  
  plugins: [
    new htmlWebpackPlugin({
      template: './index.html'
    }),
    new cleanWebpackPlugin()
  ]
}

以上所述就是小编给大家介绍的《webpack4.0入门学习笔记(二)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Convergence Culture

Convergence Culture

Henry Jenkins / NYU Press / 2006-08-01 / USD 30.00

"Convergence Culture" maps a new territory: where old and new media intersect, where grassroots and corporate media collide, where the power of the media producer, and the power of the consumer intera......一起来看看 《Convergence Culture》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具