@utoo/pack 1.1.2 → 1.1.3

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.
Files changed (51) hide show
  1. package/README.md +2 -2
  2. package/cjs/index.d.ts +7 -6
  3. package/cjs/index.js +6 -5
  4. package/config_schema.json +1 -1
  5. package/esm/index.d.ts +7 -6
  6. package/esm/index.js +6 -5
  7. package/package.json +19 -18
  8. package/cjs/build.d.ts +0 -3
  9. package/cjs/build.js +0 -82
  10. package/cjs/dev.d.ts +0 -43
  11. package/cjs/dev.js +0 -376
  12. package/cjs/find-root.d.ts +0 -4
  13. package/cjs/find-root.js +0 -75
  14. package/cjs/hmr.d.ts +0 -80
  15. package/cjs/hmr.js +0 -286
  16. package/cjs/loaderWorkerPool.d.ts +0 -1
  17. package/cjs/loaderWorkerPool.js +0 -35
  18. package/cjs/mkcert.d.ts +0 -7
  19. package/cjs/mkcert.js +0 -183
  20. package/cjs/project.d.ts +0 -43
  21. package/cjs/project.js +0 -285
  22. package/cjs/types.d.ts +0 -300
  23. package/cjs/types.js +0 -2
  24. package/cjs/util.d.ts +0 -19
  25. package/cjs/util.js +0 -155
  26. package/cjs/webpackCompat.d.ts +0 -7
  27. package/cjs/webpackCompat.js +0 -408
  28. package/cjs/xcodeProfile.d.ts +0 -1
  29. package/cjs/xcodeProfile.js +0 -16
  30. package/esm/build.d.ts +0 -3
  31. package/esm/build.js +0 -79
  32. package/esm/dev.d.ts +0 -43
  33. package/esm/dev.js +0 -360
  34. package/esm/find-root.d.ts +0 -4
  35. package/esm/find-root.js +0 -66
  36. package/esm/hmr.d.ts +0 -80
  37. package/esm/hmr.js +0 -279
  38. package/esm/loaderWorkerPool.d.ts +0 -1
  39. package/esm/loaderWorkerPool.js +0 -32
  40. package/esm/mkcert.d.ts +0 -7
  41. package/esm/mkcert.js +0 -176
  42. package/esm/project.d.ts +0 -43
  43. package/esm/project.js +0 -247
  44. package/esm/types.d.ts +0 -300
  45. package/esm/types.js +0 -1
  46. package/esm/util.d.ts +0 -19
  47. package/esm/util.js +0 -141
  48. package/esm/webpackCompat.d.ts +0 -7
  49. package/esm/webpackCompat.js +0 -401
  50. package/esm/xcodeProfile.d.ts +0 -1
  51. package/esm/xcodeProfile.js +0 -13
