@utoo/web 1.3.0 → 1.3.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/loaderWorker.js +1 -1
- package/esm/project/Project.d.ts +15 -0
- package/esm/project/Project.js +4 -0
- package/esm/project/checkCompatibility.d.ts +15 -0
- package/esm/project/checkCompatibility.js +31 -0
- package/esm/utoo/index.d.ts +32 -1
- package/esm/utoo/index.js +212 -58
- package/esm/utoo/index_bg.wasm +0 -0
- package/esm/webpackLoaders/cjs.js +107 -141
- package/esm/webpackLoaders/polyfills/{fsPolyfill.d.ts → fsPolyfill/index.d.ts} +3 -3
- package/esm/webpackLoaders/polyfills/fsPolyfill/index.js +384 -0
- package/esm/webpackLoaders/polyfills/{fsPromisesPolyfill.d.ts → fsPolyfill/promises.d.ts} +1 -1
- package/esm/webpackLoaders/polyfills/fsPolyfill/promises.js +159 -0
- package/esm/webpackLoaders/polyfills/fsPolyfill/utils.d.ts +3 -0
- package/esm/webpackLoaders/polyfills/fsPolyfill/utils.js +91 -0
- package/package.json +1 -1
- package/esm/webpackLoaders/polyfills/fsPolyfill.js +0 -291
- package/esm/webpackLoaders/polyfills/fsPromisesPolyfill.js +0 -113
|
@@ -7,6 +7,34 @@ const statCache = {};
|
|
|
7
7
|
const pkgJsonCache = {};
|
|
8
8
|
const resolutionCache = {};
|
|
9
9
|
const searchPathsCache = {};
|
|
10
|
+
const RESOLUTION_EXTENSIONS = [
|
|
11
|
+
"",
|
|
12
|
+
".js",
|
|
13
|
+
".cjs",
|
|
14
|
+
".json",
|
|
15
|
+
"/index.js",
|
|
16
|
+
"/index.cjs",
|
|
17
|
+
"/index.json",
|
|
18
|
+
];
|
|
19
|
+
const tryReadFile = (p) => {
|
|
20
|
+
try {
|
|
21
|
+
return fs.readFileSync(p, "utf8");
|
|
22
|
+
}
|
|
23
|
+
catch (_a) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const resolveWithExtensions = (p, skipIndexJsIfJs = false) => {
|
|
28
|
+
for (const ext of RESOLUTION_EXTENSIONS) {
|
|
29
|
+
if (skipIndexJsIfJs && ext === "/index.js" && p.endsWith(".js"))
|
|
30
|
+
continue;
|
|
31
|
+
const candidate = p + ext;
|
|
32
|
+
const code = tryReadFile(candidate);
|
|
33
|
+
if (code !== null)
|
|
34
|
+
return { code, id: candidate };
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
};
|
|
10
38
|
const executeModule = (moduleCode, moduleId, id, importMaps, entrypoint) => {
|
|
11
39
|
if (installedModules[moduleId]) {
|
|
12
40
|
return installedModules[moduleId].exports;
|
|
@@ -19,7 +47,6 @@ const executeModule = (moduleCode, moduleId, id, importMaps, entrypoint) => {
|
|
|
19
47
|
if (moduleId.includes("node_modules")) {
|
|
20
48
|
installedModules[moduleId] = module;
|
|
21
49
|
}
|
|
22
|
-
// Hack for entrypoint
|
|
23
50
|
if (moduleId === entrypoint) {
|
|
24
51
|
moduleCode = "self.Buffer = require('buffer').Buffer;" + moduleCode;
|
|
25
52
|
}
|
|
@@ -44,15 +71,14 @@ const executeModule = (moduleCode, moduleId, id, importMaps, entrypoint) => {
|
|
|
44
71
|
console.warn = (...args) => {
|
|
45
72
|
var _a;
|
|
46
73
|
const msg = ((_a = args[0]) === null || _a === void 0 ? void 0 : _a.toString()) || "";
|
|
47
|
-
if (msg.includes("(SystemJS Error#W3")) {
|
|
48
|
-
|
|
74
|
+
if (!msg.includes("(SystemJS Error#W3")) {
|
|
75
|
+
originalWarn.apply(console, args);
|
|
49
76
|
}
|
|
50
|
-
originalWarn.apply(console, args);
|
|
51
77
|
};
|
|
52
78
|
try {
|
|
53
79
|
System.set(moduleId, { default: finalExports });
|
|
54
80
|
}
|
|
55
|
-
catch (
|
|
81
|
+
catch (_a) {
|
|
56
82
|
// ignore
|
|
57
83
|
}
|
|
58
84
|
finally {
|
|
@@ -61,47 +87,52 @@ const executeModule = (moduleCode, moduleId, id, importMaps, entrypoint) => {
|
|
|
61
87
|
return finalExports;
|
|
62
88
|
};
|
|
63
89
|
const loadModule = (id, context, importMaps, entrypoint) => {
|
|
90
|
+
var _a;
|
|
64
91
|
const cacheKey = `${context}:${id}`;
|
|
65
92
|
if (resolutionCache[cacheKey]) {
|
|
66
93
|
const cachedId = resolutionCache[cacheKey];
|
|
67
|
-
if (installedModules[cachedId])
|
|
94
|
+
if (installedModules[cachedId])
|
|
68
95
|
return installedModules[cachedId].exports;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const moduleCode = fs.readFileSync(cachedId, "utf8");
|
|
73
|
-
return executeModule(moduleCode, cachedId, id, importMaps, entrypoint);
|
|
74
|
-
}
|
|
75
|
-
catch (e) {
|
|
76
|
-
// If read fails, fall back to full resolution
|
|
96
|
+
const code = tryReadFile(cachedId);
|
|
97
|
+
if (code !== null) {
|
|
98
|
+
return executeModule(code, cachedId, id, importMaps, entrypoint);
|
|
77
99
|
}
|
|
78
100
|
}
|
|
79
101
|
// 1. Resolve
|
|
80
|
-
let resolvedId = id;
|
|
81
|
-
if (id.startsWith(".")) {
|
|
82
|
-
resolvedId = path.join(context, id);
|
|
83
|
-
}
|
|
102
|
+
let resolvedId = id.startsWith(".") ? path.join(context, id) : id;
|
|
84
103
|
// 2. Check Cache (SystemJS)
|
|
85
|
-
|
|
86
|
-
if (
|
|
87
|
-
return
|
|
88
|
-
if (id !== resolvedId) {
|
|
89
|
-
dependency = System.get(id);
|
|
90
|
-
if (dependency)
|
|
91
|
-
return dependency.default;
|
|
92
|
-
}
|
|
104
|
+
const sysDef = System.get(resolvedId) || (id !== resolvedId && System.get(id));
|
|
105
|
+
if (sysDef)
|
|
106
|
+
return sysDef.default;
|
|
93
107
|
// 3. Check Node Polyfills
|
|
94
|
-
if (id in nodePolyFills)
|
|
95
|
-
// @ts-ignore
|
|
108
|
+
if (id in nodePolyFills)
|
|
96
109
|
return nodePolyFills[id];
|
|
97
|
-
|
|
98
|
-
if (resolvedId in nodePolyFills) {
|
|
99
|
-
// @ts-ignore
|
|
110
|
+
if (resolvedId in nodePolyFills)
|
|
100
111
|
return nodePolyFills[resolvedId];
|
|
101
|
-
}
|
|
102
112
|
// 4. Check importMaps & FS
|
|
103
113
|
let moduleCode = importMaps[resolvedId] || importMaps[id];
|
|
104
114
|
let moduleId = importMaps[resolvedId] ? resolvedId : id;
|
|
115
|
+
if (!moduleCode) {
|
|
116
|
+
let longestMatch = null;
|
|
117
|
+
for (const key in importMaps) {
|
|
118
|
+
const sanitized = key.replace(/^\.?\//, "");
|
|
119
|
+
if (!sanitized)
|
|
120
|
+
continue;
|
|
121
|
+
const isMatch = [id, resolvedId].some((p) => p.startsWith("/") &&
|
|
122
|
+
p.endsWith(sanitized) &&
|
|
123
|
+
p[p.length - sanitized.length - 1] === "/");
|
|
124
|
+
if (isMatch && (!longestMatch || key.length > longestMatch.key.length)) {
|
|
125
|
+
longestMatch = { key, sanitized };
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (longestMatch) {
|
|
129
|
+
moduleCode = importMaps[longestMatch.key];
|
|
130
|
+
moduleId =
|
|
131
|
+
id.startsWith("/") && id.endsWith(longestMatch.sanitized)
|
|
132
|
+
? id
|
|
133
|
+
: resolvedId;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
105
136
|
// Fallback: Try resolving from node_modules
|
|
106
137
|
if (!moduleCode && !id.startsWith(".") && !id.startsWith("/")) {
|
|
107
138
|
let searchPaths = searchPathsCache[context];
|
|
@@ -110,140 +141,76 @@ const loadModule = (id, context, importMaps, entrypoint) => {
|
|
|
110
141
|
let currentDir = context;
|
|
111
142
|
while (true) {
|
|
112
143
|
if (path.basename(currentDir) !== "node_modules") {
|
|
113
|
-
const
|
|
114
|
-
if (!searchPaths.includes(
|
|
115
|
-
searchPaths.push(
|
|
116
|
-
}
|
|
144
|
+
const nmPath = path.join(currentDir, "node_modules");
|
|
145
|
+
if (!searchPaths.includes(nmPath))
|
|
146
|
+
searchPaths.push(nmPath);
|
|
117
147
|
}
|
|
118
148
|
const parent = path.dirname(currentDir);
|
|
119
149
|
if (parent === currentDir)
|
|
120
150
|
break;
|
|
121
151
|
currentDir = parent;
|
|
122
152
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
if (!searchPaths.includes(cwdNodeModules)) {
|
|
127
|
-
searchPaths.push(cwdNodeModules);
|
|
128
|
-
}
|
|
153
|
+
const cwdNm = path.join(nodePolyFills.process.cwd(), "node_modules");
|
|
154
|
+
if (!searchPaths.includes(cwdNm))
|
|
155
|
+
searchPaths.push(cwdNm);
|
|
129
156
|
searchPathsCache[context] = searchPaths;
|
|
130
157
|
}
|
|
131
158
|
for (const nodeModulesDir of searchPaths) {
|
|
132
159
|
const nodeModulesPath = path.join(nodeModulesDir, id);
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
let
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
catch (e) {
|
|
142
|
-
// ignore
|
|
143
|
-
}
|
|
144
|
-
if (pkg) {
|
|
145
|
-
try {
|
|
146
|
-
const mainField = pkg.main;
|
|
147
|
-
if (mainField) {
|
|
148
|
-
const candidates = [
|
|
149
|
-
path.resolve(nodeModulesPath, mainField),
|
|
150
|
-
path.resolve(nodeModulesPath, mainField) + ".js",
|
|
151
|
-
path.resolve(nodeModulesPath, mainField) + ".json",
|
|
152
|
-
path.resolve(nodeModulesPath, mainField, "index.js"),
|
|
153
|
-
];
|
|
154
|
-
for (const candidate of candidates) {
|
|
155
|
-
try {
|
|
156
|
-
// Try reading directly to bypass potential statSync issues
|
|
157
|
-
moduleCode = fs.readFileSync(candidate, "utf8");
|
|
158
|
-
resolvedId = candidate;
|
|
159
|
-
moduleId = candidate;
|
|
160
|
-
break;
|
|
161
|
-
}
|
|
162
|
-
catch (e) {
|
|
163
|
-
// ignore
|
|
164
|
-
}
|
|
165
|
-
}
|
|
160
|
+
const pkgPath = path.join(nodeModulesPath, "package.json");
|
|
161
|
+
// Check package.json main
|
|
162
|
+
let pkgMain;
|
|
163
|
+
if (!pkgJsonCache[pkgPath]) {
|
|
164
|
+
const json = tryReadFile(pkgPath);
|
|
165
|
+
if (json) {
|
|
166
|
+
try {
|
|
167
|
+
pkgJsonCache[pkgPath] = JSON.parse(json);
|
|
166
168
|
}
|
|
167
|
-
|
|
168
|
-
catch (e) {
|
|
169
|
-
// ignore
|
|
169
|
+
catch (_b) { }
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
|
-
if (
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
resolvedId = p;
|
|
180
|
-
moduleId = p;
|
|
181
|
-
break;
|
|
182
|
-
}
|
|
183
|
-
catch (e) {
|
|
184
|
-
// ignore
|
|
185
|
-
}
|
|
172
|
+
if ((_a = pkgJsonCache[pkgPath]) === null || _a === void 0 ? void 0 : _a.main) {
|
|
173
|
+
pkgMain = path.resolve(nodeModulesPath, pkgJsonCache[pkgPath].main);
|
|
174
|
+
const res = resolveWithExtensions(pkgMain);
|
|
175
|
+
if (res) {
|
|
176
|
+
moduleCode = res.code;
|
|
177
|
+
moduleId = resolvedId = res.id;
|
|
178
|
+
break;
|
|
186
179
|
}
|
|
187
180
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
if (!moduleCode && id.startsWith("/")) {
|
|
194
|
-
const extensions = ["", ".js", ".json", "/index.js"];
|
|
195
|
-
for (const ext of extensions) {
|
|
196
|
-
if (ext === "/index.js" && id.endsWith(".js"))
|
|
197
|
-
continue;
|
|
198
|
-
const p = id + ext;
|
|
199
|
-
try {
|
|
200
|
-
moduleCode = fs.readFileSync(p, "utf8");
|
|
201
|
-
resolvedId = p;
|
|
202
|
-
moduleId = id;
|
|
181
|
+
// Check direct file/directory
|
|
182
|
+
const res = resolveWithExtensions(nodeModulesPath);
|
|
183
|
+
if (res) {
|
|
184
|
+
moduleCode = res.code;
|
|
185
|
+
moduleId = resolvedId = res.id;
|
|
203
186
|
break;
|
|
204
187
|
}
|
|
205
|
-
catch (e) {
|
|
206
|
-
// ignore
|
|
207
|
-
}
|
|
208
188
|
}
|
|
209
189
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
const
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const p = resolvedId + ext;
|
|
217
|
-
try {
|
|
218
|
-
moduleCode = fs.readFileSync(p, "utf8");
|
|
219
|
-
resolvedId = p;
|
|
220
|
-
moduleId = p;
|
|
221
|
-
break;
|
|
222
|
-
}
|
|
223
|
-
catch (e) {
|
|
224
|
-
// ignore
|
|
225
|
-
}
|
|
190
|
+
// Fallback: Try resolving absolute or relative path
|
|
191
|
+
if (!moduleCode && (id.startsWith("/") || id.startsWith("."))) {
|
|
192
|
+
const res = resolveWithExtensions(resolvedId, id.endsWith(".js"));
|
|
193
|
+
if (res) {
|
|
194
|
+
moduleCode = res.code;
|
|
195
|
+
moduleId = resolvedId = res.id;
|
|
226
196
|
}
|
|
227
197
|
}
|
|
228
198
|
if (moduleCode) {
|
|
229
|
-
|
|
230
|
-
resolutionCache[cacheKey] = moduleId;
|
|
199
|
+
resolutionCache[`${context}:${id}`] = moduleId;
|
|
231
200
|
return executeModule(moduleCode, moduleId, id, importMaps, entrypoint);
|
|
232
201
|
}
|
|
233
|
-
const error = new Error(`Worker: Dependency ${id} (resolved: ${resolvedId}) not found. Context: ${context}`
|
|
202
|
+
const error = new Error(`Worker: Dependency ${id} (resolved: ${resolvedId}) not found. Context: ${context}. ` +
|
|
203
|
+
`ImportMaps count: ${Object.keys(importMaps).length}. ` +
|
|
204
|
+
`CWD: ${nodePolyFills.process.cwd()}. ` +
|
|
205
|
+
`Sample ImportMaps keys: ${Object.keys(importMaps).slice(0, 5).join(", ")}`);
|
|
234
206
|
console.error(error);
|
|
235
207
|
throw error;
|
|
236
208
|
};
|
|
237
209
|
export async function cjs(entrypoint, importMaps) {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
delete pkgJsonCache[key];
|
|
243
|
-
for (const key in resolutionCache)
|
|
244
|
-
delete resolutionCache[key];
|
|
245
|
-
for (const key in searchPathsCache)
|
|
246
|
-
delete searchPathsCache[key];
|
|
210
|
+
[statCache, pkgJsonCache, resolutionCache, searchPathsCache].forEach((c) => {
|
|
211
|
+
for (const key in c)
|
|
212
|
+
delete c[key];
|
|
213
|
+
});
|
|
247
214
|
await Promise.all(Object.entries(importMaps).map(async ([k, v]) => {
|
|
248
215
|
if (v.startsWith("https://")) {
|
|
249
216
|
try {
|
|
@@ -252,18 +219,17 @@ export async function cjs(entrypoint, importMaps) {
|
|
|
252
219
|
importMaps[k] = await response.text();
|
|
253
220
|
}
|
|
254
221
|
else {
|
|
255
|
-
console.error(`Failed to fetch loader '${k}'
|
|
222
|
+
console.error(`Failed to fetch loader '${k}': ${response.status}`);
|
|
256
223
|
delete importMaps[k];
|
|
257
224
|
}
|
|
258
225
|
}
|
|
259
226
|
catch (error) {
|
|
260
|
-
console.error(`Error fetching loader '${k}'
|
|
227
|
+
console.error(`Error fetching loader '${k}':`, error);
|
|
261
228
|
delete importMaps[k];
|
|
262
229
|
}
|
|
263
230
|
}
|
|
264
231
|
}));
|
|
265
232
|
// @ts-ignore
|
|
266
|
-
// a hack for loader-runner resolving
|
|
267
233
|
self.__systemjs_require__ = (id) => loadModule(id, path.dirname(entrypoint), importMaps, entrypoint);
|
|
268
234
|
// @ts-ignore
|
|
269
235
|
self.__systemjs_require__.resolve = (request) => request;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Buffer } from "buffer";
|
|
2
|
-
import { Stats } from "
|
|
3
|
-
import { promises } from "./
|
|
2
|
+
import { Stats } from "../../../types";
|
|
3
|
+
import { promises } from "./promises";
|
|
4
4
|
export declare function readFileSync(path: string, options: any): string | Buffer<any>;
|
|
5
5
|
export declare function readdirSync(path: string, options?: any): any;
|
|
6
6
|
export declare function writeFileSync(path: string, data: string | Uint8Array, options?: any): void;
|
|
@@ -11,7 +11,7 @@ export declare function copyFileSync(src: string, dst: string): void;
|
|
|
11
11
|
export declare function statSync(p: string): Stats;
|
|
12
12
|
export declare function lstatSync(p: string): Stats;
|
|
13
13
|
export declare function realpathSync(p: string): string;
|
|
14
|
-
export declare function accessSync(
|
|
14
|
+
export declare function accessSync(p: string, mode?: number): void;
|
|
15
15
|
export declare function existsSync(path: string): boolean;
|
|
16
16
|
export declare function readFile(path: string, options: any, cb: Function): void;
|
|
17
17
|
export declare function readdir(path: string, options: any, cb: Function): void;
|