/
webpack.config.js
78 lines (72 loc) · 2.04 KB
/
webpack.config.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
const path = require('path');
const { WebpackPluginServe: Serve } = require('webpack-plugin-serve');
const importFresh = require('import-fresh');
const nodeExternals = require('webpack-node-externals');
const SRC_DIR_CLIENT = path.resolve(__dirname, 'client');
const SRC_DIR_SERVER = path.resolve(__dirname, 'server');
const DIST_DIR = path.resolve(__dirname, 'dist');
const serve = new Serve({
port: 3000,
static: [DIST_DIR],
waitForBuild: true,
middleware(app) {
app.use(async (ctx, next) => {
const renderer = importFresh(path.resolve(DIST_DIR, 'server.js'));
await renderer(ctx, next);
});
}
});
function createConfig(opts) {
const { name } = opts;
const isServer = name === 'server';
const optimize = process.env.NODE_ENV === 'production';
return {
name,
mode: optimize ? 'production' : 'development',
entry: isServer
? { server: './server/main.js' }
: { client: ['./client/index.js', ...(optimize ? [] : ['webpack-plugin-serve/client'])] },
output: {
path: DIST_DIR,
filename: '[name].js',
libraryTarget: isServer ? 'commonjs2' : 'var'
},
module: {
rules: [
{
test: /\.js$/,
include: [SRC_DIR_SERVER, SRC_DIR_CLIENT],
use: [
{
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
modules: false,
targets: isServer ? { node: 'current' } : '> 0.5%, last 2 versions, not dead'
}
],
['@babel/preset-react', { development: !optimize }]
]
}
}
]
}
]
},
plugins: [isServer ? serve.attach() : serve],
target: isServer ? 'node' : 'web',
...(isServer ? { externals: nodeExternals() } : {}),
watch: !isServer && !optimize
};
}
module.exports = [
createConfig({
name: 'client'
}),
createConfig({
name: 'server'
})
];