项目结构
webpackDevServer
│ package-lock.json
│ package.json
│ postcss.config.js
│ webpack.config.js
└─src
index.css
index.js
index.css
安装依赖
在webpackDevServer目录下运行
npm i webpack webpack-cli html-webpack-plugin css-loader style-loader webpack-dev-server --save-dev
webpack配置文件
const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exprots = {
mode:'development',
entry:'./src/index.js',
output:{
path:resolve(__dirname,'dist'),
filename:'[name].build.js'
},
module:{
rules:[
{
// 匹配文件规则
test:/\.css$/i,
// 使用的loader
use:['style-loader','css-loader']
}
]
},
devServer:{
static:{
directory:resolve(__dirname,'dist'),
},
liveReload:true,
compress:true,
port:8080,
},
plugins:[
new HtmlWebpackPlugin()
]
}
本地启动命令
- 方式一:在webpackDevServer目录下运行
webpack-dev-server --config ./webapck.config.js
- 方式二:在package.json文件的scripts属性新增如下命令
{
...
"scripts":{
"dev":"webpack-dev-server --config ./webpack.config.js"
}
...
}
修改保存后,在webpackDevServer目录下运行如下命令:
npm run dev