-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
211 lines (191 loc) · 6.31 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
// gets the webpack config for build
// merges the user provided config with the default config
// and returns the merged config
// By default this generates umd builds
import merge from "lodash.mergewith";
import path from "path";
import fs from "fs";
import { BundleAnalyzerPlugin } from "webpack-bundle-analyzer";
import webpack from "webpack";
import { createRequire } from "node:module";
import ESLintPlugin from "eslint-webpack-plugin";
import mergeWith from "lodash.mergewith";
import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";
const require = createRequire(import.meta.url);
import paths, { appModuleFileExtensions } from "./paths.js";
import babelConfig from "./babel.config.js";
import torusConfig from "./torus.config.js";
import { readFile } from "../helpers/utils.js";
import tsconfigBuild from "./tsconfig.build.js";
const { appWebpackConfig, appBuild } = paths;
const { NODE_ENV = "production" } = process.env;
const configImported = (await readFile(appWebpackConfig)).default || { baseConfig: {} };
const babelLoaderOptions = {
...babelConfig,
babelrc: false,
configFile: false,
cacheDirectory: true,
cacheCompression: false,
};
if (fs.existsSync(paths.appBrowserslistConfig)) {
babelLoaderOptions.browserslistConfigFile = paths.appBrowserslistConfig;
} else {
babelLoaderOptions.targets = torusConfig.browserslistrc;
}
export const babelLoader = {
test: /\.(ts|js)x?$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: "babel-loader",
options: babelLoaderOptions,
},
};
const polyfillPlugins = [
new webpack.ProvidePlugin({
Buffer: ["buffer", "Buffer"],
}),
new webpack.ProvidePlugin({
process: "process/browser.js",
}),
];
export const resolveWebpackModule = (localPath) => {
if (fs.existsSync(path.resolve(paths.ownNodeModules, localPath))) return new URL(paths.ownNodeModules + "/" + localPath, import.meta.url).pathname;
else if (fs.existsSync(path.resolve(paths.appNodeModules, localPath)))
return new URL(paths.appNodeModules + "/" + localPath, import.meta.url).pathname;
return require.resolve(localPath);
};
const polyfillFallback = mergeWith(
{
http: resolveWebpackModule("stream-http/index.js"),
https: resolveWebpackModule("https-browserify/index.js"),
os: resolveWebpackModule("os-browserify/browser.js"),
crypto: resolveWebpackModule("crypto-browserify/index.js"),
assert: resolveWebpackModule("assert/build/assert.js"),
stream: resolveWebpackModule("stream-browserify/index.js"),
url: resolveWebpackModule("url/url.js"),
buffer: resolveWebpackModule("buffer/index.js"),
zlib: resolveWebpackModule("browserify-zlib/lib/index.js"),
fs: false,
path: false,
},
torusConfig.polyfillNodeDeps,
(objValue, srcValue) => {
if (srcValue === true) return objValue;
if (typeof srcValue === "string") return srcValue;
return undefined;
},
);
function generateLibraryName(pkgName) {
return pkgName.charAt(0).toUpperCase() + pkgName.slice(1);
}
// objValue is the first object (our default config)
function customizer(objValue, srcValue, key) {
// merge plugins if they are not there
if (Array.isArray(objValue) && (key === "plugins" || key === "rules")) {
return [...objValue, ...(srcValue || [])];
}
}
export default (pkgName) => {
const { baseConfig: userBaseConfig, ...rest } = configImported;
// create a copy of baseConfig every time so that loaders use new instances
const umdConfig = merge(
getDefaultUmdConfig(pkgName),
merge(getDefaultBaseConfig(pkgName), userBaseConfig, customizer),
rest.umdConfig || {},
customizer,
);
const finalConfigs = [];
if (torusConfig.umd) finalConfigs.push(umdConfig);
delete rest.umdConfig;
return [
...finalConfigs,
...Object.values(rest || {}).map((x) => {
const baseConfig = merge(getDefaultBaseConfig(pkgName), userBaseConfig, customizer);
return merge(baseConfig, x, customizer);
}),
];
};
export const getDefaultBaseConfig = () => {
return {
mode: NODE_ENV,
devtool: NODE_ENV === "development" ? "eval" : false,
entry: paths.appIndexFile,
target: "web",
output: {
path: appBuild,
library: {
// Giving a name to builds aggregates all exports under that name
// name: generateLibraryName(pkgName)
},
},
resolve: {
extensions: appModuleFileExtensions.map((x) => `.${x}`),
alias: {
"bn.js": require.resolve("bn.js/lib/bn.js"),
// lodash: require.resolve("lodash/index.js"),
},
},
plugins: [new webpack.IgnorePlugin({ resourceRegExp: /^\.\/wordlists\/(?!english)/, contextRegExp: /bip39\/src$/ })],
module: {
rules: [babelLoader],
},
node: {},
};
};
export const getDefaultUmdConfig = (pkgName) => {
const plugins = [];
if (tsconfigBuild.compilerOptions.plugins && tsconfigBuild.compilerOptions.plugins.length > 0) {
const isTransformPlugin = tsconfigBuild.compilerOptions.plugins.some((x) => x.transform === "typescript-transform-paths");
if (isTransformPlugin) {
plugins.push(
new ForkTsCheckerWebpackPlugin({
typescript: {
mode: "readonly",
context: paths.appPath,
configFile: paths.appTsBuildConfig,
typescriptPath: require.resolve("ts-patch/compiler"),
},
}),
);
}
}
if (NODE_ENV === "production") {
plugins.push(
new ESLintPlugin({
context: paths.appPath,
threads: !process.env.CI,
configType: "flat",
extensions: ["ts", "tsx"],
emitError: true,
emitWarning: true,
failOnError: process.env.NODE_ENV === "production",
cache: true,
cacheLocation: path.resolve(paths.appNodeModules, ".cache/.eslintcache"),
}),
);
}
return {
output: {
filename: `${pkgName}.umd.min.js`,
library: {
type: "umd",
// Giving a name to builds aggregates all exports under that name
name: generateLibraryName(pkgName),
},
},
plugins: [
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,
}),
new BundleAnalyzerPlugin({
analyzerMode: torusConfig.analyzerMode,
openAnalyzer: false,
}),
...polyfillPlugins, // always polyfill buffer and process
...plugins,
],
resolve: {
fallback: polyfillFallback,
},
};
};