undefined

webpack之devserver介绍

dev-server

安装开发服务器的库

1
npm i -D webpack-dev-server
在packag.json添加运行命令start

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
"name": "2",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack --config webpack.config.dev.js",
"start": "webpack-dev-server --config webpack.config.dev.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-react": "^6.24.1",
"html-webpack-plugin": "^2.30.1",
"webpack": "^3.5.5",
"webpack-dev-server": "^2.8.2"
},
"dependencies": {
"react": "^15.6.1",
"react-dom": "^15.6.1"
}
}

运行,开启服务器,开启服务器的时候,首先找到配置文件,先帮我们打包,打包完成之后,再让我们去访问已经打包好的资源。所以后面要加–config webpack.config.dev.js找到配置文件

1
npm run start

注意:这里dev-server打包后的资源并不是放到硬盘里面,而是打包后放到内存里面,所以文件夹下面并没有生成新的打包资源文件。

定制化配置

webpack.config.dev.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');

module.exports = {
entry: './src/app.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'app.js',
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'src/index.html'
})
],
module: {
rules: [{
test: /\.js$/,
use: [{
loader: 'babel-loader',
options: {
presets: ['react']
}
}]
}]
},
devServer: {
open: true,
port: 9000
}
};
觉得本站不错,请作者吃根辣条