-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.dev.js
More file actions
146 lines (126 loc) · 4.22 KB
/
webpack.dev.js
File metadata and controls
146 lines (126 loc) · 4.22 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict'
const webpack = require('webpack')
const { merge } = require('webpack-merge')
const CopyPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
const portfinder = require('portfinder')
const path = require('path')
const ESLintPlugin = require('eslint-webpack-plugin');
const { VueLoaderPlugin } = require('vue-loader')
const utils = require('./utils/index')
const config = require('./config')
const baseWebpackConfig = require('./webpack.base')
const HOST = process.env.HOST
const PORT = process.env.PORT && Number(process.env.PORT)
const devWebpackConfig = merge(baseWebpackConfig, {
mode: 'development',
optimization: {
// namedModules: true
},
module: {
rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
},
// cheap-module-eval-source-map is faster for development
devtool: config.dev.devtool,
// these devServer options should be customized in /config/index.js
// devServer: {
// clientLogLevel: 'silent',
// // 告诉服务器内容的来源
// contentBase: './dist',
// // 为每个静态文件开启 gzip compression
// compress: true,
// port: 9000,
// // 所有中间件执行之前的自定义执行函数
// before: function (app, server, compiler) {
// app.get('/some/path', function (req, res) {
// res.json({ custom: 'response' });
// });
// },
// // 提供自定义中间件,当 devServer 服务器内部的 所有中间件执行完成之后执行
// after: function (app, server, compiler) {
// // do fancy stuff
// },
// // 允许将允许访问开发服务器的服务列入白名单
// allowedHosts: [
// '127.0.0.1',
// 'localhost',
// 'subdomain2.host.com',
// 'host2.com',
// ],
// // 用于在启动时通过 ZeroConf 网络广播你的开发服务器,用于服务发现
// bonjour: false,
// // 以减少在 lazy 模式中的编译操作。 默认情况下,在 lazy 模式中,每个请求都触发新的编译
// // 使用 filename 仅当请求某个文件时才可执行编译
// lazy: true,
// filename: '[name].js',
// headers: {
// 'X-Custom-Foo': 'bar',
// },
// /// 当使用 HTML5 History API 时, 所有的 404 请求都会响应 index.html 的内容
// historyApiFallback: {
// rewrites: [
// { from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') },
// ],
// disableDotRule: true,
// },
// host: '0.0.0.0',
// hot: true,
// index: 'index.html',
// },
devServer: {
contentBase: './dist',
},
plugins: [
new VueLoaderPlugin(),
new webpack.DefinePlugin({
'process.env': require('./config/dev.env')
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
// https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
// filename: 'index.html',
// template: 'index.html',
title: 'Development',
inject: true
}),
// copy custom static assets
// https://www.npmjs.com/package/copy-webpack-plugin
new CopyPlugin({
patterns: [
{
from: path.resolve(__dirname, '../static'),
to: config.dev.assetsSubDirectory
},
],
options: {
concurrency: 100,
},
}),
new ESLintPlugin({}),
]
})
module.exports = new Promise((resolve, reject) => {
portfinder.basePort = process.env.PORT || config.dev.port
portfinder.getPort((err, port) => {
if (err) {
reject(err)
} else {
// publish the new Port, necessary for e2e tests
process.env.PORT = port
// add port to devServer config
// devWebpackConfig.devServer.port = port
// Add FriendlyErrorsPlugin
devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
compilationSuccessInfo: {
messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`],
},
onErrors: config.dev.notifyOnErrors
? utils.createNotifierCallback()
: undefined
}))
resolve(devWebpackConfig)
}
})
})