@awsless/ts-file-cache 0.0.22 → 0.0.23
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/dist/index.cjs +45 -4
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +45 -4
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -20,8 +20,6 @@ const findImports = async (file, code) => {
|
|
|
20
20
|
} });
|
|
21
21
|
return [...importing].map((importee) => {
|
|
22
22
|
if (importee.startsWith(".")) return (0, path.resolve)((0, path.dirname)(file), importee);
|
|
23
|
-
const parts = importee.split("/");
|
|
24
|
-
if (parts.length > 2) return parts.slice(0, 2).join("/");
|
|
25
23
|
return importee;
|
|
26
24
|
});
|
|
27
25
|
};
|
|
@@ -65,6 +63,17 @@ const toAbsolute = (file) => {
|
|
|
65
63
|
};
|
|
66
64
|
//#endregion
|
|
67
65
|
//#region src/hash.ts
|
|
66
|
+
const resolveSubpath = (localPackage, subpath) => {
|
|
67
|
+
const exact = localPackage.exports[subpath];
|
|
68
|
+
if (exact) return exact;
|
|
69
|
+
const patterns = Object.entries(localPackage.exports).filter(([pattern]) => pattern.includes("*")).toSorted(([a], [b]) => b.indexOf("*") - a.indexOf("*"));
|
|
70
|
+
for (const [pattern, target] of patterns) {
|
|
71
|
+
const [prefix, suffix] = pattern.split("*");
|
|
72
|
+
if (subpath.length > prefix.length + suffix.length && subpath.startsWith(prefix) && subpath.endsWith(suffix)) return target.replaceAll("*", subpath.slice(prefix.length, subpath.length - suffix.length));
|
|
73
|
+
}
|
|
74
|
+
if (subpath === ".") return localPackage.main;
|
|
75
|
+
return localPackage.hasExports ? void 0 : (0, path.join)(localPackage.path, subpath);
|
|
76
|
+
};
|
|
68
77
|
const generateRecursiveFileHashes = async (workspace, file, sourceFile, allowedExtensions, hashes) => {
|
|
69
78
|
if (isLocalCodeFile(file)) {
|
|
70
79
|
try {
|
|
@@ -90,8 +99,10 @@ const generateRecursiveFileHashes = async (workspace, file, sourceFile, allowedE
|
|
|
90
99
|
else {
|
|
91
100
|
const localPackage = workspace.packages[dependency.link];
|
|
92
101
|
if (!localPackage) throw new Error(`Can't find the local workspace package for: ${file}`);
|
|
93
|
-
|
|
94
|
-
|
|
102
|
+
const subpath = `.${file.slice(module.length)}`;
|
|
103
|
+
const entry = resolveSubpath(localPackage, subpath);
|
|
104
|
+
if (!entry) throw new Error(`Workspace package doesn't export the import: ${file}`);
|
|
105
|
+
await generateRecursiveFileHashes(workspace, entry, sourceFile, allowedExtensions, hashes);
|
|
95
106
|
}
|
|
96
107
|
return;
|
|
97
108
|
}
|
|
@@ -321,15 +332,45 @@ const buildPackages = async (cwd, importers) => {
|
|
|
321
332
|
const packageJson = await (0, fs_promises.readFile)((0, path.join)(cwd, path$1, "package.json"), "utf8");
|
|
322
333
|
const packageData = JSON.parse(packageJson);
|
|
323
334
|
const entry = packageData.module ?? packageData.main;
|
|
335
|
+
const exports = Object.fromEntries(Object.entries(exportFiles(packageData.exports)).map(([subpath, file]) => [subpath, (0, path.join)(cwd, path$1, file)]));
|
|
324
336
|
packages[(0, path.join)(cwd, path$1)] = {
|
|
325
337
|
name: packageData.name,
|
|
326
338
|
path: (0, path.join)(cwd, path$1),
|
|
327
339
|
main: entry ? (0, path.join)(cwd, path$1, entry) : void 0,
|
|
340
|
+
exports,
|
|
341
|
+
hasExports: packageData.exports !== void 0,
|
|
328
342
|
dependencies
|
|
329
343
|
};
|
|
330
344
|
}));
|
|
331
345
|
return packages;
|
|
332
346
|
};
|
|
347
|
+
const exportFiles = (exports) => {
|
|
348
|
+
const target = (value) => {
|
|
349
|
+
if (typeof value === "string") return value;
|
|
350
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
351
|
+
const conditions = value;
|
|
352
|
+
for (const condition of [
|
|
353
|
+
"bun",
|
|
354
|
+
"import",
|
|
355
|
+
"module",
|
|
356
|
+
"default",
|
|
357
|
+
"require"
|
|
358
|
+
]) {
|
|
359
|
+
const file = target(conditions[condition]);
|
|
360
|
+
if (file) return file;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
if (exports && typeof exports === "object" && !Array.isArray(exports)) {
|
|
365
|
+
const entries = Object.entries(exports);
|
|
366
|
+
if (entries.every(([key]) => key.startsWith("."))) return Object.fromEntries(entries.flatMap(([subpath, value]) => {
|
|
367
|
+
const file = target(value);
|
|
368
|
+
return file ? [[subpath, file]] : [];
|
|
369
|
+
}));
|
|
370
|
+
}
|
|
371
|
+
const file = target(exports);
|
|
372
|
+
return file ? { ".": file } : {};
|
|
373
|
+
};
|
|
333
374
|
//#endregion
|
|
334
375
|
//#region src/index.ts
|
|
335
376
|
const loadWorkspace = async (search) => {
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -19,8 +19,6 @@ const findImports = async (file, code) => {
|
|
|
19
19
|
} });
|
|
20
20
|
return [...importing].map((importee) => {
|
|
21
21
|
if (importee.startsWith(".")) return resolve(dirname(file), importee);
|
|
22
|
-
const parts = importee.split("/");
|
|
23
|
-
if (parts.length > 2) return parts.slice(0, 2).join("/");
|
|
24
22
|
return importee;
|
|
25
23
|
});
|
|
26
24
|
};
|
|
@@ -64,6 +62,17 @@ const toAbsolute = (file) => {
|
|
|
64
62
|
};
|
|
65
63
|
//#endregion
|
|
66
64
|
//#region src/hash.ts
|
|
65
|
+
const resolveSubpath = (localPackage, subpath) => {
|
|
66
|
+
const exact = localPackage.exports[subpath];
|
|
67
|
+
if (exact) return exact;
|
|
68
|
+
const patterns = Object.entries(localPackage.exports).filter(([pattern]) => pattern.includes("*")).toSorted(([a], [b]) => b.indexOf("*") - a.indexOf("*"));
|
|
69
|
+
for (const [pattern, target] of patterns) {
|
|
70
|
+
const [prefix, suffix] = pattern.split("*");
|
|
71
|
+
if (subpath.length > prefix.length + suffix.length && subpath.startsWith(prefix) && subpath.endsWith(suffix)) return target.replaceAll("*", subpath.slice(prefix.length, subpath.length - suffix.length));
|
|
72
|
+
}
|
|
73
|
+
if (subpath === ".") return localPackage.main;
|
|
74
|
+
return localPackage.hasExports ? void 0 : join(localPackage.path, subpath);
|
|
75
|
+
};
|
|
67
76
|
const generateRecursiveFileHashes = async (workspace, file, sourceFile, allowedExtensions, hashes) => {
|
|
68
77
|
if (isLocalCodeFile(file)) {
|
|
69
78
|
try {
|
|
@@ -89,8 +98,10 @@ const generateRecursiveFileHashes = async (workspace, file, sourceFile, allowedE
|
|
|
89
98
|
else {
|
|
90
99
|
const localPackage = workspace.packages[dependency.link];
|
|
91
100
|
if (!localPackage) throw new Error(`Can't find the local workspace package for: ${file}`);
|
|
92
|
-
|
|
93
|
-
|
|
101
|
+
const subpath = `.${file.slice(module.length)}`;
|
|
102
|
+
const entry = resolveSubpath(localPackage, subpath);
|
|
103
|
+
if (!entry) throw new Error(`Workspace package doesn't export the import: ${file}`);
|
|
104
|
+
await generateRecursiveFileHashes(workspace, entry, sourceFile, allowedExtensions, hashes);
|
|
94
105
|
}
|
|
95
106
|
return;
|
|
96
107
|
}
|
|
@@ -320,15 +331,45 @@ const buildPackages = async (cwd, importers) => {
|
|
|
320
331
|
const packageJson = await readFile(join(cwd, path, "package.json"), "utf8");
|
|
321
332
|
const packageData = JSON.parse(packageJson);
|
|
322
333
|
const entry = packageData.module ?? packageData.main;
|
|
334
|
+
const exports = Object.fromEntries(Object.entries(exportFiles(packageData.exports)).map(([subpath, file]) => [subpath, join(cwd, path, file)]));
|
|
323
335
|
packages[join(cwd, path)] = {
|
|
324
336
|
name: packageData.name,
|
|
325
337
|
path: join(cwd, path),
|
|
326
338
|
main: entry ? join(cwd, path, entry) : void 0,
|
|
339
|
+
exports,
|
|
340
|
+
hasExports: packageData.exports !== void 0,
|
|
327
341
|
dependencies
|
|
328
342
|
};
|
|
329
343
|
}));
|
|
330
344
|
return packages;
|
|
331
345
|
};
|
|
346
|
+
const exportFiles = (exports) => {
|
|
347
|
+
const target = (value) => {
|
|
348
|
+
if (typeof value === "string") return value;
|
|
349
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
350
|
+
const conditions = value;
|
|
351
|
+
for (const condition of [
|
|
352
|
+
"bun",
|
|
353
|
+
"import",
|
|
354
|
+
"module",
|
|
355
|
+
"default",
|
|
356
|
+
"require"
|
|
357
|
+
]) {
|
|
358
|
+
const file = target(conditions[condition]);
|
|
359
|
+
if (file) return file;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
if (exports && typeof exports === "object" && !Array.isArray(exports)) {
|
|
364
|
+
const entries = Object.entries(exports);
|
|
365
|
+
if (entries.every(([key]) => key.startsWith("."))) return Object.fromEntries(entries.flatMap(([subpath, value]) => {
|
|
366
|
+
const file = target(value);
|
|
367
|
+
return file ? [[subpath, file]] : [];
|
|
368
|
+
}));
|
|
369
|
+
}
|
|
370
|
+
const file = target(exports);
|
|
371
|
+
return file ? { ".": file } : {};
|
|
372
|
+
};
|
|
332
373
|
//#endregion
|
|
333
374
|
//#region src/index.ts
|
|
334
375
|
const loadWorkspace = async (search) => {
|