@utoo/pack 0.0.1-alpha.18 → 0.0.1-alpha.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.
@@ -1,338 +0,0 @@
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
- stats: compatStats(stats),
16
- },
17
- buildId: webpackConfig.name,
18
- };
19
- }
20
- function compatMode(webpackMode) {
21
- return webpackMode
22
- ? webpackMode === "none"
23
- ? "production"
24
- : webpackMode
25
- : "production";
26
- }
27
- function compatEntry(webpackEntry) {
28
- const entry = [];
29
- switch (typeof webpackEntry) {
30
- case "string":
31
- entry.push({ import: webpackEntry });
32
- break;
33
- case "object":
34
- if (Array.isArray(webpackEntry)) {
35
- webpackEntry.forEach((e) => entry.push({
36
- import: e,
37
- }));
38
- }
39
- else {
40
- Object.entries(webpackEntry).forEach(([k, v]) => {
41
- var _a;
42
- switch (typeof v) {
43
- case "string":
44
- entry.push({ name: k, import: v });
45
- break;
46
- case "object":
47
- if (!Array.isArray(v)) {
48
- switch (typeof v.import) {
49
- case "string":
50
- entry.push({
51
- name: k,
52
- import: v.import,
53
- library: ((_a = v.library) === null || _a === void 0 ? void 0 : _a.type) === "umd"
54
- ? {
55
- name: typeof v.library.name === "string"
56
- ? v.library.name
57
- : undefined,
58
- export: typeof v.library.export === "string"
59
- ? [v.library.export]
60
- : v.library.export,
61
- }
62
- : undefined,
63
- });
64
- break;
65
- default:
66
- break;
67
- }
68
- }
69
- else {
70
- throw "multi entry items for one entry not supported yet";
71
- }
72
- break;
73
- default:
74
- throw "non string and non object entry path not supported yet";
75
- }
76
- });
77
- }
78
- break;
79
- case "function":
80
- throw "functional entry not supported yet";
81
- default:
82
- throw "entry config not compatible now";
83
- }
84
- return entry;
85
- }
86
- function compatFromWebpackPlugin(webpackPlugins, picker) {
87
- const plugin = webpackPlugins === null || webpackPlugins === void 0 ? void 0 : webpackPlugins.find((p) => p && typeof p === "object" && p.constructor.name === picker.pluginName);
88
- return picker(plugin);
89
- }
90
- compatDefine.pluginName = "DefinePlugin";
91
- function compatDefine(maybeWebpackPluginInstance) {
92
- return maybeWebpackPluginInstance === null || maybeWebpackPluginInstance === void 0 ? void 0 : maybeWebpackPluginInstance.definitions;
93
- }
94
- function compatExternals(webpackExternals) {
95
- if (!webpackExternals) {
96
- return undefined;
97
- }
98
- let externals = {};
99
- switch (typeof webpackExternals) {
100
- case "string": {
101
- // Single string external: "lodash" -> { "lodash": "lodash" }
102
- externals[webpackExternals] = webpackExternals;
103
- break;
104
- }
105
- case "object": {
106
- if (Array.isArray(webpackExternals)) {
107
- // Array of externals: ["lodash", "react"] -> { "lodash": "lodash", "react": "react" }
108
- externals = webpackExternals.reduce((acc, external) => {
109
- if (typeof external === "string") {
110
- acc[external] = external;
111
- }
112
- else if (typeof external === "object" && external !== null) {
113
- Object.assign(acc, compatExternals(external));
114
- }
115
- return acc;
116
- }, {});
117
- }
118
- else if (webpackExternals instanceof RegExp) {
119
- throw "regex external not supported yet";
120
- }
121
- else {
122
- if ("byLayer" in webpackExternals) {
123
- throw "by layer external item not supported yet";
124
- }
125
- Object.entries(webpackExternals).forEach(([key, value]) => {
126
- if (typeof value === "string") {
127
- // Check if it's a script type with shorthand syntax: "global@https://example.com/script.js"
128
- if (value.includes("@") &&
129
- (value.startsWith("script ") || value.includes("://"))) {
130
- const match = value.match(/^(?:script\s+)?(.+?)@(.+)$/);
131
- if (match) {
132
- const [, globalName, scriptUrl] = match;
133
- // Use utoo-pack string format: "script globalName@url"
134
- externals[key] = `script ${globalName}@${scriptUrl}`;
135
- }
136
- else {
137
- externals[key] = value;
138
- }
139
- }
140
- else {
141
- // Simple string mapping: { "react": "React" }
142
- externals[key] = value;
143
- }
144
- }
145
- else if (Array.isArray(value)) {
146
- // Array format handling
147
- if (value.length >= 2) {
148
- const [first, second] = value;
149
- // Check if it's a script type array: ["https://example.com/script.js", "GlobalName"]
150
- if (typeof first === "string" &&
151
- first.includes("://") &&
152
- typeof second === "string") {
153
- // Use utoo-pack object format for script
154
- externals[key] = {
155
- root: second,
156
- type: "script",
157
- script: first,
158
- };
159
- }
160
- else if (typeof first === "string" &&
161
- typeof second === "string") {
162
- // Handle type prefix formats
163
- if (first.startsWith("commonjs")) {
164
- externals[key] = `commonjs ${second}`;
165
- }
166
- else if (first === "module") {
167
- externals[key] = `esm ${second}`;
168
- }
169
- else if (first === "var" ||
170
- first === "global" ||
171
- first === "window") {
172
- externals[key] = second;
173
- }
174
- else if (first === "script") {
175
- // Script type without URL in array format - treat as regular script prefix
176
- externals[key] = `script ${second}`;
177
- }
178
- else {
179
- externals[key] = `${first} ${second}`;
180
- }
181
- }
182
- else {
183
- externals[key] = value[0] || key;
184
- }
185
- }
186
- else {
187
- externals[key] = value[0] || key;
188
- }
189
- }
190
- else if (typeof value === "object" && value !== null) {
191
- // Object format: handle complex configurations
192
- if ("root" in value || "commonjs" in value || "amd" in value) {
193
- // Standard webpack externals object format
194
- if (value.commonjs) {
195
- externals[key] = `commonjs ${value.commonjs}`;
196
- }
197
- else if (value.root) {
198
- externals[key] = value.root;
199
- }
200
- else if (value.amd) {
201
- externals[key] = value.amd;
202
- }
203
- else {
204
- externals[key] = key;
205
- }
206
- }
207
- else {
208
- // Treat as utoo-pack specific configuration (might already be in correct format)
209
- externals[key] = value;
210
- }
211
- }
212
- else {
213
- // Fallback to key name
214
- externals[key] = key;
215
- }
216
- });
217
- }
218
- break;
219
- }
220
- case "function": {
221
- throw "functional external not supported yet";
222
- }
223
- default:
224
- break;
225
- }
226
- return externals;
227
- }
228
- function compatModule(webpackModule) {
229
- if (!Array.isArray(webpackModule === null || webpackModule === void 0 ? void 0 : webpackModule.rules)) {
230
- return;
231
- }
232
- const moduleRules = {
233
- rules: webpackModule.rules.reduce((acc, cur) => {
234
- var _a, _b;
235
- switch (typeof cur) {
236
- case "object":
237
- if (cur) {
238
- let condition = (_b = (_a = cur.test) === null || _a === void 0 ? void 0 : _a.toString().match(/(\.\w+)/)) === null || _b === void 0 ? void 0 : _b[1];
239
- if (condition) {
240
- Object.assign(acc, {
241
- ["*" + condition]: {
242
- loaders: typeof cur.use === "string"
243
- ? [cur.use]
244
- : typeof (cur === null || cur === void 0 ? void 0 : cur.use) === "object"
245
- ? Array.isArray(cur.use)
246
- ? cur.use.map((use) => typeof use === "string"
247
- ? { loader: use, options: {} }
248
- : {
249
- loader: use.loader,
250
- options: use.options || {},
251
- })
252
- : [
253
- {
254
- loader: cur.loader,
255
- options: cur.options || {},
256
- },
257
- ]
258
- : [],
259
- as: "*.js",
260
- },
261
- });
262
- }
263
- }
264
- break;
265
- default:
266
- break;
267
- }
268
- return acc;
269
- }, {}),
270
- };
271
- return moduleRules;
272
- }
273
- function compatResolve(webpackResolve) {
274
- if (!webpackResolve) {
275
- return;
276
- }
277
- const { alias, extensions } = webpackResolve;
278
- return {
279
- alias: alias
280
- ? Array.isArray(alias)
281
- ? alias.reduce((acc, cur) => Object.assign(acc, { [cur.name]: cur.alias }), {})
282
- : Object.entries(alias).reduce((acc, [k, v]) => {
283
- if (typeof v === "string") {
284
- Object.assign(acc, { [k]: v });
285
- }
286
- else {
287
- throw "non string alias value not supported yet";
288
- }
289
- return acc;
290
- }, {})
291
- : undefined,
292
- extensions,
293
- };
294
- }
295
- function compatOutput(webpackOutput) {
296
- if ((webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.filename) && typeof webpackOutput.filename !== "string") {
297
- throw "non string output filename not supported yet";
298
- }
299
- if ((webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.chunkFilename) &&
300
- typeof webpackOutput.chunkFilename !== "string") {
301
- throw "non string output chunkFilename not supported yet";
302
- }
303
- return {
304
- path: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.path,
305
- filename: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.filename,
306
- chunkFilename: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.chunkFilename,
307
- clean: !!(webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.clean),
308
- };
309
- }
310
- function compatTarget(webpackTarget) {
311
- return webpackTarget
312
- ? Array.isArray(webpackTarget)
313
- ? webpackTarget.join(" ")
314
- : webpackTarget
315
- : undefined;
316
- }
317
- function compatSourceMaps(webpackSourceMaps) {
318
- return !!webpackSourceMaps;
319
- }
320
- function compatOptimization(webpackOptimization) {
321
- if (!webpackOptimization) {
322
- return;
323
- }
324
- const { moduleIds, minimize,
325
- // TODO: concatenateModules to be supported, need to upgrade to next.js@15.4
326
- } = webpackOptimization;
327
- return {
328
- moduleIds: moduleIds === "named"
329
- ? "named"
330
- : moduleIds === "deterministic"
331
- ? "deterministic"
332
- : undefined,
333
- minify: minimize,
334
- };
335
- }
336
- function compatStats(webpackStats) {
337
- return !!webpackStats;
338
- }