1 | const path = require('path')
|
2 | const child_process = require('child_process')
|
3 |
|
4 | const webpack = require('webpack')
|
5 | const ExtractTextPlugin = require('extract-text-webpack-plugin')
|
6 |
|
7 | const { clientConfig, serverConfig, cordovaConfig } = require('./webpack.config')
|
8 | const { ExtractTextLoader, injectWHM } = require('./common.js')
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | const argv = process.argv[2]
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const commonPlugins = [
|
21 | new webpack.HotModuleReplacementPlugin(),
|
22 | new webpack.DefinePlugin({
|
23 | 'process.env.NODE_ENV': '"development"',
|
24 | }),
|
25 | new ExtractTextPlugin({ filename: 'styles.css' }),
|
26 | ]
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | clientConfig.devtool = 'cheap-module-eval-source-map'
|
33 |
|
34 | clientConfig.output.filename = '[name].js'
|
35 | clientConfig.module.rules.push(...ExtractTextLoader)
|
36 |
|
37 | clientConfig.plugins.push(...commonPlugins)
|
38 | clientConfig.performance = { hints: false }
|
39 |
|
40 | const clientCompiler = webpack(clientConfig)
|
41 | injectWHM(clientConfig, clientCompiler)
|
42 |
|
43 | let clientStarted = false
|
44 | if (argv !== 'cordovaOnly') {
|
45 | clientCompiler.watch({}, (err, stats) => {
|
46 | console.log('Client Bundles \n', stats.toString({ chunkModules: false, colors: true }), '\n')
|
47 |
|
48 |
|
49 | if (clientStarted) return
|
50 |
|
51 | createServer()
|
52 | clientStarted = true
|
53 | })
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | serverConfig.devtool = 'cheap-module-eval-source-map'
|
70 |
|
71 |
|
72 | serverConfig.entry.server.push(__dirname + '/signal.js?hmr')
|
73 | serverConfig.module.rules.push(ExtractTextLoader[1])
|
74 | serverConfig.plugins.push(...commonPlugins)
|
75 | serverConfig.performance = { hints: false }
|
76 |
|
77 |
|
78 | function createServer() {
|
79 | let child
|
80 |
|
81 | webpack(serverConfig).watch({}, (err, stats) => {
|
82 | console.log('Server Bundle \n', stats.toString({ colors: true }), '\n')
|
83 | if (stats.hasErrors()) return
|
84 | if (child) {
|
85 | child.send('hmr')
|
86 | return
|
87 | }
|
88 | createChild()
|
89 | })
|
90 |
|
91 | function createChild() {
|
92 | child = child_process.fork(path.join(serverConfig.output.path, serverConfig.output.filename))
|
93 | const start = Date.now()
|
94 | child.on('exit', (code, signal) => {
|
95 | console.error('Child server exited with code:', code, 'and signal:', signal)
|
96 | child = null
|
97 | if (!code) return
|
98 | if (Date.now() - start > 1000) createChild()
|
99 | })
|
100 | }
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | cordovaConfig.devtool = 'cheap-module-eval-source-map'
|
108 | cordovaConfig.module.rules.push(...ExtractTextLoader)
|
109 |
|
110 |
|
111 | cordovaConfig.plugins.push(...commonPlugins)
|
112 |
|
113 | cordovaConfig.plugins = cordovaConfig.plugins.filter(p => !(p instanceof webpack.HotModuleReplacementPlugin))
|
114 | cordovaConfig.performance = { hints: false }
|
115 |
|
116 | if (argv === 'all' || argv === 'cordovaOnly') {
|
117 | webpack(cordovaConfig).watch({}, (err, stats) => {
|
118 | console.log('Cordova Bundles \n', stats.toString({ chunkModules: false, colors: true }), '\n')
|
119 | })
|
120 | }
|