1. 作用:
  2. 1、可以写es6代码
  3. 2es6模块化
  4. 3、压缩代码

2-1 配置

  1. cnpm init -y
  2. cnpm i webpack webpack-cli -S
  3. yarn init -y
  4. yarn add webpack webpack-cli

2-2 创建src文件夹

src index.js

  1. # index.js
  2. console.log("hello")

2-3 新建webpack.config.js

  1. //模快化语法
  2. const path = require("path");
  3. module.exports ={
  4. //开发模式
  5. mode:"development",
  6. //打包入口 从哪一个文件开始打包
  7. entry:path.join(__dirname,"src","index.js"),
  8. // 打包好的文件放在哪里
  9. output:{
  10. //路径
  11. path:path.join(__dirname,'dist'),
  12. filename:'bundle.js'
  13. },
  14. }

2-4 配置脚本

  1. # package.json
  2. "scripts": {
  3. "build": "webpack",
  4. },

2-5 执行脚本

  1. npm run build

打包完毕,生成文件

image.png

plugins

1 给打包文件一个html文件

src index.html

  1. #index.html
  2. <div>webpack 入门</div>

配置

  1. cnpm i html-webpack-plugin -S
  2. yarn add html-webpack-plugin
  3. //自动生成index.html文件

webpack.config.js

  1. 引入
  2. const HtmlWebpackPlugin = require("html-webpack-plugin")
  3. module.exports ={
  4. ...
  5. plugins:[
  6. new HtmlWebpackPlugin({
  7. //以哪个文件为模板
  8. template:path.join(__dirname,'src','index.html'),
  9. //文件名
  10. filename:'index.html'
  11. })
  12. ],
  13. }

打包完毕 自动引入html

image.png