UNPKG

2 kBJavaScriptView Raw
1const webpack = require('webpack')
2const ExtractTextPlugin = require('extract-text-webpack-plugin')
3
4const { cssLoader, injectWHM } = require('./common')
5
6const { clientConfig, serverConfig, } = require('./webpack.config.test')
7const clientConfigNode = require('lodash/cloneDeep')(clientConfig)
8
9const commonPlugins = [
10 new webpack.DefinePlugin({
11 'process.env.TEST': true,
12 'process.env.NODE_ENV': '"development"',
13 }),
14 new ExtractTextPlugin({ filename: 'styles.css', allChunks: false }), // has to use this for universal server client rendering
15]
16
17
18// Client run in browser
19clientConfig.devtool = 'cheap-module-eval-source-map' // eslint-disable-line no-param-reassign
20clientConfig.module.rules.push(...cssLoader)
21clientConfig.plugins.push(new webpack.HotModuleReplacementPlugin())
22clientConfig.plugins.push(...commonPlugins)
23
24const clientCompiler = webpack(clientConfig)
25injectWHM(clientConfig, clientCompiler, 9080)
26clientCompiler.watch({}, (err, stats) => {
27 console.log('Client Bundles in for browser \n', stats.toString({ chunkModules: false, colors: true }), '\n')
28})
29
30// Client run in node
31clientConfigNode.entry.client = ['./src/client/entry.node.test.js']
32clientConfigNode.output.filename = './src/client/client.node.js'
33compileAndTest(clientConfigNode, 'CLIENT')
34
35// Server
36compileAndTest(serverConfig, 'SERVER')
37
38/**
39 * @arg {Object} config
40 * @arg {string} arch - 'CLIENT' | 'SERVER'
41 */
42
43function compileAndTest(config, arch) {
44 config.devtool = 'cheap-module-eval-source-map' // eslint-disable-line no-param-reassign
45 config.module.rules.push(...cssLoader)
46 config.plugins.push(...commonPlugins)
47
48 let child
49 webpack(config).watch({}, (err, stats) => {
50 if (stats.hasErrors()) {
51 console.log(arch + '\n', stats.toString({ colors: true }))
52 return
53 }
54 if (child) child.kill()
55 child = require('child_process').fork(require('path').join(config.output.path, config.output.filename))
56 })
57}