-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.server.js
157 lines (147 loc) · 3.78 KB
/
config.server.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* eslint-env node */
/* eslint-disable no-console */
const childProcess = require('child_process')
const path = require('path')
const chalk = require('chalk')
const nodeExternals = require('webpack-node-externals')
function setupRules(dirs, verbose) {
return [
{
test: /\.jsx?$/,
include: dirs.src,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
babelrc: false,
sourceType: 'unambiguous',
presets: [
'@babel/preset-react',
[
'@babel/preset-env',
{
targets: { node: true },
modules: false,
debug: verbose,
},
],
],
plugins: [
'@babel/plugin-proposal-export-default-from',
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-proposal-object-rest-spread',
['@babel/plugin-proposal-decorators', { legacy: true }],
'add-react-static-displayname',
['@babel/plugin-proposal-class-properties', { loose: true }],
['@babel/plugin-proposal-private-methods', { loose: true }],
[
'@babel/plugin-proposal-private-property-in-object',
{ loose: true },
],
'@babel/plugin-transform-runtime',
],
},
},
},
{ test: /\.(css|sass)$/, use: 'ignore-loader' },
]
}
function setupPlugins(debug, serverUrl, dirs, inspect, cwd) {
if (!debug) {
return []
}
const time = stats => {
let t = stats.endTime - stats.startTime
let unit = 'ms'
if (t > 60000) {
t /= 60000
unit = 'm'
} else if (t > 1000) {
t /= 1000
unit = 's'
}
return `${chalk.yellow('⚙')} ${chalk.white(t)}${chalk.gray(unit)}`
}
// Server debug
// Start and restart server
console.log(
` ${chalk.magenta('⯂')} Node koaze server: ${chalk.blue(serverUrl.href)}`
)
class ServerDevPlugin {
apply(compiler) {
compiler.hooks.done.tap('WebpackozeaServerDevPlugin', stats => {
if (this.server) {
// eslint-disable-next-line
console.log(
` ${chalk.cyan('↻')} Restarting node server. ${time(stats)}`
)
this.server.kill()
} else {
// eslint-disable-next-line
console.log(
` ${chalk.green('⏻')} Starting node server. ${time(stats)}`
)
}
this.server = childProcess.fork(path.resolve(dirs.dist, 'server.js'), {
cwd,
silent: false,
execArgv: inspect ? ['--inspect'] : [],
})
})
}
}
return [new ServerDevPlugin()]
}
module.exports = function getBaseConfigServer({
cwd,
debug,
dirs,
inspect,
publicPath,
serverUrl,
verbose,
}) {
const main = 'server'
const entry = {}
entry[main] = []
// Main entry point
entry[main].push(path.resolve(dirs.src, main))
// Loading rules
const rules = setupRules(dirs, verbose)
// Plugins
const plugins = setupPlugins(debug, serverUrl, dirs, inspect, cwd)
// We might need to remove [name] here for long time cache
const filename = '[name].js'
const conf = {
mode: debug ? 'development' : 'production',
entry,
output: {
path: dirs.dist,
filename,
chunkFilename: filename,
publicPath,
libraryTarget: 'commonjs2',
},
watch: debug ? true : void 0,
target: 'node',
optimization: void 0,
performance: {
hints: false,
},
resolve: {
extensions: ['.mjs', '.js', '.jsx'],
},
module: { rules },
plugins,
node: {
__dirname: true,
},
externals: [
nodeExternals({
modulesDir: dirs.modules,
allowlist: [/\.css$/],
}),
],
}
return conf
}