@utoo/pack-shared 0.0.7 → 1.1.8-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/issue.js CHANGED
@@ -1,13 +1,9 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatIssue = formatIssue;
4
- exports.handleIssues = handleIssues;
5
- const code_frame_1 = require("@babel/code-frame");
6
- const styledString_1 = require("./styledString");
7
- function formatIssue(issue, forceColor = true) {
1
+ import { codeFrameColumns } from "@babel/code-frame";
2
+ import { renderStyledStringToErrorAnsi } from "./styledString";
3
+ export function formatIssue(issue, forceColor = true) {
8
4
  const { filePath, title, description, source } = issue;
9
5
  let { documentationLink } = issue;
10
- let formattedTitle = (0, styledString_1.renderStyledStringToErrorAnsi)(title).replace(/\n/g, "\n ");
6
+ let formattedTitle = renderStyledStringToErrorAnsi(title).replace(/\n/g, "\n ");
11
7
  let formattedFilePath = filePath
12
8
  .replace("[project]/", "./")
13
9
  .replaceAll("/./", "/")
@@ -27,7 +23,7 @@ function formatIssue(issue, forceColor = true) {
27
23
  if ((source === null || source === void 0 ? void 0 : source.range) && source.source.content) {
28
24
  const { start, end } = source.range;
29
25
  message +=
30
- (0, code_frame_1.codeFrameColumns)(source.source.content, {
26
+ codeFrameColumns(source.source.content, {
31
27
  start: {
32
28
  line: start.line + 1,
33
29
  column: start.column + 1,
@@ -41,7 +37,7 @@ function formatIssue(issue, forceColor = true) {
41
37
  { forceColor }).trim() + "\n\n";
42
38
  }
43
39
  if (description) {
44
- message += (0, styledString_1.renderStyledStringToErrorAnsi)(description) + "\n\n";
40
+ message += renderStyledStringToErrorAnsi(description) + "\n\n";
45
41
  }
46
42
  // TODO: make it possible to enable this for debugging, but not in tests.
47
43
  // if (detail) {
@@ -53,7 +49,7 @@ function formatIssue(issue, forceColor = true) {
53
49
  }
54
50
  return message;
55
51
  }
56
- function handleIssues(issues, throwErrors = true, forceColor = true) {
52
+ export function handleIssues(issues, throwErrors = true, forceColor = true) {
57
53
  const topLevelErrors = new Set();
58
54
  const topLevelWarnings = new Set();
59
55
  for (const issue of issues) {
@@ -85,7 +81,7 @@ function isNodeModulesIssue(issue) {
85
81
  if (issue.severity === "warning" && issue.stage === "config") {
86
82
  // Override for the externalize issue
87
83
  // `Package foo (serverExternalPackages or default list) can't be external`
88
- if ((0, styledString_1.renderStyledStringToErrorAnsi)(issue.title).includes("can't be external")) {
84
+ if (renderStyledStringToErrorAnsi(issue.title).includes("can't be external")) {
89
85
  return false;
90
86
  }
91
87
  }
@@ -1,7 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MAGIC_IDENTIFIER_REGEX = void 0;
4
- exports.decodeMagicIdentifier = decodeMagicIdentifier;
5
1
  function decodeHex(hexStr) {
6
2
  if (hexStr.trim() === "") {
7
3
  throw new Error("can't decode empty hex");
@@ -13,7 +9,7 @@ function decodeHex(hexStr) {
13
9
  return String.fromCodePoint(num);
14
10
  }
15
11
  const DECODE_REGEX = /^__TURBOPACK__([a-zA-Z0-9_$]+)__$/;
16
- function decodeMagicIdentifier(identifier) {
12
+ export function decodeMagicIdentifier(identifier) {
17
13
  const matches = identifier.match(DECODE_REGEX);
18
14
  if (!matches) {
19
15
  return identifier;
@@ -86,4 +82,4 @@ function decodeMagicIdentifier(identifier) {
86
82
  }
87
83
  return output;
88
84
  }
89
- exports.MAGIC_IDENTIFIER_REGEX = /__TURBOPACK__[a-zA-Z0-9_$]+__/g;
85
+ export const MAGIC_IDENTIFIER_REGEX = /__TURBOPACK__[a-zA-Z0-9_$]+__/g;
@@ -1,16 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.renderStyledStringToErrorAnsi = renderStyledStringToErrorAnsi;
4
- const picocolors_1 = require("picocolors");
5
- const magicIdentifier_1 = require("./magicIdentifier");
6
- function renderStyledStringToErrorAnsi(string) {
1
+ import { bold, green, magenta, red } from "picocolors";
2
+ import { decodeMagicIdentifier, MAGIC_IDENTIFIER_REGEX, } from "./magicIdentifier";
3
+ export function renderStyledStringToErrorAnsi(string) {
7
4
  function decodeMagicIdentifiers(str) {
8
- return str.replaceAll(magicIdentifier_1.MAGIC_IDENTIFIER_REGEX, (ident) => {
5
+ return str.replaceAll(MAGIC_IDENTIFIER_REGEX, (ident) => {
9
6
  try {
10
- return (0, picocolors_1.magenta)(`{${(0, magicIdentifier_1.decodeMagicIdentifier)(ident)}}`);
7
+ return magenta(`{${decodeMagicIdentifier(ident)}}`);
11
8
  }
12
9
  catch (e) {
13
- return (0, picocolors_1.magenta)(`{${ident} (decoding failed: ${e})}`);
10
+ return magenta(`{${ident} (decoding failed: ${e})}`);
14
11
  }
15
12
  });
16
13
  }
@@ -18,9 +15,9 @@ function renderStyledStringToErrorAnsi(string) {
18
15
  case "text":
19
16
  return decodeMagicIdentifiers(string.value);
20
17
  case "strong":
21
- return (0, picocolors_1.bold)((0, picocolors_1.red)(decodeMagicIdentifiers(string.value)));
18
+ return bold(red(decodeMagicIdentifiers(string.value)));
22
19
  case "code":
23
- return (0, picocolors_1.green)(decodeMagicIdentifiers(string.value));
20
+ return green(decodeMagicIdentifiers(string.value));
24
21
  case "line":
25
22
  return string.value.map(renderStyledStringToErrorAnsi).join("");
26
23
  case "stack":
package/esm/utils.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { ConfigComplete, DefineEnv, RustifiedEnv } from "./config";
2
+ import { Issue } from "./issue";
3
+ export declare class ModuleBuildError extends Error {
4
+ name: string;
5
+ }
6
+ export interface ResultWithIssues {
7
+ issues: Issue[];
8
+ }
9
+ export declare function processIssues(result: ResultWithIssues, throwIssue: boolean, logErrors: boolean): void;
10
+ export declare function isWellKnownError(issue: Issue): boolean;
11
+ export declare function rustifyEnv(env: Record<string, string>): RustifiedEnv;
12
+ interface DefineEnvOptions {
13
+ config: ConfigComplete;
14
+ dev: boolean;
15
+ optionDefineEnv?: DefineEnv;
16
+ }
17
+ export declare function createDefineEnv(options: DefineEnvOptions): DefineEnv;
18
+ type AnyFunc<T> = (this: T, ...args: any) => any;
19
+ export declare function debounce<T, F extends AnyFunc<T>>(fn: F, ms: number, maxWait?: number): (this: T, ...passedArgs: Parameters<F>) => void;
20
+ export {};
package/esm/utils.js ADDED
@@ -0,0 +1,125 @@
1
+ import { formatIssue } from "./issue";
2
+ import { renderStyledStringToErrorAnsi } from "./styledString";
3
+ export class ModuleBuildError extends Error {
4
+ constructor() {
5
+ super(...arguments);
6
+ this.name = "ModuleBuildError";
7
+ }
8
+ }
9
+ export function processIssues(result, throwIssue, logErrors) {
10
+ const relevantIssues = new Set();
11
+ for (const issue of result.issues) {
12
+ if (issue.severity !== "error" &&
13
+ issue.severity !== "fatal" &&
14
+ issue.severity !== "warning")
15
+ continue;
16
+ if (issue.severity !== "warning") {
17
+ if (throwIssue) {
18
+ const formatted = formatIssue(issue);
19
+ relevantIssues.add(formatted);
20
+ }
21
+ // if we throw the issue it will most likely get handed and logged elsewhere
22
+ else if (logErrors && isWellKnownError(issue)) {
23
+ const formatted = formatIssue(issue);
24
+ console.error(formatted);
25
+ }
26
+ }
27
+ }
28
+ if (relevantIssues.size && throwIssue) {
29
+ throw new ModuleBuildError([...relevantIssues].join("\n\n"));
30
+ }
31
+ }
32
+ export function isWellKnownError(issue) {
33
+ const { title } = issue;
34
+ const formattedTitle = renderStyledStringToErrorAnsi(title);
35
+ // TODO: add more well known errors
36
+ if (formattedTitle.includes("Module not found") ||
37
+ formattedTitle.includes("Unknown module type")) {
38
+ return true;
39
+ }
40
+ return false;
41
+ }
42
+ export function rustifyEnv(env) {
43
+ return Object.entries(env)
44
+ .filter(([_, value]) => value != null)
45
+ .map(([name, value]) => ({
46
+ name,
47
+ value,
48
+ }));
49
+ }
50
+ export function createDefineEnv(options) {
51
+ var _a;
52
+ let defineEnv = (_a = options.optionDefineEnv) !== null && _a !== void 0 ? _a : {
53
+ client: [],
54
+ edge: [],
55
+ nodejs: [],
56
+ };
57
+ function getDefineEnv() {
58
+ var _a;
59
+ const envs = {
60
+ "process.env.NODE_ENV": options.dev ? "development" : "production",
61
+ };
62
+ const userDefines = (_a = options.config.define) !== null && _a !== void 0 ? _a : {};
63
+ for (const key in userDefines) {
64
+ envs[key] = userDefines[key];
65
+ }
66
+ // serialize
67
+ const defineEnvStringified = {};
68
+ for (const key in defineEnv) {
69
+ const value = envs[key];
70
+ defineEnvStringified[key] = JSON.stringify(value);
71
+ }
72
+ return defineEnvStringified;
73
+ }
74
+ // TODO: future define envs need to extends for more compiler like server or edge.
75
+ for (const variant of Object.keys(defineEnv)) {
76
+ defineEnv[variant] = rustifyEnv(getDefineEnv());
77
+ }
78
+ return defineEnv;
79
+ }
80
+ export function debounce(fn, ms, maxWait = Infinity) {
81
+ let timeoutId;
82
+ // The time the debouncing function was first called during this debounce queue.
83
+ let startTime = 0;
84
+ // The time the debouncing function was last called.
85
+ let lastCall = 0;
86
+ // The arguments and this context of the last call to the debouncing function.
87
+ let args, context;
88
+ // A helper used to that either invokes the debounced function, or
89
+ // reschedules the timer if a more recent call was made.
90
+ function run() {
91
+ const now = Date.now();
92
+ const diff = lastCall + ms - now;
93
+ // If the diff is non-positive, then we've waited at least `ms`
94
+ // milliseconds since the last call. Or if we've waited for longer than the
95
+ // max wait time, we must call the debounced function.
96
+ if (diff <= 0 || startTime + maxWait >= now) {
97
+ // It's important to clear the timeout id before invoking the debounced
98
+ // function, in case the function calls the debouncing function again.
99
+ timeoutId = undefined;
100
+ fn.apply(context, args);
101
+ }
102
+ else {
103
+ // Else, a new call was made after the original timer was scheduled. We
104
+ // didn't clear the timeout (doing so is very slow), so now we need to
105
+ // reschedule the timer for the time difference.
106
+ timeoutId = setTimeout(run, diff);
107
+ }
108
+ }
109
+ return function (...passedArgs) {
110
+ // The arguments and this context of the most recent call are saved so the
111
+ // debounced function can be invoked with them later.
112
+ args = passedArgs;
113
+ context = this;
114
+ // Instead of constantly clearing and scheduling a timer, we record the
115
+ // time of the last call. If a second call comes in before the timer fires,
116
+ // then we'll reschedule in the run function. Doing this is considerably
117
+ // faster.
118
+ lastCall = Date.now();
119
+ // Only schedule a new timer if we're not currently waiting.
120
+ if (timeoutId === undefined) {
121
+ startTime = lastCall;
122
+ timeoutId = setTimeout(run, ms);
123
+ }
124
+ };
125
+ }
@@ -0,0 +1,6 @@
1
+ import type webpack from "webpack";
2
+ import { BundleOptions } from "./config";
3
+ export type WebpackConfig = Partial<Pick<webpack.Configuration, "name" | "entry" | "mode" | "module" | "resolve" | "externals" | "output" | "target" | "devtool" | "optimization" | "plugins" | "stats">> & {
4
+ webpackMode: true;
5
+ };
6
+ export declare function compatOptionsFromWebpack(webpackConfig: WebpackConfig): BundleOptions;
@@ -0,0 +1,410 @@
1
+ export function compatOptionsFromWebpack(webpackConfig) {
2
+ const { entry, mode, module, resolve, externals, output, target, devtool, optimization, plugins, stats, } = webpackConfig;
3
+ return {
4
+ config: {
5
+ entry: compatEntry(entry),
6
+ mode: compatMode(mode),
7
+ module: compatModule(module),
8
+ resolve: compatResolve(resolve),
9
+ externals: compatExternals(externals),
10
+ output: compatOutput(output),
11
+ target: compatTarget(target),
12
+ sourceMaps: compatSourceMaps(devtool),
13
+ optimization: compatOptimization(optimization),
14
+ define: compatFromWebpackPlugin(plugins, compatDefine),
15
+ provider: compatFromWebpackPlugin(plugins, compatProvider),
16
+ stats: compatStats(stats),
17
+ html: compatFromWebpackPlugins(plugins, compatHtml),
18
+ },
19
+ buildId: webpackConfig.name,
20
+ };
21
+ }
22
+ function compatMode(webpackMode) {
23
+ return webpackMode
24
+ ? webpackMode === "none"
25
+ ? "production"
26
+ : webpackMode
27
+ : "production";
28
+ }
29
+ function compatEntry(webpackEntry) {
30
+ const entry = [];
31
+ switch (typeof webpackEntry) {
32
+ case "string":
33
+ entry.push({ import: webpackEntry });
34
+ break;
35
+ case "object":
36
+ if (Array.isArray(webpackEntry)) {
37
+ webpackEntry.forEach((e) => entry.push({
38
+ import: e,
39
+ }));
40
+ }
41
+ else {
42
+ Object.entries(webpackEntry).forEach(([k, v]) => {
43
+ var _a;
44
+ switch (typeof v) {
45
+ case "string":
46
+ entry.push({ name: k, import: v });
47
+ break;
48
+ case "object":
49
+ if (!Array.isArray(v)) {
50
+ switch (typeof v.import) {
51
+ case "string":
52
+ entry.push({
53
+ name: k,
54
+ import: v.import,
55
+ library: ((_a = v.library) === null || _a === void 0 ? void 0 : _a.type) === "umd"
56
+ ? {
57
+ name: typeof v.library.name === "string"
58
+ ? v.library.name
59
+ : undefined,
60
+ export: typeof v.library.export === "string"
61
+ ? [v.library.export]
62
+ : v.library.export,
63
+ }
64
+ : undefined,
65
+ });
66
+ break;
67
+ default:
68
+ break;
69
+ }
70
+ }
71
+ else {
72
+ if (v.length === 0) {
73
+ throw "entry value items is empty";
74
+ }
75
+ else if (v.length === 1) {
76
+ entry.push({ name: k, import: v[0] });
77
+ }
78
+ else {
79
+ throw "multi entry items for one entry not supported yet";
80
+ }
81
+ }
82
+ break;
83
+ default:
84
+ throw "non string and non object entry path not supported yet";
85
+ }
86
+ });
87
+ }
88
+ break;
89
+ case "function":
90
+ throw "functional entry not supported yet";
91
+ default:
92
+ throw "entry config not compatible now";
93
+ }
94
+ return entry;
95
+ }
96
+ function compatFromWebpackPlugin(webpackPlugins, picker) {
97
+ const plugin = webpackPlugins === null || webpackPlugins === void 0 ? void 0 : webpackPlugins.find((p) => p && typeof p === "object" && p.constructor.name === picker.pluginName);
98
+ return picker(plugin);
99
+ }
100
+ function compatFromWebpackPlugins(webpackPlugins, picker) {
101
+ const plugins = webpackPlugins === null || webpackPlugins === void 0 ? void 0 : webpackPlugins.filter((p) => p && typeof p === "object" && p.constructor.name === picker.pluginName);
102
+ if (!plugins || plugins.length === 0) {
103
+ return undefined;
104
+ }
105
+ const results = plugins.map(picker).filter((r) => r !== undefined);
106
+ if (results.length === 0)
107
+ return undefined;
108
+ return results.length === 1 ? results[0] : results;
109
+ }
110
+ compatHtml.pluginName = "HtmlWebpackPlugin";
111
+ function compatHtml(maybeWebpackPluginInstance) {
112
+ if (!maybeWebpackPluginInstance) {
113
+ return undefined;
114
+ }
115
+ return (maybeWebpackPluginInstance.userOptions ||
116
+ maybeWebpackPluginInstance.options);
117
+ }
118
+ compatDefine.pluginName = "DefinePlugin";
119
+ function compatDefine(maybeWebpackPluginInstance) {
120
+ const definitions = maybeWebpackPluginInstance === null || maybeWebpackPluginInstance === void 0 ? void 0 : maybeWebpackPluginInstance.definitions;
121
+ if (!definitions || typeof definitions !== "object") {
122
+ return definitions;
123
+ }
124
+ const processedDefinitions = {};
125
+ for (const [key, value] of Object.entries(definitions)) {
126
+ if (typeof value === "object" && value !== null) {
127
+ processedDefinitions[key] = JSON.stringify(value);
128
+ }
129
+ else {
130
+ processedDefinitions[key] = value;
131
+ }
132
+ }
133
+ return processedDefinitions;
134
+ }
135
+ compatProvider.pluginName = "ProvidePlugin";
136
+ function compatProvider(maybeWebpackPluginInstance) {
137
+ const definitions = maybeWebpackPluginInstance === null || maybeWebpackPluginInstance === void 0 ? void 0 : maybeWebpackPluginInstance.definitions;
138
+ if (!definitions || typeof definitions !== "object") {
139
+ return undefined;
140
+ }
141
+ const provider = {};
142
+ for (const [key, value] of Object.entries(definitions)) {
143
+ if (typeof value === "string") {
144
+ // Simple module import: { $: 'jquery' }
145
+ provider[key] = value;
146
+ }
147
+ else if (Array.isArray(value)) {
148
+ if (value.length === 1) {
149
+ // e.g. { process: ['process/browser'] } for default import
150
+ provider[key] = value[0];
151
+ }
152
+ else if (value.length >= 2) {
153
+ // Named export import: { Buffer: ['buffer', 'Buffer'] }
154
+ provider[key] = [value[0], value[1]];
155
+ }
156
+ }
157
+ }
158
+ return Object.keys(provider).length > 0 ? provider : undefined;
159
+ }
160
+ function compatExternals(webpackExternals) {
161
+ if (!webpackExternals) {
162
+ return undefined;
163
+ }
164
+ let externals = {};
165
+ switch (typeof webpackExternals) {
166
+ case "string": {
167
+ // Single string external: "lodash" -> { "lodash": "lodash" }
168
+ externals[webpackExternals] = webpackExternals;
169
+ break;
170
+ }
171
+ case "object": {
172
+ if (Array.isArray(webpackExternals)) {
173
+ // Array of externals: ["lodash", "react"] -> { "lodash": "lodash", "react": "react" }
174
+ externals = webpackExternals.reduce((acc, external) => {
175
+ if (typeof external === "string") {
176
+ acc[external] = external;
177
+ }
178
+ else if (typeof external === "object" && external !== null) {
179
+ Object.assign(acc, compatExternals(external));
180
+ }
181
+ return acc;
182
+ }, {});
183
+ }
184
+ else if (webpackExternals instanceof RegExp) {
185
+ throw "regex external not supported yet";
186
+ }
187
+ else {
188
+ if ("byLayer" in webpackExternals) {
189
+ throw "by layer external item not supported yet";
190
+ }
191
+ Object.entries(webpackExternals).forEach(([key, value]) => {
192
+ if (typeof value === "string") {
193
+ // Check if it's a script type with shorthand syntax: "global@https://example.com/script.js"
194
+ if (value.includes("@") &&
195
+ (value.startsWith("script ") || value.includes("://"))) {
196
+ const match = value.match(/^(?:script\s+)?(.+?)@(.+)$/);
197
+ if (match) {
198
+ const [, globalName, scriptUrl] = match;
199
+ // Use utoopack string format: "script globalName@url"
200
+ externals[key] = `script ${globalName}@${scriptUrl}`;
201
+ }
202
+ else {
203
+ externals[key] = value;
204
+ }
205
+ }
206
+ else {
207
+ // Simple string mapping: { "react": "React" }
208
+ externals[key] = value;
209
+ }
210
+ }
211
+ else if (Array.isArray(value)) {
212
+ // Array format handling
213
+ if (value.length >= 2) {
214
+ const [first, second] = value;
215
+ // Check if it's a script type array: ["https://example.com/script.js", "GlobalName"]
216
+ if (typeof first === "string" &&
217
+ first.includes("://") &&
218
+ typeof second === "string") {
219
+ // Use utoopack object format for script
220
+ externals[key] = {
221
+ root: second,
222
+ type: "script",
223
+ script: first,
224
+ };
225
+ }
226
+ else if (typeof first === "string" &&
227
+ typeof second === "string") {
228
+ // Handle type prefix formats
229
+ if (first.startsWith("commonjs")) {
230
+ externals[key] = `commonjs ${second}`;
231
+ }
232
+ else if (first === "module") {
233
+ externals[key] = `esm ${second}`;
234
+ }
235
+ else if (first === "var" ||
236
+ first === "global" ||
237
+ first === "window") {
238
+ externals[key] = second;
239
+ }
240
+ else if (first === "script") {
241
+ // Script type without URL in array format - treat as regular script prefix
242
+ externals[key] = `script ${second}`;
243
+ }
244
+ else {
245
+ externals[key] = `${first} ${second}`;
246
+ }
247
+ }
248
+ else {
249
+ externals[key] = value[0] || key;
250
+ }
251
+ }
252
+ else {
253
+ externals[key] = value[0] || key;
254
+ }
255
+ }
256
+ else if (typeof value === "object" && value !== null) {
257
+ // Object format: handle complex configurations
258
+ if ("root" in value || "commonjs" in value || "amd" in value) {
259
+ // Standard webpack externals object format
260
+ if (value.commonjs) {
261
+ externals[key] = `commonjs ${value.commonjs}`;
262
+ }
263
+ else if (value.root) {
264
+ externals[key] = value.root;
265
+ }
266
+ else if (value.amd) {
267
+ externals[key] = value.amd;
268
+ }
269
+ else {
270
+ externals[key] = key;
271
+ }
272
+ }
273
+ else {
274
+ // Treat as utoopack specific configuration (might already be in correct format)
275
+ externals[key] = value;
276
+ }
277
+ }
278
+ else {
279
+ // Fallback to key name
280
+ externals[key] = key;
281
+ }
282
+ });
283
+ }
284
+ break;
285
+ }
286
+ case "function": {
287
+ throw "functional external not supported yet";
288
+ }
289
+ default:
290
+ break;
291
+ }
292
+ return externals;
293
+ }
294
+ function compatModule(webpackModule) {
295
+ if (!Array.isArray(webpackModule === null || webpackModule === void 0 ? void 0 : webpackModule.rules)) {
296
+ return;
297
+ }
298
+ const moduleRules = {
299
+ rules: webpackModule.rules.reduce((acc, cur) => {
300
+ var _a, _b;
301
+ switch (typeof cur) {
302
+ case "object":
303
+ if (cur) {
304
+ let condition = (_b = (_a = cur.test) === null || _a === void 0 ? void 0 : _a.toString().match(/(\.\w+)/)) === null || _b === void 0 ? void 0 : _b[1];
305
+ if (condition) {
306
+ Object.assign(acc, {
307
+ ["*" + condition]: {
308
+ loaders: typeof cur.use === "string"
309
+ ? [cur.use]
310
+ : typeof (cur === null || cur === void 0 ? void 0 : cur.use) === "object"
311
+ ? Array.isArray(cur.use)
312
+ ? cur.use.map((use) => typeof use === "string"
313
+ ? { loader: use, options: {} }
314
+ : {
315
+ loader: use.loader,
316
+ options: use.options || {},
317
+ })
318
+ : [
319
+ {
320
+ loader: cur.loader,
321
+ options: cur.options || {},
322
+ },
323
+ ]
324
+ : [],
325
+ as: "*.js",
326
+ },
327
+ });
328
+ }
329
+ }
330
+ break;
331
+ default:
332
+ break;
333
+ }
334
+ return acc;
335
+ }, {}),
336
+ };
337
+ return moduleRules;
338
+ }
339
+ function compatResolve(webpackResolve) {
340
+ if (!webpackResolve) {
341
+ return;
342
+ }
343
+ const { alias, extensions } = webpackResolve;
344
+ return {
345
+ alias: alias
346
+ ? Array.isArray(alias)
347
+ ? alias.reduce((acc, cur) => Object.assign(acc, { [cur.name]: cur.alias }), {})
348
+ : Object.entries(alias).reduce((acc, [k, v]) => {
349
+ if (typeof v === "string") {
350
+ // Handle alias keys ending with $ by removing the $
351
+ const aliasKey = k.endsWith("$") ? k.slice(0, -1) : k;
352
+ Object.assign(acc, { [aliasKey]: v });
353
+ }
354
+ else {
355
+ throw "non string alias value not supported yet";
356
+ }
357
+ return acc;
358
+ }, {})
359
+ : undefined,
360
+ extensions,
361
+ };
362
+ }
363
+ function compatOutput(webpackOutput) {
364
+ if ((webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.filename) && typeof webpackOutput.filename !== "string") {
365
+ throw "non string output filename not supported yet";
366
+ }
367
+ if ((webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.chunkFilename) &&
368
+ typeof webpackOutput.chunkFilename !== "string") {
369
+ throw "non string output chunkFilename not supported yet";
370
+ }
371
+ if ((webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.publicPath) &&
372
+ typeof webpackOutput.publicPath !== "string") {
373
+ throw "non string output publicPath not supported yet";
374
+ }
375
+ return {
376
+ path: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.path,
377
+ filename: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.filename,
378
+ chunkFilename: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.chunkFilename,
379
+ clean: !!(webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.clean),
380
+ publicPath: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.publicPath,
381
+ };
382
+ }
383
+ function compatTarget(webpackTarget) {
384
+ return webpackTarget
385
+ ? Array.isArray(webpackTarget)
386
+ ? webpackTarget.join(" ")
387
+ : webpackTarget
388
+ : undefined;
389
+ }
390
+ function compatSourceMaps(webpackSourceMaps) {
391
+ return !!webpackSourceMaps;
392
+ }
393
+ function compatOptimization(webpackOptimization) {
394
+ if (!webpackOptimization) {
395
+ return;
396
+ }
397
+ const { moduleIds, minimize, concatenateModules } = webpackOptimization;
398
+ return {
399
+ moduleIds: moduleIds === "named"
400
+ ? "named"
401
+ : moduleIds === "deterministic"
402
+ ? "deterministic"
403
+ : undefined,
404
+ minify: minimize,
405
+ concatenateModules,
406
+ };
407
+ }
408
+ function compatStats(webpackStats) {
409
+ return !!webpackStats;
410
+ }