作用:1、可以写es6代码2、es6模块化3、压缩代码
2-1 配置
cnpm init -ycnpm i webpack webpack-cli -Syarn init -yyarn add webpack webpack-cli
2-2 创建src文件夹
src index.js
# index.jsconsole.log("hello")
2-3 新建webpack.config.js
//模快化语法const path = require("path");module.exports ={//开发模式mode:"development",//打包入口 从哪一个文件开始打包entry:path.join(__dirname,"src","index.js"),// 打包好的文件放在哪里output:{//路径path:path.join(__dirname,'dist'),filename:'bundle.js'},}
2-4 配置脚本
# package.json"scripts": {"build": "webpack",},
2-5 执行脚本
npm run build
打包完毕,生成文件
plugins
1 给打包文件一个html文件
src index.html
#index.html<div>webpack 入门</div>
配置
cnpm i html-webpack-plugin -Syarn add html-webpack-plugin//自动生成index.html文件
webpack.config.js
引入const HtmlWebpackPlugin = require("html-webpack-plugin")module.exports ={...plugins:[new HtmlWebpackPlugin({//以哪个文件为模板template:path.join(__dirname,'src','index.html'),//文件名filename:'index.html'})],}
打包完毕 自动引入html

