-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ts
167 lines (157 loc) · 4.41 KB
/
webpack.config.ts
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
158
159
160
161
162
163
164
165
166
167
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable import/no-extraneous-dependencies */
import * as path from 'path';
import * as webpack from 'webpack';
// plugins
const nodeExternals = require('webpack-node-externals');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const RunInMemoryPlugin = require('RunInMemoryPlugin');
// logging
const log = require('debug')('app:webpack');
const logClient = require('debug')('app:webpack:client');
const logServer = require('debug')('app:webpack:server');
type Build = {
type: 'server' | 'client',
};
type wepbackEnv = {
production: boolean,
/**
* use to build in memory.
*/
inMem: boolean,
type: Build['type'],
};
function webpackConfig(env: wepbackEnv, argv: Record<string, any>): webpack.Configuration {
const production = env.production || argv.mode === 'production';
const development = !production;
const build: Build = {
type: env.type || 'client',
};
let config: webpack.Configuration = {};
const extensions = ['.js', '.jsx', '.ts', '.tsx', '.json'];
log('build: %o', build);
log('production: %o', production);
// server
if (build.type === 'server') {
/** @type {webpack.Configuration} */
const serverConfig: webpack.Configuration = {
name: 'server',
mode: production ? 'production' : 'development',
target: 'node',
externalsPresets: { node: true },
externals: nodeExternals(),
entry: {
server: [path.resolve(__dirname, './server/index.ts')],
},
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist/'),
libraryTarget: 'commonjs2',
},
module: {
rules: [
{
test: /\.[tj]sx?$/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/env', '@babel/react', '@babel/typescript'],
},
},
// exclude: /[\\/]modules[\\/]/,
},
],
},
plugins: [],
devtool: development ? 'source-map' : 'eval',
resolve: {
extensions,
plugins: [new TsconfigPathsPlugin({ extensions })],
},
};
if (development) {
serverConfig.entry.server.push(path.join(__dirname, './node_modules/webpack/hot/poll?1000'));
serverConfig.plugins.push(
new webpack.HotModuleReplacementPlugin(),
);
// build in memory
if (env.inMem) {
serverConfig.plugins.push(
new RunInMemoryPlugin({
requireFile: path.join(__dirname, './dist/server.bundle.js')
}),
);
}
}
config = serverConfig;
} else if (build.type === 'client') {// client
const nullModule = 'nullModule';
const clientConfig: webpack.Configuration = {
name: 'client',
mode: 'development',
entry: {
app: [path.resolve(__dirname, './src/Client')],
},
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'static/bundle/'),
publicPath: '/bundle/',
},
optimization: {
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
},
},
},
module: {
rules: [
{
test: /\.tsx?$/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/env', '@babel/react', '@babel/typescript'],
},
},
},
],
},
devServer: {
port: 8082,
static: false,
compress: true,
hot: true,
proxy: {
'**': 'http://localhost:8081',
},
historyApiFallback: true,
},
plugins: [],
externals: {},
devtool: 'source-map',
resolve: {
extensions,
plugins: [new TsconfigPathsPlugin({ extensions })],
fallback: {},
},
};
// preRender `.static` files
// replacing `.static` files with `nullModule`
clientConfig.plugins.push(
new webpack.NormalModuleReplacementPlugin(
/\.static(.[jt]sx?)?$/g,
nullModule,
));
// ignore it in browser
clientConfig.resolve.fallback[nullModule] = false;
// TODO execlude preRenderHook file
config = clientConfig;
}
return config;
}
export default webpackConfig;