@utoo/pack-shared 1.2.10-alpha.0 → 1.2.10-rc.1
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/cjs/config.d.ts +13 -0
- package/cjs/webpackCompat.d.ts +6 -1
- package/cjs/webpackCompat.js +137 -56
- package/esm/config.d.ts +13 -0
- package/esm/webpackCompat.d.ts +6 -1
- package/esm/webpackCompat.js +137 -56
- package/package.json +1 -1
package/cjs/config.d.ts
CHANGED
|
@@ -24,6 +24,16 @@ export type RustifiedEnv = {
|
|
|
24
24
|
value: string;
|
|
25
25
|
}[];
|
|
26
26
|
export interface ExperimentalConfig {
|
|
27
|
+
swcPlugins?: [string, any][];
|
|
28
|
+
dynamicIO?: boolean;
|
|
29
|
+
useCache?: boolean;
|
|
30
|
+
cacheHandlers?: Record<string, string>;
|
|
31
|
+
esmExternals?: boolean | "loose";
|
|
32
|
+
ppr?: boolean | "incremental";
|
|
33
|
+
taint?: boolean;
|
|
34
|
+
reactCompiler?: boolean | any;
|
|
35
|
+
viewTransition?: boolean;
|
|
36
|
+
serverActions?: boolean | any;
|
|
27
37
|
}
|
|
28
38
|
export type JSONValue = string | number | boolean | JSONValue[] | {
|
|
29
39
|
[k: string]: JSONValue;
|
|
@@ -102,6 +112,7 @@ export interface ConfigComplete {
|
|
|
102
112
|
filename?: string;
|
|
103
113
|
chunkFilename?: string;
|
|
104
114
|
cssFilename?: string;
|
|
115
|
+
cssChunkFilename?: string;
|
|
105
116
|
assetModuleFilename?: string;
|
|
106
117
|
clean?: boolean;
|
|
107
118
|
copy?: Array<{
|
|
@@ -109,6 +120,7 @@ export interface ConfigComplete {
|
|
|
109
120
|
to?: string;
|
|
110
121
|
} | string>;
|
|
111
122
|
publicPath?: string;
|
|
123
|
+
chunkLoadingGlobal?: string;
|
|
112
124
|
entryRootExport?: string;
|
|
113
125
|
};
|
|
114
126
|
target?: string;
|
|
@@ -117,6 +129,7 @@ export interface ConfigComplete {
|
|
|
117
129
|
provider?: ProviderConfig;
|
|
118
130
|
optimization?: {
|
|
119
131
|
moduleIds?: "named" | "deterministic";
|
|
132
|
+
noMangling?: boolean;
|
|
120
133
|
minify?: boolean;
|
|
121
134
|
treeShaking?: boolean;
|
|
122
135
|
splitChunks?: Record<"js" | "css", {
|
package/cjs/webpackCompat.d.ts
CHANGED
|
@@ -38,10 +38,13 @@ export interface WebpackOutput {
|
|
|
38
38
|
publicPath?: string | ((...args: any[]) => string);
|
|
39
39
|
filename?: string | ((...args: any[]) => string);
|
|
40
40
|
chunkFilename?: string | ((...args: any[]) => string);
|
|
41
|
-
|
|
41
|
+
cssFilename?: string | ((...args: any[]) => string);
|
|
42
|
+
cssChunkFilename?: string | ((...args: any[]) => string);
|
|
43
|
+
assetModuleFilename?: string | ((...args: any[]) => string);
|
|
42
44
|
library?: any;
|
|
43
45
|
libraryTarget?: string;
|
|
44
46
|
globalObject?: string;
|
|
47
|
+
chunkLoadingGlobal?: string;
|
|
45
48
|
clean?: boolean | {
|
|
46
49
|
keep?: any;
|
|
47
50
|
};
|
|
@@ -54,6 +57,8 @@ export interface WebpackOptimization {
|
|
|
54
57
|
moduleIds?: string;
|
|
55
58
|
chunkIds?: string;
|
|
56
59
|
concatenateModules?: boolean;
|
|
60
|
+
usedExports?: boolean | "global";
|
|
61
|
+
mangleExports?: boolean | "deterministic" | "size";
|
|
57
62
|
}
|
|
58
63
|
export type WebpackEntry = string | string[] | Record<string, string | {
|
|
59
64
|
import: string;
|
package/cjs/webpackCompat.js
CHANGED
|
@@ -3,14 +3,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.compatOptionsFromWebpack = compatOptionsFromWebpack;
|
|
4
4
|
function compatOptionsFromWebpack(webpackConfig) {
|
|
5
5
|
const { entry, mode, module, resolve, externals, output, target, devtool, optimization, plugins, stats, } = webpackConfig;
|
|
6
|
+
const html = compatFromWebpackPlugin(plugins, compatHtml);
|
|
7
|
+
const copy = compatFromWebpackPlugin(plugins, compatCopy);
|
|
8
|
+
const outputCompat = compatOutput(output);
|
|
9
|
+
if (outputCompat && !outputCompat.cssFilename) {
|
|
10
|
+
outputCompat.cssFilename = compatFromWebpackPlugin(plugins, compatMiniCssExtract);
|
|
11
|
+
}
|
|
12
|
+
if (outputCompat && copy) {
|
|
13
|
+
outputCompat.copy = copy;
|
|
14
|
+
}
|
|
6
15
|
return {
|
|
7
16
|
config: {
|
|
8
|
-
entry: compatEntry(entry),
|
|
17
|
+
entry: compatEntry(entry, html),
|
|
9
18
|
mode: compatMode(mode),
|
|
10
19
|
module: compatModule(module),
|
|
11
20
|
resolve: compatResolve(resolve),
|
|
12
21
|
externals: compatExternals(externals),
|
|
13
|
-
output:
|
|
22
|
+
output: outputCompat,
|
|
14
23
|
target: compatTarget(target),
|
|
15
24
|
sourceMaps: compatSourceMaps(devtool),
|
|
16
25
|
optimization: compatOptimization(optimization),
|
|
@@ -29,19 +38,20 @@ function compatMode(webpackMode) {
|
|
|
29
38
|
: webpackMode
|
|
30
39
|
: "production";
|
|
31
40
|
}
|
|
32
|
-
function compatEntry(webpackEntry) {
|
|
41
|
+
function compatEntry(webpackEntry, html) {
|
|
33
42
|
if (!webpackEntry) {
|
|
34
43
|
return undefined;
|
|
35
44
|
}
|
|
36
45
|
const entry = [];
|
|
37
46
|
switch (typeof webpackEntry) {
|
|
38
47
|
case "string":
|
|
39
|
-
entry.push({ import: webpackEntry });
|
|
48
|
+
entry.push({ import: webpackEntry, html });
|
|
40
49
|
break;
|
|
41
50
|
case "object":
|
|
42
51
|
if (Array.isArray(webpackEntry)) {
|
|
43
52
|
webpackEntry.forEach((e) => entry.push({
|
|
44
53
|
import: e,
|
|
54
|
+
html,
|
|
45
55
|
}));
|
|
46
56
|
}
|
|
47
57
|
else {
|
|
@@ -49,7 +59,7 @@ function compatEntry(webpackEntry) {
|
|
|
49
59
|
var _a;
|
|
50
60
|
switch (typeof v) {
|
|
51
61
|
case "string":
|
|
52
|
-
entry.push({ name: k, import: v });
|
|
62
|
+
entry.push({ name: k, import: v, html });
|
|
53
63
|
break;
|
|
54
64
|
case "object":
|
|
55
65
|
if (!Array.isArray(v)) {
|
|
@@ -58,6 +68,7 @@ function compatEntry(webpackEntry) {
|
|
|
58
68
|
entry.push({
|
|
59
69
|
name: k,
|
|
60
70
|
import: v.import,
|
|
71
|
+
html,
|
|
61
72
|
library: ((_a = v.library) === null || _a === void 0 ? void 0 : _a.type) === "umd"
|
|
62
73
|
? {
|
|
63
74
|
name: typeof v.library.name === "string"
|
|
@@ -79,7 +90,7 @@ function compatEntry(webpackEntry) {
|
|
|
79
90
|
throw "entry value items is empty";
|
|
80
91
|
}
|
|
81
92
|
else if (v.length === 1) {
|
|
82
|
-
entry.push({ name: k, import: v[0] });
|
|
93
|
+
entry.push({ name: k, import: v[0], html });
|
|
83
94
|
}
|
|
84
95
|
else {
|
|
85
96
|
throw "multi entry items for one entry not supported yet";
|
|
@@ -93,9 +104,9 @@ function compatEntry(webpackEntry) {
|
|
|
93
104
|
}
|
|
94
105
|
break;
|
|
95
106
|
case "function":
|
|
96
|
-
throw "functional entry not supported yet";
|
|
107
|
+
throw new Error("functional entry not supported yet");
|
|
97
108
|
default:
|
|
98
|
-
throw "entry config not compatible now";
|
|
109
|
+
throw new Error("entry config not compatible now");
|
|
99
110
|
}
|
|
100
111
|
return entry;
|
|
101
112
|
}
|
|
@@ -111,6 +122,25 @@ function compatHtml(maybeWebpackPluginInstance) {
|
|
|
111
122
|
return (maybeWebpackPluginInstance.userOptions ||
|
|
112
123
|
maybeWebpackPluginInstance.options);
|
|
113
124
|
}
|
|
125
|
+
compatMiniCssExtract.pluginName = "MiniCssExtractPlugin";
|
|
126
|
+
function compatMiniCssExtract(maybeWebpackPluginInstance) {
|
|
127
|
+
var _a;
|
|
128
|
+
return (_a = maybeWebpackPluginInstance === null || maybeWebpackPluginInstance === void 0 ? void 0 : maybeWebpackPluginInstance.options) === null || _a === void 0 ? void 0 : _a.filename;
|
|
129
|
+
}
|
|
130
|
+
compatCopy.pluginName = "CopyPlugin";
|
|
131
|
+
function compatCopy(maybeWebpackPluginInstance) {
|
|
132
|
+
const patterns = maybeWebpackPluginInstance === null || maybeWebpackPluginInstance === void 0 ? void 0 : maybeWebpackPluginInstance.patterns;
|
|
133
|
+
if (!Array.isArray(patterns))
|
|
134
|
+
return undefined;
|
|
135
|
+
return patterns.map((pattern) => {
|
|
136
|
+
if (typeof pattern === "string")
|
|
137
|
+
return pattern;
|
|
138
|
+
return {
|
|
139
|
+
from: pattern.from,
|
|
140
|
+
to: pattern.to,
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
}
|
|
114
144
|
compatDefine.pluginName = "DefinePlugin";
|
|
115
145
|
function compatDefine(maybeWebpackPluginInstance) {
|
|
116
146
|
const definitions = maybeWebpackPluginInstance === null || maybeWebpackPluginInstance === void 0 ? void 0 : maybeWebpackPluginInstance.definitions;
|
|
@@ -178,11 +208,11 @@ function compatExternals(webpackExternals) {
|
|
|
178
208
|
}, {});
|
|
179
209
|
}
|
|
180
210
|
else if (webpackExternals instanceof RegExp) {
|
|
181
|
-
throw "regex external not supported yet";
|
|
211
|
+
throw new Error("regex external not supported yet");
|
|
182
212
|
}
|
|
183
213
|
else {
|
|
184
214
|
if ("byLayer" in webpackExternals) {
|
|
185
|
-
throw "by layer external item not supported yet";
|
|
215
|
+
throw new Error("by layer external item not supported yet");
|
|
186
216
|
}
|
|
187
217
|
Object.entries(webpackExternals).forEach(([key, value]) => {
|
|
188
218
|
if (typeof value === "string") {
|
|
@@ -281,7 +311,7 @@ function compatExternals(webpackExternals) {
|
|
|
281
311
|
break;
|
|
282
312
|
}
|
|
283
313
|
case "function": {
|
|
284
|
-
throw "functional external not supported yet";
|
|
314
|
+
throw new Error("functional external not supported yet");
|
|
285
315
|
}
|
|
286
316
|
default:
|
|
287
317
|
break;
|
|
@@ -294,39 +324,72 @@ function compatModule(webpackModule) {
|
|
|
294
324
|
}
|
|
295
325
|
const moduleRules = {
|
|
296
326
|
rules: webpackModule.rules.reduce((acc, cur) => {
|
|
297
|
-
var _a
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
327
|
+
var _a;
|
|
328
|
+
if (typeof cur === "object" && cur) {
|
|
329
|
+
// Normalize condition from test, include, exclude
|
|
330
|
+
let ruleCondition = undefined;
|
|
331
|
+
if (cur.test || cur.include || cur.exclude) {
|
|
332
|
+
const conditions = [];
|
|
333
|
+
if (cur.test)
|
|
334
|
+
conditions.push({ path: cur.test });
|
|
335
|
+
if (cur.include)
|
|
336
|
+
conditions.push({ path: cur.include });
|
|
337
|
+
if (cur.exclude)
|
|
338
|
+
conditions.push({ not: { path: cur.exclude } });
|
|
339
|
+
ruleCondition =
|
|
340
|
+
conditions.length === 1 ? conditions[0] : { all: conditions };
|
|
341
|
+
}
|
|
342
|
+
// Determine the glob keys for the rules record
|
|
343
|
+
// utoo usually uses extensions as keys like "*.svg"
|
|
344
|
+
const extensions = (_a = cur.test) === null || _a === void 0 ? void 0 : _a.toString().match(/\.(\w+)/g);
|
|
345
|
+
const globKeys = extensions
|
|
346
|
+
? extensions.map((ext) => `*${ext}`)
|
|
347
|
+
: ["*"];
|
|
348
|
+
// Handle loaders
|
|
349
|
+
const loaders = typeof cur.use === "string"
|
|
350
|
+
? [{ loader: cur.use, options: {} }]
|
|
351
|
+
: Array.isArray(cur.use)
|
|
352
|
+
? cur.use.map((use) => typeof use === "string"
|
|
353
|
+
? { loader: use, options: {} }
|
|
354
|
+
: {
|
|
355
|
+
loader: use.loader,
|
|
356
|
+
options: use.options || {},
|
|
357
|
+
})
|
|
358
|
+
: cur.loader
|
|
359
|
+
? [
|
|
360
|
+
{
|
|
361
|
+
loader: cur.loader,
|
|
362
|
+
options: cur.options || {},
|
|
323
363
|
},
|
|
324
|
-
|
|
364
|
+
]
|
|
365
|
+
: [];
|
|
366
|
+
const ruleItem = {
|
|
367
|
+
loaders,
|
|
368
|
+
condition: ruleCondition,
|
|
369
|
+
as: cur.type === "asset" ||
|
|
370
|
+
cur.type === "asset/resource" ||
|
|
371
|
+
cur.type === "asset/inline" ||
|
|
372
|
+
cur.type === "asset/source" ||
|
|
373
|
+
(loaders.length > 0 &&
|
|
374
|
+
!globKeys.some((k) => k === "*.js" || k === "*.ts" || k === "*.tsx"))
|
|
375
|
+
? "*.js"
|
|
376
|
+
: undefined,
|
|
377
|
+
};
|
|
378
|
+
for (const key of globKeys) {
|
|
379
|
+
const existing = acc[key];
|
|
380
|
+
if (!existing) {
|
|
381
|
+
acc[key] = ruleItem;
|
|
382
|
+
}
|
|
383
|
+
else {
|
|
384
|
+
// If already exists, turn into a collection array
|
|
385
|
+
if (Array.isArray(existing)) {
|
|
386
|
+
existing.push(ruleItem);
|
|
387
|
+
}
|
|
388
|
+
else {
|
|
389
|
+
acc[key] = [existing, ruleItem];
|
|
325
390
|
}
|
|
326
391
|
}
|
|
327
|
-
|
|
328
|
-
default:
|
|
329
|
-
break;
|
|
392
|
+
}
|
|
330
393
|
}
|
|
331
394
|
return acc;
|
|
332
395
|
}, {}),
|
|
@@ -346,10 +409,10 @@ function compatResolve(webpackResolve) {
|
|
|
346
409
|
if (typeof v === "string") {
|
|
347
410
|
// Handle alias keys ending with $ by removing the $
|
|
348
411
|
const aliasKey = k.endsWith("$") ? k.slice(0, -1) : k;
|
|
349
|
-
|
|
412
|
+
acc[aliasKey] = v;
|
|
350
413
|
}
|
|
351
414
|
else {
|
|
352
|
-
throw "non string alias value not supported yet";
|
|
415
|
+
throw new Error("non string alias value not supported yet");
|
|
353
416
|
}
|
|
354
417
|
return acc;
|
|
355
418
|
}, {})
|
|
@@ -357,24 +420,38 @@ function compatResolve(webpackResolve) {
|
|
|
357
420
|
extensions,
|
|
358
421
|
};
|
|
359
422
|
}
|
|
423
|
+
function normalizeWebpackHash(filename) {
|
|
424
|
+
if (!filename)
|
|
425
|
+
return filename;
|
|
426
|
+
// Replace [hash] and [chunkhash] with [contenthash] as utoo only supports contenthash
|
|
427
|
+
// Also remove [ext] as it is automatically appended in utoopack
|
|
428
|
+
return filename
|
|
429
|
+
.replace(/\[(?:hash|chunkhash)(?::(\d+))?\]/g, "[contenthash:$1]")
|
|
430
|
+
.replace(/\[ext\]/g, "");
|
|
431
|
+
}
|
|
360
432
|
function compatOutput(webpackOutput) {
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
typeof webpackOutput
|
|
370
|
-
|
|
433
|
+
for (const field of [
|
|
434
|
+
"filename",
|
|
435
|
+
"chunkFilename",
|
|
436
|
+
"publicPath",
|
|
437
|
+
"cssFilename",
|
|
438
|
+
"cssChunkFilename",
|
|
439
|
+
"assetModuleFilename",
|
|
440
|
+
]) {
|
|
441
|
+
if ((webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput[field]) && typeof webpackOutput[field] !== "string") {
|
|
442
|
+
throw new Error(`non string output ${field} not supported yet`);
|
|
443
|
+
}
|
|
371
444
|
}
|
|
372
445
|
return {
|
|
373
446
|
path: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.path,
|
|
374
|
-
filename: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.filename,
|
|
375
|
-
chunkFilename: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.chunkFilename,
|
|
447
|
+
filename: normalizeWebpackHash(webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.filename),
|
|
448
|
+
chunkFilename: normalizeWebpackHash(webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.chunkFilename),
|
|
449
|
+
cssFilename: normalizeWebpackHash(webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.cssFilename),
|
|
450
|
+
cssChunkFilename: normalizeWebpackHash(webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.cssChunkFilename),
|
|
451
|
+
assetModuleFilename: normalizeWebpackHash(webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.assetModuleFilename),
|
|
376
452
|
clean: !!(webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.clean),
|
|
377
453
|
publicPath: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.publicPath,
|
|
454
|
+
chunkLoadingGlobal: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.chunkLoadingGlobal,
|
|
378
455
|
};
|
|
379
456
|
}
|
|
380
457
|
function compatTarget(webpackTarget) {
|
|
@@ -391,15 +468,19 @@ function compatOptimization(webpackOptimization) {
|
|
|
391
468
|
if (!webpackOptimization) {
|
|
392
469
|
return;
|
|
393
470
|
}
|
|
394
|
-
const { moduleIds, minimize, concatenateModules } = webpackOptimization;
|
|
471
|
+
const { moduleIds, minimize, concatenateModules, usedExports } = webpackOptimization;
|
|
395
472
|
return {
|
|
396
473
|
moduleIds: moduleIds === "named"
|
|
397
474
|
? "named"
|
|
398
475
|
: moduleIds === "deterministic"
|
|
399
476
|
? "deterministic"
|
|
400
477
|
: undefined,
|
|
478
|
+
noMangling: webpackOptimization.mangleExports === false,
|
|
401
479
|
minify: minimize,
|
|
402
480
|
concatenateModules,
|
|
481
|
+
treeShaking: !!usedExports,
|
|
482
|
+
removeUnusedExports: !!usedExports,
|
|
483
|
+
removeUnusedImports: !!usedExports,
|
|
403
484
|
};
|
|
404
485
|
}
|
|
405
486
|
function compatStats(webpackStats) {
|
package/esm/config.d.ts
CHANGED
|
@@ -24,6 +24,16 @@ export type RustifiedEnv = {
|
|
|
24
24
|
value: string;
|
|
25
25
|
}[];
|
|
26
26
|
export interface ExperimentalConfig {
|
|
27
|
+
swcPlugins?: [string, any][];
|
|
28
|
+
dynamicIO?: boolean;
|
|
29
|
+
useCache?: boolean;
|
|
30
|
+
cacheHandlers?: Record<string, string>;
|
|
31
|
+
esmExternals?: boolean | "loose";
|
|
32
|
+
ppr?: boolean | "incremental";
|
|
33
|
+
taint?: boolean;
|
|
34
|
+
reactCompiler?: boolean | any;
|
|
35
|
+
viewTransition?: boolean;
|
|
36
|
+
serverActions?: boolean | any;
|
|
27
37
|
}
|
|
28
38
|
export type JSONValue = string | number | boolean | JSONValue[] | {
|
|
29
39
|
[k: string]: JSONValue;
|
|
@@ -102,6 +112,7 @@ export interface ConfigComplete {
|
|
|
102
112
|
filename?: string;
|
|
103
113
|
chunkFilename?: string;
|
|
104
114
|
cssFilename?: string;
|
|
115
|
+
cssChunkFilename?: string;
|
|
105
116
|
assetModuleFilename?: string;
|
|
106
117
|
clean?: boolean;
|
|
107
118
|
copy?: Array<{
|
|
@@ -109,6 +120,7 @@ export interface ConfigComplete {
|
|
|
109
120
|
to?: string;
|
|
110
121
|
} | string>;
|
|
111
122
|
publicPath?: string;
|
|
123
|
+
chunkLoadingGlobal?: string;
|
|
112
124
|
entryRootExport?: string;
|
|
113
125
|
};
|
|
114
126
|
target?: string;
|
|
@@ -117,6 +129,7 @@ export interface ConfigComplete {
|
|
|
117
129
|
provider?: ProviderConfig;
|
|
118
130
|
optimization?: {
|
|
119
131
|
moduleIds?: "named" | "deterministic";
|
|
132
|
+
noMangling?: boolean;
|
|
120
133
|
minify?: boolean;
|
|
121
134
|
treeShaking?: boolean;
|
|
122
135
|
splitChunks?: Record<"js" | "css", {
|
package/esm/webpackCompat.d.ts
CHANGED
|
@@ -38,10 +38,13 @@ export interface WebpackOutput {
|
|
|
38
38
|
publicPath?: string | ((...args: any[]) => string);
|
|
39
39
|
filename?: string | ((...args: any[]) => string);
|
|
40
40
|
chunkFilename?: string | ((...args: any[]) => string);
|
|
41
|
-
|
|
41
|
+
cssFilename?: string | ((...args: any[]) => string);
|
|
42
|
+
cssChunkFilename?: string | ((...args: any[]) => string);
|
|
43
|
+
assetModuleFilename?: string | ((...args: any[]) => string);
|
|
42
44
|
library?: any;
|
|
43
45
|
libraryTarget?: string;
|
|
44
46
|
globalObject?: string;
|
|
47
|
+
chunkLoadingGlobal?: string;
|
|
45
48
|
clean?: boolean | {
|
|
46
49
|
keep?: any;
|
|
47
50
|
};
|
|
@@ -54,6 +57,8 @@ export interface WebpackOptimization {
|
|
|
54
57
|
moduleIds?: string;
|
|
55
58
|
chunkIds?: string;
|
|
56
59
|
concatenateModules?: boolean;
|
|
60
|
+
usedExports?: boolean | "global";
|
|
61
|
+
mangleExports?: boolean | "deterministic" | "size";
|
|
57
62
|
}
|
|
58
63
|
export type WebpackEntry = string | string[] | Record<string, string | {
|
|
59
64
|
import: string;
|
package/esm/webpackCompat.js
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
export function compatOptionsFromWebpack(webpackConfig) {
|
|
2
2
|
const { entry, mode, module, resolve, externals, output, target, devtool, optimization, plugins, stats, } = webpackConfig;
|
|
3
|
+
const html = compatFromWebpackPlugin(plugins, compatHtml);
|
|
4
|
+
const copy = compatFromWebpackPlugin(plugins, compatCopy);
|
|
5
|
+
const outputCompat = compatOutput(output);
|
|
6
|
+
if (outputCompat && !outputCompat.cssFilename) {
|
|
7
|
+
outputCompat.cssFilename = compatFromWebpackPlugin(plugins, compatMiniCssExtract);
|
|
8
|
+
}
|
|
9
|
+
if (outputCompat && copy) {
|
|
10
|
+
outputCompat.copy = copy;
|
|
11
|
+
}
|
|
3
12
|
return {
|
|
4
13
|
config: {
|
|
5
|
-
entry: compatEntry(entry),
|
|
14
|
+
entry: compatEntry(entry, html),
|
|
6
15
|
mode: compatMode(mode),
|
|
7
16
|
module: compatModule(module),
|
|
8
17
|
resolve: compatResolve(resolve),
|
|
9
18
|
externals: compatExternals(externals),
|
|
10
|
-
output:
|
|
19
|
+
output: outputCompat,
|
|
11
20
|
target: compatTarget(target),
|
|
12
21
|
sourceMaps: compatSourceMaps(devtool),
|
|
13
22
|
optimization: compatOptimization(optimization),
|
|
@@ -26,19 +35,20 @@ function compatMode(webpackMode) {
|
|
|
26
35
|
: webpackMode
|
|
27
36
|
: "production";
|
|
28
37
|
}
|
|
29
|
-
function compatEntry(webpackEntry) {
|
|
38
|
+
function compatEntry(webpackEntry, html) {
|
|
30
39
|
if (!webpackEntry) {
|
|
31
40
|
return undefined;
|
|
32
41
|
}
|
|
33
42
|
const entry = [];
|
|
34
43
|
switch (typeof webpackEntry) {
|
|
35
44
|
case "string":
|
|
36
|
-
entry.push({ import: webpackEntry });
|
|
45
|
+
entry.push({ import: webpackEntry, html });
|
|
37
46
|
break;
|
|
38
47
|
case "object":
|
|
39
48
|
if (Array.isArray(webpackEntry)) {
|
|
40
49
|
webpackEntry.forEach((e) => entry.push({
|
|
41
50
|
import: e,
|
|
51
|
+
html,
|
|
42
52
|
}));
|
|
43
53
|
}
|
|
44
54
|
else {
|
|
@@ -46,7 +56,7 @@ function compatEntry(webpackEntry) {
|
|
|
46
56
|
var _a;
|
|
47
57
|
switch (typeof v) {
|
|
48
58
|
case "string":
|
|
49
|
-
entry.push({ name: k, import: v });
|
|
59
|
+
entry.push({ name: k, import: v, html });
|
|
50
60
|
break;
|
|
51
61
|
case "object":
|
|
52
62
|
if (!Array.isArray(v)) {
|
|
@@ -55,6 +65,7 @@ function compatEntry(webpackEntry) {
|
|
|
55
65
|
entry.push({
|
|
56
66
|
name: k,
|
|
57
67
|
import: v.import,
|
|
68
|
+
html,
|
|
58
69
|
library: ((_a = v.library) === null || _a === void 0 ? void 0 : _a.type) === "umd"
|
|
59
70
|
? {
|
|
60
71
|
name: typeof v.library.name === "string"
|
|
@@ -76,7 +87,7 @@ function compatEntry(webpackEntry) {
|
|
|
76
87
|
throw "entry value items is empty";
|
|
77
88
|
}
|
|
78
89
|
else if (v.length === 1) {
|
|
79
|
-
entry.push({ name: k, import: v[0] });
|
|
90
|
+
entry.push({ name: k, import: v[0], html });
|
|
80
91
|
}
|
|
81
92
|
else {
|
|
82
93
|
throw "multi entry items for one entry not supported yet";
|
|
@@ -90,9 +101,9 @@ function compatEntry(webpackEntry) {
|
|
|
90
101
|
}
|
|
91
102
|
break;
|
|
92
103
|
case "function":
|
|
93
|
-
throw "functional entry not supported yet";
|
|
104
|
+
throw new Error("functional entry not supported yet");
|
|
94
105
|
default:
|
|
95
|
-
throw "entry config not compatible now";
|
|
106
|
+
throw new Error("entry config not compatible now");
|
|
96
107
|
}
|
|
97
108
|
return entry;
|
|
98
109
|
}
|
|
@@ -108,6 +119,25 @@ function compatHtml(maybeWebpackPluginInstance) {
|
|
|
108
119
|
return (maybeWebpackPluginInstance.userOptions ||
|
|
109
120
|
maybeWebpackPluginInstance.options);
|
|
110
121
|
}
|
|
122
|
+
compatMiniCssExtract.pluginName = "MiniCssExtractPlugin";
|
|
123
|
+
function compatMiniCssExtract(maybeWebpackPluginInstance) {
|
|
124
|
+
var _a;
|
|
125
|
+
return (_a = maybeWebpackPluginInstance === null || maybeWebpackPluginInstance === void 0 ? void 0 : maybeWebpackPluginInstance.options) === null || _a === void 0 ? void 0 : _a.filename;
|
|
126
|
+
}
|
|
127
|
+
compatCopy.pluginName = "CopyPlugin";
|
|
128
|
+
function compatCopy(maybeWebpackPluginInstance) {
|
|
129
|
+
const patterns = maybeWebpackPluginInstance === null || maybeWebpackPluginInstance === void 0 ? void 0 : maybeWebpackPluginInstance.patterns;
|
|
130
|
+
if (!Array.isArray(patterns))
|
|
131
|
+
return undefined;
|
|
132
|
+
return patterns.map((pattern) => {
|
|
133
|
+
if (typeof pattern === "string")
|
|
134
|
+
return pattern;
|
|
135
|
+
return {
|
|
136
|
+
from: pattern.from,
|
|
137
|
+
to: pattern.to,
|
|
138
|
+
};
|
|
139
|
+
});
|
|
140
|
+
}
|
|
111
141
|
compatDefine.pluginName = "DefinePlugin";
|
|
112
142
|
function compatDefine(maybeWebpackPluginInstance) {
|
|
113
143
|
const definitions = maybeWebpackPluginInstance === null || maybeWebpackPluginInstance === void 0 ? void 0 : maybeWebpackPluginInstance.definitions;
|
|
@@ -175,11 +205,11 @@ function compatExternals(webpackExternals) {
|
|
|
175
205
|
}, {});
|
|
176
206
|
}
|
|
177
207
|
else if (webpackExternals instanceof RegExp) {
|
|
178
|
-
throw "regex external not supported yet";
|
|
208
|
+
throw new Error("regex external not supported yet");
|
|
179
209
|
}
|
|
180
210
|
else {
|
|
181
211
|
if ("byLayer" in webpackExternals) {
|
|
182
|
-
throw "by layer external item not supported yet";
|
|
212
|
+
throw new Error("by layer external item not supported yet");
|
|
183
213
|
}
|
|
184
214
|
Object.entries(webpackExternals).forEach(([key, value]) => {
|
|
185
215
|
if (typeof value === "string") {
|
|
@@ -278,7 +308,7 @@ function compatExternals(webpackExternals) {
|
|
|
278
308
|
break;
|
|
279
309
|
}
|
|
280
310
|
case "function": {
|
|
281
|
-
throw "functional external not supported yet";
|
|
311
|
+
throw new Error("functional external not supported yet");
|
|
282
312
|
}
|
|
283
313
|
default:
|
|
284
314
|
break;
|
|
@@ -291,39 +321,72 @@ function compatModule(webpackModule) {
|
|
|
291
321
|
}
|
|
292
322
|
const moduleRules = {
|
|
293
323
|
rules: webpackModule.rules.reduce((acc, cur) => {
|
|
294
|
-
var _a
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
324
|
+
var _a;
|
|
325
|
+
if (typeof cur === "object" && cur) {
|
|
326
|
+
// Normalize condition from test, include, exclude
|
|
327
|
+
let ruleCondition = undefined;
|
|
328
|
+
if (cur.test || cur.include || cur.exclude) {
|
|
329
|
+
const conditions = [];
|
|
330
|
+
if (cur.test)
|
|
331
|
+
conditions.push({ path: cur.test });
|
|
332
|
+
if (cur.include)
|
|
333
|
+
conditions.push({ path: cur.include });
|
|
334
|
+
if (cur.exclude)
|
|
335
|
+
conditions.push({ not: { path: cur.exclude } });
|
|
336
|
+
ruleCondition =
|
|
337
|
+
conditions.length === 1 ? conditions[0] : { all: conditions };
|
|
338
|
+
}
|
|
339
|
+
// Determine the glob keys for the rules record
|
|
340
|
+
// utoo usually uses extensions as keys like "*.svg"
|
|
341
|
+
const extensions = (_a = cur.test) === null || _a === void 0 ? void 0 : _a.toString().match(/\.(\w+)/g);
|
|
342
|
+
const globKeys = extensions
|
|
343
|
+
? extensions.map((ext) => `*${ext}`)
|
|
344
|
+
: ["*"];
|
|
345
|
+
// Handle loaders
|
|
346
|
+
const loaders = typeof cur.use === "string"
|
|
347
|
+
? [{ loader: cur.use, options: {} }]
|
|
348
|
+
: Array.isArray(cur.use)
|
|
349
|
+
? cur.use.map((use) => typeof use === "string"
|
|
350
|
+
? { loader: use, options: {} }
|
|
351
|
+
: {
|
|
352
|
+
loader: use.loader,
|
|
353
|
+
options: use.options || {},
|
|
354
|
+
})
|
|
355
|
+
: cur.loader
|
|
356
|
+
? [
|
|
357
|
+
{
|
|
358
|
+
loader: cur.loader,
|
|
359
|
+
options: cur.options || {},
|
|
320
360
|
},
|
|
321
|
-
|
|
361
|
+
]
|
|
362
|
+
: [];
|
|
363
|
+
const ruleItem = {
|
|
364
|
+
loaders,
|
|
365
|
+
condition: ruleCondition,
|
|
366
|
+
as: cur.type === "asset" ||
|
|
367
|
+
cur.type === "asset/resource" ||
|
|
368
|
+
cur.type === "asset/inline" ||
|
|
369
|
+
cur.type === "asset/source" ||
|
|
370
|
+
(loaders.length > 0 &&
|
|
371
|
+
!globKeys.some((k) => k === "*.js" || k === "*.ts" || k === "*.tsx"))
|
|
372
|
+
? "*.js"
|
|
373
|
+
: undefined,
|
|
374
|
+
};
|
|
375
|
+
for (const key of globKeys) {
|
|
376
|
+
const existing = acc[key];
|
|
377
|
+
if (!existing) {
|
|
378
|
+
acc[key] = ruleItem;
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
// If already exists, turn into a collection array
|
|
382
|
+
if (Array.isArray(existing)) {
|
|
383
|
+
existing.push(ruleItem);
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
acc[key] = [existing, ruleItem];
|
|
322
387
|
}
|
|
323
388
|
}
|
|
324
|
-
|
|
325
|
-
default:
|
|
326
|
-
break;
|
|
389
|
+
}
|
|
327
390
|
}
|
|
328
391
|
return acc;
|
|
329
392
|
}, {}),
|
|
@@ -343,10 +406,10 @@ function compatResolve(webpackResolve) {
|
|
|
343
406
|
if (typeof v === "string") {
|
|
344
407
|
// Handle alias keys ending with $ by removing the $
|
|
345
408
|
const aliasKey = k.endsWith("$") ? k.slice(0, -1) : k;
|
|
346
|
-
|
|
409
|
+
acc[aliasKey] = v;
|
|
347
410
|
}
|
|
348
411
|
else {
|
|
349
|
-
throw "non string alias value not supported yet";
|
|
412
|
+
throw new Error("non string alias value not supported yet");
|
|
350
413
|
}
|
|
351
414
|
return acc;
|
|
352
415
|
}, {})
|
|
@@ -354,24 +417,38 @@ function compatResolve(webpackResolve) {
|
|
|
354
417
|
extensions,
|
|
355
418
|
};
|
|
356
419
|
}
|
|
420
|
+
function normalizeWebpackHash(filename) {
|
|
421
|
+
if (!filename)
|
|
422
|
+
return filename;
|
|
423
|
+
// Replace [hash] and [chunkhash] with [contenthash] as utoo only supports contenthash
|
|
424
|
+
// Also remove [ext] as it is automatically appended in utoopack
|
|
425
|
+
return filename
|
|
426
|
+
.replace(/\[(?:hash|chunkhash)(?::(\d+))?\]/g, "[contenthash:$1]")
|
|
427
|
+
.replace(/\[ext\]/g, "");
|
|
428
|
+
}
|
|
357
429
|
function compatOutput(webpackOutput) {
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
typeof webpackOutput
|
|
367
|
-
|
|
430
|
+
for (const field of [
|
|
431
|
+
"filename",
|
|
432
|
+
"chunkFilename",
|
|
433
|
+
"publicPath",
|
|
434
|
+
"cssFilename",
|
|
435
|
+
"cssChunkFilename",
|
|
436
|
+
"assetModuleFilename",
|
|
437
|
+
]) {
|
|
438
|
+
if ((webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput[field]) && typeof webpackOutput[field] !== "string") {
|
|
439
|
+
throw new Error(`non string output ${field} not supported yet`);
|
|
440
|
+
}
|
|
368
441
|
}
|
|
369
442
|
return {
|
|
370
443
|
path: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.path,
|
|
371
|
-
filename: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.filename,
|
|
372
|
-
chunkFilename: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.chunkFilename,
|
|
444
|
+
filename: normalizeWebpackHash(webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.filename),
|
|
445
|
+
chunkFilename: normalizeWebpackHash(webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.chunkFilename),
|
|
446
|
+
cssFilename: normalizeWebpackHash(webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.cssFilename),
|
|
447
|
+
cssChunkFilename: normalizeWebpackHash(webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.cssChunkFilename),
|
|
448
|
+
assetModuleFilename: normalizeWebpackHash(webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.assetModuleFilename),
|
|
373
449
|
clean: !!(webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.clean),
|
|
374
450
|
publicPath: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.publicPath,
|
|
451
|
+
chunkLoadingGlobal: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.chunkLoadingGlobal,
|
|
375
452
|
};
|
|
376
453
|
}
|
|
377
454
|
function compatTarget(webpackTarget) {
|
|
@@ -388,15 +465,19 @@ function compatOptimization(webpackOptimization) {
|
|
|
388
465
|
if (!webpackOptimization) {
|
|
389
466
|
return;
|
|
390
467
|
}
|
|
391
|
-
const { moduleIds, minimize, concatenateModules } = webpackOptimization;
|
|
468
|
+
const { moduleIds, minimize, concatenateModules, usedExports } = webpackOptimization;
|
|
392
469
|
return {
|
|
393
470
|
moduleIds: moduleIds === "named"
|
|
394
471
|
? "named"
|
|
395
472
|
: moduleIds === "deterministic"
|
|
396
473
|
? "deterministic"
|
|
397
474
|
: undefined,
|
|
475
|
+
noMangling: webpackOptimization.mangleExports === false,
|
|
398
476
|
minify: minimize,
|
|
399
477
|
concatenateModules,
|
|
478
|
+
treeShaking: !!usedExports,
|
|
479
|
+
removeUnusedExports: !!usedExports,
|
|
480
|
+
removeUnusedImports: !!usedExports,
|
|
400
481
|
};
|
|
401
482
|
}
|
|
402
483
|
function compatStats(webpackStats) {
|