-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.ts
165 lines (146 loc) Β· 3.57 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
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import type { Configuration } from 'webpack';
import webpack from 'webpack';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import nodeExternals from 'webpack-node-externals';
import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
import WebpackShellPluginNext from 'webpack-shell-plugin-next';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Basic configuration for both assemblies.
const baseConfig: Configuration = {
context: __dirname,
entry: './src/index.ts',
target: 'node',
externalsPresets: { node: true },
externals: [
nodeExternals({
modulesDir: path.join(__dirname, 'node_modules'),
}),
],
resolve: {
extensions: ['.ts'],
plugins: [
// Add support for TypeScript paths.
new TsconfigPathsPlugin({
configFile: './tsconfig.base.json',
}),
],
},
};
// Common JS configuration.
const cjsConfig: Configuration = {
...baseConfig,
output: {
filename: 'index.cjs',
path: path.resolve(__dirname, 'build/common'),
library: { type: 'commonjs' },
clean: true,
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [['@babel/preset-env', { targets: { node: 14 } }], ['@babel/preset-typescript']],
plugins: ['@babel/plugin-transform-modules-commonjs'],
},
},
},
],
},
};
// ESM configuration.
const esmConfig: Configuration = {
...baseConfig,
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'build/esm'),
library: { type: 'module' },
module: true,
clean: true,
},
experiments: {
outputModule: true,
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [['@babel/preset-env', { targets: { node: 14, esmodules: true } }], ['@babel/preset-typescript']],
},
},
},
],
},
};
// Common plugins and optimizations for both configurations.
const pluginsAndOptimization = (isProduction: boolean, isWatchMode: boolean) => ({
plugins: [
// Scripts to run before the build.
new WebpackShellPluginNext({
onBuildStart: {
scripts: [],
blocking: true,
parallel: false,
},
safe: true,
}),
// Type Checking.
new ForkTsCheckerWebpackPlugin({
async: false,
typescript: {
configFile: './tsconfig.build.json',
},
}),
...(isProduction
? [
// Copying static files.
// new CopyPlugin({
// patterns: [{ from: 'public', to: './' }],
// }),
]
: [
// Output of the bundle size report.
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
reportFilename: 'bundle-report.html',
}),
]),
// Output of the build progress.
new webpack.ProgressPlugin(),
],
watch: isWatchMode,
});
/**
* Export both configurations.
*
* @param {Record<string, unknown>} env
* @param {import('webpack').WebpackOptionsNormalized} argv
*
* @returns {import('webpack').Configuration[]} - Configurations for CommonJS and ESM.
*/
export default (env: Record<string, unknown>, argv: { mode: string; watch: boolean }): Configuration[] => {
const isProduction = argv.mode === 'production';
const isWatchMode = argv.watch === true;
// Combining configurations for CommonJS and ESM.
return [
{
...cjsConfig,
...pluginsAndOptimization(isProduction, isWatchMode),
},
{
...esmConfig,
...pluginsAndOptimization(isProduction, isWatchMode),
},
];
};