-
Notifications
You must be signed in to change notification settings - Fork 21
/
webpack.dev.js
141 lines (123 loc) · 3.76 KB
/
webpack.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
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
/* eslint no-sync: "error" */
'use strict'
// libraries
const path = require('path')
const humanizeDuration = require('humanize-duration')
const fs = require('fs')
// Webpack plugins
const webpack = require('webpack')
const serve = require('webpack-serve');
const merge = require('webpack-merge')
// Webpack configs
const common = require('./webpack/webpack.config.js')
// Additional imports
const packageJson = require('../package')
const devSettings = require('../app-dev')
const cli = require('./utils/cli')
// define your stuff
const baseDir = path.join(__dirname, '../')
const buildDir = path.join(baseDir, 'build')
// const apiDir = path.join(process.cwd(), 'build', 'api')
const sslKey = path.join(__dirname, 'ssl', 'localhost.key')
const sslCrt = path.join(__dirname, 'ssl', 'localhost.crt')
const defaultPort = 9000
// define your stuff
const PLUGINS = []
const configs = merge.multiple(common, {
app: {
mode: 'development',
devtool: 'inline-source-map',
plugins: PLUGINS,
},
staticAssets: {},
vendors: {},
pwa: {},
})
/* eslint no-sync: ["error", { allowAtRootLevel: true }]*/
serve({}, {
compiler: webpack(configs),
content: buildDir,
clipboard: true,
devMiddleware: devSettings.webpackDevMiddleware,
host: devSettings.host || "localhost",
hotClient: devSettings.webpackHotClient || {},
http2: devSettings.http2 || true,
https: {
key: fs.readFileSync(sslKey), // Private keys in PEM format.
cert: fs.readFileSync(sslCrt), // Cert chains in PEM format.
// pfx: PFX or PKCS12 encoded private key and certificate chain.
// passphrase: A shared passphrase used for a single private key and/or a PFX.
},
logLevel: devSettings.logLevel || 'info',
logTime: devSettings.logTime || false,
open: devSettings.open || false,
port: devSettings.port || defaultPort
}).then((server) => {
server.on('listening', ({
server,
options
}) => {
cli.debug(`server session ctx: ${server.sessionIdContext}`)
cli.banner(packageJson.name, packageJson.version, options.host, options.port, options.hot.https)
})
/**
* Emitted when a compiler has started a build.
*
* @type Compiler Instance
*/
server.on('build-started', (compiler) => {
cli.info(`building ${compiler.compiler.options.name} - mode: ${compiler.compiler.options.mode} - target: ${compiler.compiler.options.target}`)
})
/**
* Emitted when a compiler has finished a build.
*
* @type Stats Object
* @type Compiler Instance
*/
server.on('build-finished', (stats) => {
if (stats.stats.compilation.errors.length > 0) {
stats.stats.compilation.errors.forEach((e) => {
cli.error(e)
})
}
if (stats.stats.compilation.warnings.length > 0) {
stats.stats.compilation.warnings.forEach((e) => {
cli.warn(e)
})
}
const files = Object.keys(stats.stats.compilation.assets)
if (files.length > 0 && devSettings.logLevel === 'debug') {
files.forEach((file) => {
cli.debug(file)
})
}
const execTime = `${stats.stats.compilation.name} compilation finished in ${humanizeDuration(stats.stats.endTime - stats.stats.startTime)}`
cli.ok(execTime)
})
/**
* Emitted when a compiler has encountered and error, or a build has errors.
*
* @type Stats Object
* @type Compiler Instance
*/
server.on('compiler-error', (stats) => {
if (stats.json.errors.length > 0) {
stats.json.errors.forEach((e) => {
cli.error(e)
})
}
})
/**
* Emitted when a compiler has encountered a warning, or a build has warnings.
*
* @type Stats Object
* @type Compiler Instance
*/
server.on('compiler-warning', (stats) => {
if (stats.json.warnings.length > 0) {
stats.json.warnings.forEach((w) => {
cli.warn(w)
})
}
})
});