forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
309 lines (287 loc) · 8.21 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* eslint-disable @typescript-eslint/no-var-requires */
const webpack = require('webpack');
const packageJSON = require('./package.json');
const path = require('path');
const { merge } = require('webpack-merge');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const ReactRefreshTypeScript = require('react-refresh-typescript');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const isProduction = mode => mode === 'production';
const isDevelopment = mode => !isProduction(mode);
const variants = {
clerk: 'clerk',
clerkBrowser: 'clerk.browser',
clerkHeadless: 'clerk.headless',
clerkHeadlessBrowser: 'clerk.headless.browser',
};
const variantToSourceFile = {
[variants.clerk]: './src/index.ts',
[variants.clerkBrowser]: './src/index.browser.ts',
[variants.clerkHeadless]: './src/index.headless.ts',
[variants.clerkHeadlessBrowser]: './src/index.headless.browser.ts',
};
/** @returns { import('webpack').Configuration } */
const common = ({ mode }) => {
return {
mode,
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx'],
},
plugins: [
new webpack.DefinePlugin({
__DEV__: isDevelopment(mode),
__PKG_VERSION__: JSON.stringify(packageJSON.version),
__PKG_NAME__: JSON.stringify(packageJSON.name),
}),
new webpack.EnvironmentPlugin({
CLERK_ENV: mode,
NODE_ENV: mode,
}),
],
output: {
chunkFilename: `[name]_[fullhash:6]_${packageJSON.version}.js`,
},
optimization: {
splitChunks: {
cacheGroups: {
zxcvbnTSCoreVendor: {
test: /[\\/]node_modules[\\/](@zxcvbn-ts)[\\/](core)[\\/]/,
name: 'zxcvbn-ts-core',
chunks: 'all',
},
zxcvbnTSCommonVendor: {
test: /[\\/]node_modules[\\/](@zxcvbn-ts)[\\/](language-common)[\\/]/,
name: 'zxcvbn-common',
chunks: 'all',
},
common: {
minChunks: 1,
name: 'ui-common',
priority: -20,
test: module => module.resource && !module.resource.includes('/ui/components'),
},
defaultVendors: {
minChunks: 1,
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
priority: -10,
},
},
},
},
};
};
/** @type { () => (import('webpack').RuleSetRule) } */
const svgLoader = () => {
return {
test: /\.svg$/,
resolve: {
fullySpecified: false,
},
use: {
loader: '@svgr/webpack',
options: {
svgo: true,
svgoConfig: {
floatPrecision: 3,
transformPrecision: 1,
plugins: ['preset-default', 'removeDimensions', 'removeStyleElement'],
},
},
},
};
};
/** @type { () => (import('webpack').RuleSetRule) } */
const typescriptLoaderProd = () => {
return {
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
resolve: {
fullySpecified: false,
},
use: [
{
loader: 'ts-loader',
options: { transpileOnly: true },
},
],
};
};
/** @type { () => (import('webpack').RuleSetRule) } */
const typescriptLoaderDev = () => {
return {
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
resolve: {
fullySpecified: false,
},
use: [
{
loader: 'ts-loader',
options: {
configFile: 'tsconfig.dev.json',
transpileOnly: true,
getCustomTransformers: () => ({
before: [ReactRefreshTypeScript()],
}),
},
},
],
};
};
/** @type { () => (import('webpack').Configuration) } */
const commonForProd = () => {
return {
devtool: undefined,
module: {
rules: [svgLoader(), typescriptLoaderProd()],
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
libraryTarget: 'umd',
globalObject: 'globalThis',
},
plugins: [
// new webpack.optimize.LimitChunkCountPlugin({
// maxChunks: 5,
// }),
// new webpack.optimize.MinChunkSizePlugin({
// minChunkSize: 10000,
// })
],
};
};
/** @type { () => (import('webpack').Configuration) } */
const externalsForHeadless = () => {
return {
externals: {
react: 'react',
'react-dom': 'react-dom',
},
};
};
const entryForVariant = variant => {
return { entry: { [variant]: variantToSourceFile[variant] } };
};
/** @type { () => (import('webpack').Configuration)[] } */
const prodConfig = ({ mode, env }) => {
const clerkBrowser = merge(entryForVariant(variants.clerkBrowser), common({ mode }), commonForProd());
const clerkHeadless = merge(
entryForVariant(variants.clerkHeadless),
common({ mode }),
commonForProd(),
// Disable chunking for the headless variant, since it's meant to be used in a non-browser environment and
// attempting to load chunks causes issues due to usage of a dynamic publicPath. We generally are only concerned with
// chunking in our browser bundles.
{
output: {
publicPath: '',
},
optimization: {
splitChunks: false,
},
},
// externalsForHeadless(),
);
const clerkHeadlessBrowser = merge(
entryForVariant(variants.clerkHeadlessBrowser),
common({ mode }),
commonForProd(),
// externalsForHeadless(),
);
const clerkEsm = merge(entryForVariant(variants.clerk), common({ mode }), commonForProd(), {
experiments: {
outputModule: true,
},
output: {
filename: '[name].mjs',
libraryTarget: 'module',
},
plugins: [
// Include the lazy chunks in the bundle as well
// so that the final bundle can be imported and bundled again
// by a different bundler, eg the webpack instance used by react-scripts
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,
}),
],
});
const clerkCjs = merge(clerkEsm, {
output: {
filename: '[name].js',
libraryTarget: 'commonjs',
},
});
return [clerkBrowser, clerkHeadless, clerkHeadlessBrowser, clerkEsm, clerkCjs];
};
const devConfig = ({ mode, env }) => {
const variant = env.variant || variants.clerkBrowser;
// accept an optional devOrigin environment option to change the origin of the dev server.
// By default we use https://js.lclclerk.com which is what our local dev proxy looks for.
const devUrl = new URL(env.devOrigin || 'https://js.lclclerk.com');
const commonForDev = () => {
return {
module: {
rules: [svgLoader(), typescriptLoaderDev()],
},
plugins: [
new ReactRefreshWebpackPlugin({ overlay: { sockHost: devUrl.host } }),
...(env.serveAnalyzer ? [new BundleAnalyzerPlugin()] : []),
],
devtool: 'eval-cheap-source-map',
output: {
publicPath: `${devUrl.origin}/npm`,
crossOriginLoading: 'anonymous',
filename: `${variant}.js`,
libraryTarget: 'umd',
},
optimization: {
minimize: false,
},
devServer: {
allowedHosts: ['all'],
headers: { 'Access-Control-Allow-Origin': '*' },
host: '0.0.0.0',
port: 4000,
hot: true,
liveReload: false,
client: { webSocketURL: `auto://${devUrl.host}/ws` },
},
};
};
const entryToConfigMap = {
// prettier-ignore
[variants.clerk]: merge(
entryForVariant(variants.clerk),
common({ mode }),
commonForDev(),
),
// prettier-ignore
[variants.clerkBrowser]: merge(
entryForVariant(variants.clerkBrowser),
common({ mode }),
commonForDev(),
),
[variants.clerkHeadless]: merge(
entryForVariant(variants.clerkHeadless),
common({ mode }),
commonForDev(),
// externalsForHeadless(),
),
[variants.clerkHeadlessBrowser]: merge(
entryForVariant(variants.clerkHeadlessBrowser),
common({ mode }),
commonForDev(),
// externalsForHeadless(),
),
};
if (!entryToConfigMap[variant]) {
throw new Error('Clerk variant does not exist in config');
}
return entryToConfigMap[variant];
};
module.exports = env => {
const mode = env.production ? 'production' : 'development';
return isProduction(mode) ? prodConfig({ mode, env }) : devConfig({ mode, env });
};