package/esm/util.js DELETED
@@ -1,141 +0,0 @@
1
- import { formatIssue, renderStyledStringToErrorAnsi } from "@utoo/pack-shared";
2
- import path from "path";
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
- }
126
- // ref:
127
- // https://github.com/vercel/next.js/pull/51883
128
- export function blockStdout() {
129
- // rust needs stdout to be blocking, otherwise it will throw an error (on macOS at least) when writing a lot of data (logs) to it
130
- // see https://github.com/napi-rs/napi-rs/issues/1630
131
- // and https://github.com/nodejs/node/blob/main/doc/api/process.md#a-note-on-process-io
132
- if (process.stdout._handle != null) {
133
- process.stdout._handle.setBlocking(true);
134
- }
135
- if (process.stderr._handle != null) {
136
- process.stderr._handle.setBlocking(true);
137
- }
138
- }
139
- export function getPackPath() {
140
- return path.resolve(__dirname, "..");
141
- }
@@ -1,7 +0,0 @@
1
- import type webpack from "webpack";
2
- import { BundleOptions } from "./types";
3
- export declare function readWebpackConfig(projectPath?: string, rootPath?: string): any;
4
- export type WebpackConfig = Partial<Pick<webpack.Configuration, "name" | "entry" | "mode" | "module" | "resolve" | "externals" | "output" | "target" | "devtool" | "optimization" | "plugins" | "stats">> & {
5
- webpackMode: true;
6
- };
7
- export declare function compatOptionsFromWebpack(webpackConfig: WebpackConfig, projectPath?: string, rootPath?: string): BundleOptions;
@@ -1,401 +0,0 @@
1
- import path from "path";
2
- export function readWebpackConfig(projectPath, rootPath) {
3
- const projectPathOutOfRoot = projectPath === undefined
4
- ? process.cwd()
5
- : path.join(rootPath !== null && rootPath !== void 0 ? rootPath : "", projectPath);
6
- const configPath = path.join(projectPathOutOfRoot, "webpack.config.js");
7
- return require(configPath);
8
- }
9
- export function compatOptionsFromWebpack(webpackConfig, projectPath, rootPath) {
10
- const { entry, mode, module, resolve, externals, output, target, devtool, optimization, plugins, stats, } = webpackConfig.entry
11
- ? webpackConfig
12
- : readWebpackConfig(projectPath, rootPath);
13
- return {
14
- config: {
15
- entry: compatEntry(entry),
16
- mode: compatMode(mode),
17
- module: compatModule(module),
18
- resolve: compatResolve(resolve),
19
- externals: compatExternals(externals),
20
- output: compatOutput(output),
21
- target: compatTarget(target),
22
- sourceMaps: compatSourceMaps(devtool),
23
- optimization: compatOptimization(optimization),
24
- define: compatFromWebpackPlugin(plugins, compatDefine),
25
- provider: compatFromWebpackPlugin(plugins, compatProvider),
26
- stats: compatStats(stats),
27
- },
28
- buildId: webpackConfig.name,
29
- };
30
- }
31
- function compatMode(webpackMode) {
32
- return webpackMode
33
- ? webpackMode === "none"
34
- ? "production"
35
- : webpackMode
36
- : "production";
37
- }
38
- function compatEntry(webpackEntry) {
39
- const entry = [];
40
- switch (typeof webpackEntry) {
41
- case "string":
42
- entry.push({ import: webpackEntry });
43
- break;
44
- case "object":
45
- if (Array.isArray(webpackEntry)) {
46
- webpackEntry.forEach((e) => entry.push({
47
- import: e,
48
- }));
49
- }
50
- else {
51
- Object.entries(webpackEntry).forEach(([k, v]) => {
52
- var _a;
53
- switch (typeof v) {
54
- case "string":
55
- entry.push({ name: k, import: v });
56
- break;
57
- case "object":
58
- if (!Array.isArray(v)) {
59
- switch (typeof v.import) {
60
- case "string":
61
- entry.push({
62
- name: k,
63
- import: v.import,
64
- library: ((_a = v.library) === null || _a === void 0 ? void 0 : _a.type) === "umd"
65
- ? {
66
- name: typeof v.library.name === "string"
67
- ? v.library.name
68
- : undefined,
69
- export: typeof v.library.export === "string"
70
- ? [v.library.export]
71
- : v.library.export,
72
- }
73
- : undefined,
74
- });
75
- break;
76
- default:
77
- break;
78
- }
79
- }
80
- else {
81
- if (v.length === 0) {
82
- throw "entry value items is empty";
83
- }
84
- else if (v.length === 1) {
85
- entry.push({ name: k, import: v[0] });
86
- }
87
- else {
88
- throw "multi entry items for one entry not supported yet";
89
- }
90
- }
91
- break;
92
- default:
93
- throw "non string and non object entry path not supported yet";
94
- }
95
- });
96
- }
97
- break;
98
- case "function":
99
- throw "functional entry not supported yet";
100
- default:
101
- throw "entry config not compatible now";
102
- }
103
- return entry;
104
- }
105
- function compatFromWebpackPlugin(webpackPlugins, picker) {
106
- const plugin = webpackPlugins === null || webpackPlugins === void 0 ? void 0 : webpackPlugins.find((p) => p && typeof p === "object" && p.constructor.name === picker.pluginName);
107
- return picker(plugin);
108
- }
109
- compatDefine.pluginName = "DefinePlugin";
110
- function compatDefine(maybeWebpackPluginInstance) {
111
- const definitions = maybeWebpackPluginInstance === null || maybeWebpackPluginInstance === void 0 ? void 0 : maybeWebpackPluginInstance.definitions;
112
- if (!definitions || typeof definitions !== "object") {
113
- return definitions;
114
- }
115
- const processedDefinitions = {};
116
- for (const [key, value] of Object.entries(definitions)) {
117
- if (typeof value === "object" && value !== null) {
118
- processedDefinitions[key] = JSON.stringify(value);
119
- }
120
- else {
121
- processedDefinitions[key] = value;
122
- }
123
- }
124
- return processedDefinitions;
125
- }
126
- compatProvider.pluginName = "ProvidePlugin";
127
- function compatProvider(maybeWebpackPluginInstance) {
128
- const definitions = maybeWebpackPluginInstance === null || maybeWebpackPluginInstance === void 0 ? void 0 : maybeWebpackPluginInstance.definitions;
129
- if (!definitions || typeof definitions !== "object") {
130
- return undefined;
131
- }
132
- const provider = {};
133
- for (const [key, value] of Object.entries(definitions)) {
134
- if (typeof value === "string") {
135
- // Simple module import: { $: 'jquery' }
136
- provider[key] = value;
137
- }
138
- else if (Array.isArray(value)) {
139
- if (value.length === 1) {
140
- // e.g. { process: ['process/browser'] } for default import
141
- provider[key] = value[0];
142
- }
143
- else if (value.length >= 2) {
144
- // Named export import: { Buffer: ['buffer', 'Buffer'] }
145
- provider[key] = [value[0], value[1]];
146
- }
147
- }
148
- }
149
- return Object.keys(provider).length > 0 ? provider : undefined;
150
- }
151
- function compatExternals(webpackExternals) {
152
- if (!webpackExternals) {
153
- return undefined;
154
- }
155
- let externals = {};
156
- switch (typeof webpackExternals) {
157
- case "string": {
158
- // Single string external: "lodash" -> { "lodash": "lodash" }
159
- externals[webpackExternals] = webpackExternals;
160
- break;
161
- }
162
- case "object": {
163
- if (Array.isArray(webpackExternals)) {
164
- // Array of externals: ["lodash", "react"] -> { "lodash": "lodash", "react": "react" }
165
- externals = webpackExternals.reduce((acc, external) => {
166
- if (typeof external === "string") {
167
- acc[external] = external;
168
- }
169
- else if (typeof external === "object" && external !== null) {
170
- Object.assign(acc, compatExternals(external));
171
- }
172
- return acc;
173
- }, {});
174
- }
175
- else if (webpackExternals instanceof RegExp) {
176
- throw "regex external not supported yet";
177
- }
178
- else {
179
- if ("byLayer" in webpackExternals) {
180
- throw "by layer external item not supported yet";
181
- }
182
- Object.entries(webpackExternals).forEach(([key, value]) => {
183
- if (typeof value === "string") {
184
- // Check if it's a script type with shorthand syntax: "global@https://example.com/script.js"
185
- if (value.includes("@") &&
186
- (value.startsWith("script ") || value.includes("://"))) {
187
- const match = value.match(/^(?:script\s+)?(.+?)@(.+)$/);
188
- if (match) {
189
- const [, globalName, scriptUrl] = match;
190
- // Use utoo-pack string format: "script globalName@url"
191
- externals[key] = `script ${globalName}@${scriptUrl}`;
192
- }
193
- else {
194
- externals[key] = value;
195
- }
196
- }
197
- else {
198
- // Simple string mapping: { "react": "React" }
199
- externals[key] = value;
200
- }
201
- }
202
- else if (Array.isArray(value)) {
203
- // Array format handling
204
- if (value.length >= 2) {
205
- const [first, second] = value;
206
- // Check if it's a script type array: ["https://example.com/script.js", "GlobalName"]
207
- if (typeof first === "string" &&
208
- first.includes("://") &&
209
- typeof second === "string") {
210
- // Use utoo-pack object format for script
211
- externals[key] = {
212
- root: second,
213
- type: "script",
214
- script: first,
215
- };
216
- }
217
- else if (typeof first === "string" &&
218
- typeof second === "string") {
219
- // Handle type prefix formats
220
- if (first.startsWith("commonjs")) {
221
- externals[key] = `commonjs ${second}`;
222
- }
223
- else if (first === "module") {
224
- externals[key] = `esm ${second}`;
225
- }
226
- else if (first === "var" ||
227
- first === "global" ||
228
- first === "window") {
229
- externals[key] = second;
230
- }
231
- else if (first === "script") {
232
- // Script type without URL in array format - treat as regular script prefix
233
- externals[key] = `script ${second}`;
234
- }
235
- else {
236
- externals[key] = `${first} ${second}`;
237
- }
238
- }
239
- else {
240
- externals[key] = value[0] || key;
241
- }
242
- }
243
- else {
244
- externals[key] = value[0] || key;
245
- }
246
- }
247
- else if (typeof value === "object" && value !== null) {
248
- // Object format: handle complex configurations
249
- if ("root" in value || "commonjs" in value || "amd" in value) {
250
- // Standard webpack externals object format
251
- if (value.commonjs) {
252
- externals[key] = `commonjs ${value.commonjs}`;
253
- }
254
- else if (value.root) {
255
- externals[key] = value.root;
256
- }
257
- else if (value.amd) {
258
- externals[key] = value.amd;
259
- }
260
- else {
261
- externals[key] = key;
262
- }
263
- }
264
- else {
265
- // Treat as utoo-pack specific configuration (might already be in correct format)
266
- externals[key] = value;
267
- }
268
- }
269
- else {
270
- // Fallback to key name
271
- externals[key] = key;
272
- }
273
- });
274
- }
275
- break;
276
- }
277
- case "function": {
278
- throw "functional external not supported yet";
279
- }
280
- default:
281
- break;
282
- }
283
- return externals;
284
- }
285
- function compatModule(webpackModule) {
286
- if (!Array.isArray(webpackModule === null || webpackModule === void 0 ? void 0 : webpackModule.rules)) {
287
- return;
288
- }
289
- const moduleRules = {
290
- rules: webpackModule.rules.reduce((acc, cur) => {
291
- var _a, _b;
292
- switch (typeof cur) {
293
- case "object":
294
- if (cur) {
295
- let condition = (_b = (_a = cur.test) === null || _a === void 0 ? void 0 : _a.toString().match(/(\.\w+)/)) === null || _b === void 0 ? void 0 : _b[1];
296
- if (condition) {
297
- Object.assign(acc, {
298
- ["*" + condition]: {
299
- loaders: typeof cur.use === "string"
300
- ? [cur.use]
301
- : typeof (cur === null || cur === void 0 ? void 0 : cur.use) === "object"
302
- ? Array.isArray(cur.use)
303
- ? cur.use.map((use) => typeof use === "string"
304
- ? { loader: use, options: {} }
305
- : {
306
- loader: use.loader,
307
- options: use.options || {},
308
- })
309
- : [
310
- {
311
- loader: cur.loader,
312
- options: cur.options || {},
313
- },
314
- ]
315
- : [],
316
- as: "*.js",
317
- },
318
- });
319
- }
320
- }
321
- break;
322
- default:
323
- break;
324
- }
325
- return acc;
326
- }, {}),
327
- };
328
- return moduleRules;
329
- }
330
- function compatResolve(webpackResolve) {
331
- if (!webpackResolve) {
332
- return;
333
- }
334
- const { alias, extensions } = webpackResolve;
335
- return {
336
- alias: alias
337
- ? Array.isArray(alias)
338
- ? alias.reduce((acc, cur) => Object.assign(acc, { [cur.name]: cur.alias }), {})
339
- : Object.entries(alias).reduce((acc, [k, v]) => {
340
- if (typeof v === "string") {
341
- // Handle alias keys ending with $ by removing the $
342
- const aliasKey = k.endsWith("$") ? k.slice(0, -1) : k;
343
- Object.assign(acc, { [aliasKey]: v });
344
- }
345
- else {
346
- throw "non string alias value not supported yet";
347
- }
348
- return acc;
349
- }, {})
350
- : undefined,
351
- extensions,
352
- };
353
- }
354
- function compatOutput(webpackOutput) {
355
- if ((webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.filename) && typeof webpackOutput.filename !== "string") {
356
- throw "non string output filename not supported yet";
357
- }
358
- if ((webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.chunkFilename) &&
359
- typeof webpackOutput.chunkFilename !== "string") {
360
- throw "non string output chunkFilename not supported yet";
361
- }
362
- if ((webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.publicPath) &&
363
- typeof webpackOutput.publicPath !== "string") {
364
- throw "non string output publicPath not supported yet";
365
- }
366
- return {
367
- path: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.path,
368
- filename: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.filename,
369
- chunkFilename: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.chunkFilename,
370
- clean: !!(webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.clean),
371
- publicPath: webpackOutput === null || webpackOutput === void 0 ? void 0 : webpackOutput.publicPath,
372
- };
373
- }
374
- function compatTarget(webpackTarget) {
375
- return webpackTarget
376
- ? Array.isArray(webpackTarget)
377
- ? webpackTarget.join(" ")
378
- : webpackTarget
379
- : undefined;
380
- }
381
- function compatSourceMaps(webpackSourceMaps) {
382
- return !!webpackSourceMaps;
383
- }
384
- function compatOptimization(webpackOptimization) {
385
- if (!webpackOptimization) {
386
- return;
387
- }
388
- const { moduleIds, minimize, concatenateModules } = webpackOptimization;
389
- return {
390
- moduleIds: moduleIds === "named"
391
- ? "named"
392
- : moduleIds === "deterministic"
393
- ? "deterministic"
394
- : undefined,
395
- minify: minimize,
396
- concatenateModules,
397
- };
398
- }
399
- function compatStats(webpackStats) {
400
- return !!webpackStats;
401
- }
@@ -1 +0,0 @@
1
- export declare function xcodeProfilingReady(): Promise<void>;
@@ -1,13 +0,0 @@
1
- export async function xcodeProfilingReady() {
2
- await new Promise((resolve) => {
3
- const readline = require("readline");
4
- const rl = readline.createInterface({
5
- input: process.stdin,
6
- output: process.stdout,
7
- });
8
- rl.question(`Xcode profile enabled. Current process ${process.title} (${process.pid}) . Press Enter to continue...\n`, () => {
9
- rl.close();
10
- resolve();
11
- });
12
- });
13
- }