-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
webpack.config.ts
132 lines (125 loc) · 3.55 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
import { resolve } from 'path';
import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import { RunScriptWebpackPlugin } from 'run-script-webpack-plugin';
import * as webpack from 'webpack';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import nodeExternals from 'webpack-node-externals';
const rootDir = process.cwd();
const appsDir = resolve(rootDir, 'apps');
const clientDirectory = resolve(appsDir, 'client');
const httpDirectory = resolve(appsDir, 'server');
const buildDirectory = resolve(rootDir, 'build');
const isProduction = process.env.NODE_ENV === 'production';
const isDevelopment = !isProduction;
const isAnalyzing = process.env.ANALYZE === 'true';
const mode = isProduction ? 'production' : 'development';
export const getBackendConfig = (): webpack.Configuration => ({
cache: {
type: 'filesystem',
allowCollectingMemory: true,
},
context: httpDirectory,
entry: {
server: [isDevelopment && 'webpack/hot/poll?100', './src/main.ts'].filter(
Boolean,
),
},
externals: [nodeExternals({ allowlist: ['webpack/hot/poll?100'] })],
mode,
module: {
rules: [
{
test: /.*((?!spec).).tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
output: {
path: buildDirectory,
},
plugins: [
isDevelopment && new webpack.HotModuleReplacementPlugin(),
isDevelopment &&
new webpack.WatchIgnorePlugin({ paths: [/\.js$/, /\.d\.ts$/] }),
isDevelopment &&
new RunScriptWebpackPlugin({
name: 'server.js',
keyboard: true,
nodeArgs: ['--unhandled-rejections=strict'],
}),
].filter(Boolean),
resolve: {
extensions: ['.js', '.json', '.ts'],
},
target: 'node',
});
export const getClientConfig = (): webpack.Configuration & {
devServer: any;
} => ({
cache: {
type: 'filesystem',
allowCollectingMemory: true,
},
context: clientDirectory,
devServer: {
open: isDevelopment,
hot: isDevelopment,
},
devtool: isProduction ? 'source-map' : 'eval-source-map',
entry: [
isDevelopment && 'react-refresh/runtime',
isDevelopment && 'webpack-hot-middleware/client',
'./src',
].filter(Boolean),
mode,
module: {
rules: [
{
test: /.*((?!spec).).tsx?$/,
use: 'babel-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader', 'postcss-loader'],
},
],
},
output: {
path: buildDirectory,
filename: 'app.[fullhash].js',
},
plugins: [
isAnalyzing && new BundleAnalyzerPlugin(),
isDevelopment && new webpack.HotModuleReplacementPlugin(),
isDevelopment &&
new ReactRefreshWebpackPlugin({ overlay: { sockIntegration: 'whm' } }),
isDevelopment &&
new webpack.WatchIgnorePlugin({ paths: [/\.js$/, /\.d\.ts$/] }),
// new CleanWebpackPlugin({ cleanStaleWebpackAssets: true }),
new HtmlWebpackPlugin({
inject: false,
title: 'RethinkDB Administration Console',
template: 'templates/index.ejs',
}),
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
}),
new CopyWebpackPlugin({ patterns: [{ from: 'static' }] }),
].filter(Boolean),
resolve: {
extensions: ['.js', '.json', '.ts', '.tsx'],
fallback: { crypto: false },
},
});
function setupConfig() {
const configs = [getBackendConfig()];
if (isProduction) {
configs.push(getClientConfig());
}
return configs;
}
export default setupConfig;