UNPKG

4.02 kBJavaScriptView Raw
1const path = require('path')
2const child_process = require('child_process')
3
4const webpack = require('webpack')
5const ExtractTextPlugin = require('extract-text-webpack-plugin')
6
7const { clientConfig, serverConfig, cordovaConfig } = require('./webpack.config')
8const { ExtractTextLoader, injectWHM } = require('./common.js')
9
10// get the last argument
11// possible values:
12// null - run client (browser) and server
13// all - run client (brower), server and cordova
14// cordovaOnly - run only cordova
15const argv = process.argv[2]
16
17// use css module when file name is xxx.module.css
18// dont mix global and local css, e.g. in global.css don't @import local.css or vice versa
19
20const commonPlugins = [
21 new webpack.HotModuleReplacementPlugin(),
22 new webpack.DefinePlugin({
23 'process.env.NODE_ENV': '"development"',
24 }),
25 new ExtractTextPlugin({ filename: 'styles.css' }), // has to use this for universal server client rendering
26]
27
28/**
29 * Client
30 */
31
32clientConfig.devtool = 'cheap-module-eval-source-map'
33// add hot middleware on port 8080
34clientConfig.output.filename = '[name].js'
35clientConfig.module.rules.push(...ExtractTextLoader)
36// clientConfig.module.noParse = /someModuleDist|anotherModuleDist/
37clientConfig.plugins.push(...commonPlugins)
38clientConfig.performance = { hints: false }
39
40const clientCompiler = webpack(clientConfig)
41injectWHM(clientConfig, clientCompiler)
42
43let clientStarted = false
44if (argv !== 'cordovaOnly') {
45 clientCompiler.watch({}, (err, stats) => {
46 console.log('Client Bundles \n', stats.toString({ chunkModules: false, colors: true }), '\n')
47 // console.log('Client Bundles \n',stats.toString({colors:true}),'\n')
48
49 if (clientStarted) return
50 // the build/webpack-assets.json is ready, so go on create the server bundle
51 createServer()
52 clientStarted = true
53 })
54}
55
56
57// // use inbuilt http module
58// if (argv !== 'cordovaOnly') {
59// http.createServer((req, res) => {
60// res.setHeader('Access-Control-Allow-Origin', '*')
61// webpackHotMiddleware(clientCompiler, { log: false })(req, res)
62// }).listen(8080)
63// }
64
65
66/**
67 * Server
68 */
69serverConfig.devtool = 'cheap-module-eval-source-map'
70
71// allow hot module on server side, hmr is the signal to reload
72serverConfig.entry.server.push(__dirname + '/signal.js?hmr') // eslint-disable-line
73serverConfig.module.rules.push(ExtractTextLoader[1]) // to handle css module
74serverConfig.plugins.push(...commonPlugins)
75serverConfig.performance = { hints: false }
76
77
78function 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() // arbitrarily only after the server has started for more than 1 sec
99 })
100 }
101}
102
103/**
104 * Cordova
105 */
106
107cordovaConfig.devtool = 'cheap-module-eval-source-map'
108cordovaConfig.module.rules.push(...ExtractTextLoader)
109// cordovaConfig.module.noParse = /^[@a-z][a-z\/\.\-0-9]*$/i
110
111cordovaConfig.plugins.push(...commonPlugins)
112// remove the new webpack.HotModuleReplacementPlugin(),
113cordovaConfig.plugins = cordovaConfig.plugins.filter(p => !(p instanceof webpack.HotModuleReplacementPlugin))
114cordovaConfig.performance = { hints: false }
115
116if (argv === 'all' || argv === 'cordovaOnly') {
117 webpack(cordovaConfig).watch({}, (err, stats) => { // eslint-disable-line no-unused-expressions
118 console.log('Cordova Bundles \n', stats.toString({ chunkModules: false, colors: true }), '\n')
119 })
120}