webpack5-[html-webpack-plugin]的使用
2020-01-13 04:51
573 查看
webpack5-[html-webpack-plugin]的使用
一、为什么要用 html-webpack-plugin 插件,这个插件有什么用?
html-webpack-plugin 插件可以在内存中生成 html 文件
html-webpack-plugin 插件可以在生成的文件中自动帮我们导入合适的js文件
二、步骤
-
初始化
npm init -y
-
安装
npm i webpack webpack-cli webpack-dev-server html-webpack-plugin --save-dev
-
拉取jQuery
npm i jquery -d
-
编写js文件
import $ from 'jquery' $(function () { $('li:odd').css("color", "pink") $('li:even').css("color", "skyblue") })
-
编写html文件
<ul> <li>这是第1个li</li> <li>这是第2个li</li> <li>这是第3个li</li> <li>这是第4个li</li> <li>这是第5个li</li> <li>这是第6个li</li> <li>这是第7个li</li> <li>这是第8个li</li> <li>这是第9个li</li> <li>这是第10个li</li> </ul>
-
在package.json文件中添加
"dev": "webpack-dev-server"
-
编写配置文件 webpack.config.js
const path = require('path') const webpack = require('webpack') const htmlwebpackplugin = require('html-webpack-plugin') // 引入html-webpack-plugin插件 module.exports = { entry: path.join(__dirname, './src/js/index.js'), output: { path: path.join(__dirname, './dist'), filename: "index.js" }, mode: "development", devServer: { open: true, port: 3000, contentBase: "src", hot: true }, plugins: [ new webpack.HotModuleReplacementPlugin(), new htmlwebpackplugin({ // html-webpack-plugin 插件对象 template: path.join(__dirname, './src/index.html'), // 指定模板文件 filename: "index.html" //设置内存中的文件名 }) ] }
-
启动
npm run dev
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- htmlWebpackPlugin的使用
- [js高手之路]深入浅出webpack教程系列4-插件使用之html-webpack-plugin配置(上)
- [js高手之路]深入浅出webpack教程系列6-插件使用之html-webpack-plugin配置(下)
- 9.1webpack配置文件以及webpack-dev-server 工具html-webpack-plugin插件使用
- webpack插件:使用HtmlWebpackPlugin在页面中使用EJS语法引用模板文件(二)
- html-webpack-plugin插件的详细介绍和使用
- html-webpack-plugin 中使用 title选项设置模版中的值无效
- [js高手之路]深入浅出webpack教程系列4-插件使用之html-webpack-plugin配置(上)
- [js高手之路]深入浅出webpack教程系列6-插件使用之html-webpack-plugin配置(下)
- html-webpack-plugin插件使用chunks属性时报错
- webpack学习之二,管理输出和html-webpack-plugin使用
- webpack的使用(插件HtmlWebpackPlugin)
- 使用webpack-html-plugin打包生成html页面失败,是什么原因啊?大神们,求助
- 使用html-webpack-plugin'入再内存中生成 html 页面插件
- html-webpack-plugin 中使用 title选项设置模版中的值无效
- [js高手之路]深入浅出webpack教程系列5-插件使用之html-webpack-plugin配置(中)
- HtmlWebpackPlugin插件使用实现html文件自动生成
- webpack 插件html-webpack-plugin的具体使用
- webpack 插件: html-webpack-plugin
- webpack入门(六)——html-webpack-plugin