@forgeax/engine-pack 0.0.0-dev.8d955ade1c79
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/LICENSE +202 -0
- package/README.md +429 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/__tests__/ai-recovery.unit.test.d.ts +2 -0
- package/dist/__tests__/ai-recovery.unit.test.d.ts.map +1 -0
- package/dist/__tests__/artifact-path.test.d.ts +2 -0
- package/dist/__tests__/artifact-path.test.d.ts.map +1 -0
- package/dist/__tests__/asset-evidence-error-hints.test.d.ts +2 -0
- package/dist/__tests__/asset-evidence-error-hints.test.d.ts.map +1 -0
- package/dist/__tests__/asset-evidence-lifecycle.test.d.ts +2 -0
- package/dist/__tests__/asset-evidence-lifecycle.test.d.ts.map +1 -0
- package/dist/__tests__/asset-evidence-schema.test.d.ts +2 -0
- package/dist/__tests__/asset-evidence-schema.test.d.ts.map +1 -0
- package/dist/__tests__/atlas-cli-region-mismatch.test.d.ts +2 -0
- package/dist/__tests__/atlas-cli-region-mismatch.test.d.ts.map +1 -0
- package/dist/__tests__/atlas-error-code-owner.test-d.d.ts +2 -0
- package/dist/__tests__/atlas-error-code-owner.test-d.d.ts.map +1 -0
- package/dist/__tests__/cli-asset-evidence.integration.test.d.ts +2 -0
- package/dist/__tests__/cli-asset-evidence.integration.test.d.ts.map +1 -0
- package/dist/__tests__/cli.unit.test.d.ts +2 -0
- package/dist/__tests__/cli.unit.test.d.ts.map +1 -0
- package/dist/__tests__/evidence-source-inventory.unit.test.d.ts +2 -0
- package/dist/__tests__/evidence-source-inventory.unit.test.d.ts.map +1 -0
- package/dist/__tests__/guid-collision.unit.test.d.ts +2 -0
- package/dist/__tests__/guid-collision.unit.test.d.ts.map +1 -0
- package/dist/__tests__/guid-validator-surface.unit.test.d.ts +2 -0
- package/dist/__tests__/guid-validator-surface.unit.test.d.ts.map +1 -0
- package/dist/__tests__/load-asset-config.test.d.ts +2 -0
- package/dist/__tests__/load-asset-config.test.d.ts.map +1 -0
- package/dist/__tests__/material-cook-dependencies.unit.test.d.ts +2 -0
- package/dist/__tests__/material-cook-dependencies.unit.test.d.ts.map +1 -0
- package/dist/__tests__/material-cook-receipt.unit.test.d.ts +2 -0
- package/dist/__tests__/material-cook-receipt.unit.test.d.ts.map +1 -0
- package/dist/__tests__/material-cook-schema.unit.test.d.ts +2 -0
- package/dist/__tests__/material-cook-schema.unit.test.d.ts.map +1 -0
- package/dist/__tests__/mesh-bin-consumer-surface.unit.test.d.ts +2 -0
- package/dist/__tests__/mesh-bin-consumer-surface.unit.test.d.ts.map +1 -0
- package/dist/__tests__/mesh-bin-contract.unit.test.d.ts +2 -0
- package/dist/__tests__/mesh-bin-contract.unit.test.d.ts.map +1 -0
- package/dist/__tests__/meta-schema-optional.test.d.ts +2 -0
- package/dist/__tests__/meta-schema-optional.test.d.ts.map +1 -0
- package/dist/__tests__/meta-source-overrides.unit.test.d.ts +2 -0
- package/dist/__tests__/meta-source-overrides.unit.test.d.ts.map +1 -0
- package/dist/__tests__/native-cooker-registry.test.d.ts +2 -0
- package/dist/__tests__/native-cooker-registry.test.d.ts.map +1 -0
- package/dist/__tests__/offline-evidence-artifact-verification-owner.test-d.d.ts +2 -0
- package/dist/__tests__/offline-evidence-artifact-verification-owner.test-d.d.ts.map +1 -0
- package/dist/__tests__/offline-evidence.integration.test.d.ts +2 -0
- package/dist/__tests__/offline-evidence.integration.test.d.ts.map +1 -0
- package/dist/__tests__/owner-chain.integration.test.d.ts +2 -0
- package/dist/__tests__/owner-chain.integration.test.d.ts.map +1 -0
- package/dist/__tests__/pack-v2-fixture-migration.test.d.ts +2 -0
- package/dist/__tests__/pack-v2-fixture-migration.test.d.ts.map +1 -0
- package/dist/__tests__/pack-v2-schema.test.d.ts +2 -0
- package/dist/__tests__/pack-v2-schema.test.d.ts.map +1 -0
- package/dist/__tests__/pack.unit.test.d.ts +2 -0
- package/dist/__tests__/pack.unit.test.d.ts.map +1 -0
- package/dist/__tests__/package-finalizer.contract.test.d.ts +2 -0
- package/dist/__tests__/package-finalizer.contract.test.d.ts.map +1 -0
- package/dist/__tests__/producer-contract-schema.unit.test.d.ts +2 -0
- package/dist/__tests__/producer-contract-schema.unit.test.d.ts.map +1 -0
- package/dist/__tests__/resolve-asset-source.test.d.ts +2 -0
- package/dist/__tests__/resolve-asset-source.test.d.ts.map +1 -0
- package/dist/__tests__/runtime-projection.unit.test.d.ts +2 -0
- package/dist/__tests__/runtime-projection.unit.test.d.ts.map +1 -0
- package/dist/__tests__/runtime.browser.test.d.ts +2 -0
- package/dist/__tests__/runtime.browser.test.d.ts.map +1 -0
- package/dist/__tests__/scanner-inventory.contract.test.d.ts +2 -0
- package/dist/__tests__/scanner-inventory.contract.test.d.ts.map +1 -0
- package/dist/__tests__/scanner-inventory.test.d.ts +2 -0
- package/dist/__tests__/scanner-inventory.test.d.ts.map +1 -0
- package/dist/__tests__/scanner-mount-cycle.test.d.ts +2 -0
- package/dist/__tests__/scanner-mount-cycle.test.d.ts.map +1 -0
- package/dist/__tests__/scriptable-pack-cli.integration.test.d.ts +2 -0
- package/dist/__tests__/scriptable-pack-cli.integration.test.d.ts.map +1 -0
- package/dist/__tests__/scriptable-pack-ddc-key.unit.test.d.ts +2 -0
- package/dist/__tests__/scriptable-pack-ddc-key.unit.test.d.ts.map +1 -0
- package/dist/__tests__/scriptable-pack.test-d.d.ts +2 -0
- package/dist/__tests__/scriptable-pack.test-d.d.ts.map +1 -0
- package/dist/__tests__/scriptable-pack.unit.test.d.ts +2 -0
- package/dist/__tests__/scriptable-pack.unit.test.d.ts.map +1 -0
- package/dist/__tests__/topology-actions.unit.test.d.ts +2 -0
- package/dist/__tests__/topology-actions.unit.test.d.ts.map +1 -0
- package/dist/__tests__/topology.unit.test.d.ts +2 -0
- package/dist/__tests__/topology.unit.test.d.ts.map +1 -0
- package/dist/artifact-path.d.ts +9 -0
- package/dist/artifact-path.d.ts.map +1 -0
- package/dist/artifact-path.mjs +53 -0
- package/dist/artifact-path.mjs.map +1 -0
- package/dist/atlas/run-atlas.d.ts +8 -0
- package/dist/atlas/run-atlas.d.ts.map +1 -0
- package/dist/atlas/shelf-pack.d.ts +55 -0
- package/dist/atlas/shelf-pack.d.ts.map +1 -0
- package/dist/bridge.d.ts +2 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/bridge.mjs +3 -0
- package/dist/bridge.mjs.map +1 -0
- package/dist/build.d.ts +19 -0
- package/dist/build.d.ts.map +1 -0
- package/dist/build.mjs +10203 -0
- package/dist/build.mjs.map +1 -0
- package/dist/builtin.d.ts +44 -0
- package/dist/builtin.d.ts.map +1 -0
- package/dist/builtin.mjs +141 -0
- package/dist/builtin.mjs.map +1 -0
- package/dist/catalog-builder.d.ts +43 -0
- package/dist/catalog-builder.d.ts.map +1 -0
- package/dist/catalog-delta.d.ts +11 -0
- package/dist/catalog-delta.d.ts.map +1 -0
- package/dist/catalog-projection.d.ts +91 -0
- package/dist/catalog-projection.d.ts.map +1 -0
- package/dist/cli-asset.d.ts +22 -0
- package/dist/cli-asset.d.ts.map +1 -0
- package/dist/cli-asset.mjs +9697 -0
- package/dist/cli-asset.mjs.map +1 -0
- package/dist/config.d.ts +6 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.mjs +29 -0
- package/dist/config.mjs.map +1 -0
- package/dist/deriveAssetName.d.ts +15 -0
- package/dist/deriveAssetName.d.ts.map +1 -0
- package/dist/errors.d.ts +30 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.mjs +19 -0
- package/dist/errors.mjs.map +1 -0
- package/dist/evidence/material-cook.d.ts +92 -0
- package/dist/evidence/material-cook.d.ts.map +1 -0
- package/dist/evidence/offline-evidence.d.ts +25 -0
- package/dist/evidence/offline-evidence.d.ts.map +1 -0
- package/dist/evidence/source-inventory.d.ts +29 -0
- package/dist/evidence/source-inventory.d.ts.map +1 -0
- package/dist/guid.d.ts +45 -0
- package/dist/guid.d.ts.map +1 -0
- package/dist/guid.mjs +94 -0
- package/dist/guid.mjs.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +8612 -0
- package/dist/index.mjs.map +1 -0
- package/dist/material/artifact-writer.d.ts +14 -0
- package/dist/material/artifact-writer.d.ts.map +1 -0
- package/dist/material-cook.d.ts +2 -0
- package/dist/material-cook.d.ts.map +1 -0
- package/dist/material-cook.mjs +248 -0
- package/dist/material-cook.mjs.map +1 -0
- package/dist/mesh-bin-contract.d.ts +36 -0
- package/dist/mesh-bin-contract.d.ts.map +1 -0
- package/dist/mesh-bin-contract.mjs +96 -0
- package/dist/mesh-bin-contract.mjs.map +1 -0
- package/dist/name.mjs +22 -0
- package/dist/name.mjs.map +1 -0
- package/dist/native-cooker-registry.d.ts +47 -0
- package/dist/native-cooker-registry.d.ts.map +1 -0
- package/dist/native-cooker.d.ts +3 -0
- package/dist/native-cooker.d.ts.map +1 -0
- package/dist/native-cooker.mjs +142 -0
- package/dist/native-cooker.mjs.map +1 -0
- package/dist/package-finalizer.d.ts +121 -0
- package/dist/package-finalizer.d.ts.map +1 -0
- package/dist/producer-contract.d.ts +19 -0
- package/dist/producer-contract.d.ts.map +1 -0
- package/dist/resolve-asset-source.d.ts +4 -0
- package/dist/resolve-asset-source.d.ts.map +1 -0
- package/dist/resolve-asset-source.mjs +109 -0
- package/dist/resolve-asset-source.mjs.map +1 -0
- package/dist/runtime-projection.d.ts +14 -0
- package/dist/runtime-projection.d.ts.map +1 -0
- package/dist/runtime-publication.d.ts +44 -0
- package/dist/runtime-publication.d.ts.map +1 -0
- package/dist/runtime.d.ts +8 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.mjs +7261 -0
- package/dist/runtime.mjs.map +1 -0
- package/dist/scanner.d.ts +113 -0
- package/dist/scanner.d.ts.map +1 -0
- package/dist/scanner.mjs +8819 -0
- package/dist/scanner.mjs.map +1 -0
- package/dist/schema-compiled.d.ts +12 -0
- package/dist/schema-compiled.d.ts.map +1 -0
- package/dist/schema.mjs +7369 -0
- package/dist/schema.mjs.map +1 -0
- package/dist/scriptable-pack-node.d.ts +62 -0
- package/dist/scriptable-pack-node.d.ts.map +1 -0
- package/dist/scriptable-pack-node.mjs +1674 -0
- package/dist/scriptable-pack-node.mjs.map +1 -0
- package/dist/scriptable-pack-worker.d.ts +2 -0
- package/dist/scriptable-pack-worker.d.ts.map +1 -0
- package/dist/scriptable-pack-worker.mjs +224 -0
- package/dist/scriptable-pack-worker.mjs.map +1 -0
- package/dist/scriptable-pack.d.ts +558 -0
- package/dist/scriptable-pack.d.ts.map +1 -0
- package/dist/scriptable-pack.mjs +672 -0
- package/dist/scriptable-pack.mjs.map +1 -0
- package/dist/topology.d.ts +10 -0
- package/dist/topology.d.ts.map +1 -0
- package/package.json +154 -0
- package/schema/meta.schema.json +173 -0
- package/schema/pack.schema.json +232 -0
- package/src/__tests__/ai-recovery.unit.test.ts +114 -0
- package/src/__tests__/artifact-path.test.ts +41 -0
- package/src/__tests__/asset-evidence-error-hints.test.ts +46 -0
- package/src/__tests__/asset-evidence-lifecycle.test.ts +104 -0
- package/src/__tests__/asset-evidence-schema.test.ts +86 -0
- package/src/__tests__/atlas-cli-region-mismatch.test.ts +134 -0
- package/src/__tests__/atlas-error-code-owner.test-d.ts +23 -0
- package/src/__tests__/cli-asset-evidence.integration.test.ts +172 -0
- package/src/__tests__/cli.unit.test.ts +448 -0
- package/src/__tests__/evidence-source-inventory.unit.test.ts +37 -0
- package/src/__tests__/guid-collision.unit.test.ts +112 -0
- package/src/__tests__/guid-validator-surface.unit.test.ts +20 -0
- package/src/__tests__/load-asset-config.test.ts +121 -0
- package/src/__tests__/material-cook-dependencies.unit.test.ts +112 -0
- package/src/__tests__/material-cook-receipt.unit.test.ts +148 -0
- package/src/__tests__/material-cook-schema.unit.test.ts +152 -0
- package/src/__tests__/mesh-bin-consumer-surface.unit.test.ts +22 -0
- package/src/__tests__/mesh-bin-contract.unit.test.ts +32 -0
- package/src/__tests__/meta-schema-optional.test.ts +116 -0
- package/src/__tests__/meta-source-overrides.unit.test.ts +71 -0
- package/src/__tests__/native-cooker-registry.test.ts +64 -0
- package/src/__tests__/offline-evidence-artifact-verification-owner.test-d.ts +64 -0
- package/src/__tests__/offline-evidence.integration.test.ts +106 -0
- package/src/__tests__/owner-chain.integration.test.ts +78 -0
- package/src/__tests__/pack-v2-fixture-migration.test.ts +87 -0
- package/src/__tests__/pack-v2-schema.test.ts +142 -0
- package/src/__tests__/pack.unit.test.ts +2218 -0
- package/src/__tests__/package-finalizer.contract.test.ts +105 -0
- package/src/__tests__/producer-contract-schema.unit.test.ts +92 -0
- package/src/__tests__/resolve-asset-source.test.ts +121 -0
- package/src/__tests__/runtime-projection.unit.test.ts +87 -0
- package/src/__tests__/runtime.browser.test.ts +15 -0
- package/src/__tests__/scanner-inventory.contract.test.ts +167 -0
- package/src/__tests__/scanner-inventory.test.ts +76 -0
- package/src/__tests__/scanner-mount-cycle.test.ts +232 -0
- package/src/__tests__/scriptable-pack-cli.integration.test.ts +47 -0
- package/src/__tests__/scriptable-pack-ddc-key.unit.test.ts +63 -0
- package/src/__tests__/scriptable-pack.test-d.ts +131 -0
- package/src/__tests__/scriptable-pack.unit.test.ts +789 -0
- package/src/__tests__/topology-actions.unit.test.ts +94 -0
- package/src/__tests__/topology.unit.test.ts +85 -0
- package/src/artifact-path.ts +69 -0
- package/src/atlas/run-atlas.ts +358 -0
- package/src/atlas/shelf-pack.ts +172 -0
- package/src/atlas/upng-ambient.d.ts +13 -0
- package/src/bridge.ts +2 -0
- package/src/build.ts +139 -0
- package/src/builtin.ts +117 -0
- package/src/catalog-builder.ts +313 -0
- package/src/catalog-delta.ts +189 -0
- package/src/catalog-projection.ts +344 -0
- package/src/cli-asset.ts +586 -0
- package/src/config.ts +36 -0
- package/src/deriveAssetName.ts +42 -0
- package/src/errors.ts +47 -0
- package/src/evidence/material-cook.ts +411 -0
- package/src/evidence/offline-evidence.ts +90 -0
- package/src/evidence/source-inventory.ts +193 -0
- package/src/guid.ts +99 -0
- package/src/index.ts +108 -0
- package/src/material/artifact-writer.ts +26 -0
- package/src/material-cook.ts +15 -0
- package/src/mesh-bin-contract.ts +150 -0
- package/src/native-cooker-registry.ts +235 -0
- package/src/native-cooker.ts +2 -0
- package/src/package-finalizer.ts +394 -0
- package/src/producer-contract.ts +277 -0
- package/src/resolve-asset-source.ts +88 -0
- package/src/runtime-projection.ts +100 -0
- package/src/runtime-publication.ts +190 -0
- package/src/runtime.ts +23 -0
- package/src/scanner.ts +715 -0
- package/src/schema/material-cook.schema.json +119 -0
- package/src/schema-compiled.ts +238 -0
- package/src/scriptable-pack-node.ts +1394 -0
- package/src/scriptable-pack-worker.ts +308 -0
- package/src/scriptable-pack.ts +1006 -0
- package/src/topology.ts +157 -0
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { dirname, extname, join, resolve } from 'node:path';
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
5
|
+
import { parentPort, workerData } from 'node:worker_threads';
|
|
6
|
+
import ts from 'typescript';
|
|
7
|
+
|
|
8
|
+
if (parentPort === null) throw new Error('ScriptablePack worker requires a parent port');
|
|
9
|
+
|
|
10
|
+
const port = parentPort;
|
|
11
|
+
const workerMode = workerData as {
|
|
12
|
+
readonly mode?: 'single' | 'reusable';
|
|
13
|
+
readonly sourcePath?: string;
|
|
14
|
+
readonly compileRoot?: string;
|
|
15
|
+
};
|
|
16
|
+
const reusable = workerMode.mode === 'reusable';
|
|
17
|
+
let sourcePath = workerMode.sourcePath;
|
|
18
|
+
let compileRoot = workerMode.compileRoot;
|
|
19
|
+
type LoadedDefinition = {
|
|
20
|
+
readonly schemaVersion?: unknown;
|
|
21
|
+
readonly packageId?: unknown;
|
|
22
|
+
readonly name?: unknown;
|
|
23
|
+
readonly assets?: unknown;
|
|
24
|
+
readonly sceneComponents?: unknown;
|
|
25
|
+
readonly externalAssets?: unknown;
|
|
26
|
+
readonly build?: unknown;
|
|
27
|
+
};
|
|
28
|
+
let definition: LoadedDefinition | undefined;
|
|
29
|
+
let nextReadId = 0;
|
|
30
|
+
const reads = new Map<number, (value: unknown) => void>();
|
|
31
|
+
|
|
32
|
+
function failure(error: unknown): unknown {
|
|
33
|
+
if (error === null || typeof error !== 'object') return error;
|
|
34
|
+
const value = error as Record<string, unknown>;
|
|
35
|
+
return {
|
|
36
|
+
name: value.name,
|
|
37
|
+
code: value.code,
|
|
38
|
+
expected: value.expected,
|
|
39
|
+
actual: value.actual,
|
|
40
|
+
hint: value.hint,
|
|
41
|
+
detail: value.detail,
|
|
42
|
+
message: value.message,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const SOURCE_EXTENSIONS = ['.ts', '.tsx', '.mts', '.cts', '.js', '.mjs', '.cjs'] as const;
|
|
47
|
+
|
|
48
|
+
function resolveRelativeImport(importer: string, specifier: string): string | undefined {
|
|
49
|
+
if (!specifier.startsWith('.')) return undefined;
|
|
50
|
+
const raw = resolve(dirname(importer), specifier);
|
|
51
|
+
const candidates =
|
|
52
|
+
extname(raw).length > 0
|
|
53
|
+
? [raw]
|
|
54
|
+
: [
|
|
55
|
+
raw,
|
|
56
|
+
...SOURCE_EXTENSIONS.map((extension) => `${raw}${extension}`),
|
|
57
|
+
resolve(raw, 'index.ts'),
|
|
58
|
+
];
|
|
59
|
+
return candidates.find((candidate) => existsSync(candidate));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function outputName(path: string): string {
|
|
63
|
+
const digest = createHash('sha256').update(path).digest('hex').slice(0, 20);
|
|
64
|
+
return `${digest}.mjs`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function resolveBareImport(importer: string, specifier: string): string | undefined {
|
|
68
|
+
const resolver = (
|
|
69
|
+
import.meta as ImportMeta & {
|
|
70
|
+
readonly resolve?: (specifier: string, parent?: string) => string;
|
|
71
|
+
}
|
|
72
|
+
).resolve;
|
|
73
|
+
if (resolver === undefined) return undefined;
|
|
74
|
+
try {
|
|
75
|
+
const resolved = resolver(specifier, pathToFileURL(importer).href);
|
|
76
|
+
return resolved.startsWith('file:') ? fileURLToPath(resolved) : undefined;
|
|
77
|
+
} catch {
|
|
78
|
+
// Type-only or optional imports may not be installed in the game project.
|
|
79
|
+
// Preserve them for TypeScript to erase or let Node report the real load
|
|
80
|
+
// failure instead of guessing a workspace root.
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function resolvedModuleSpecifier(
|
|
86
|
+
target: string,
|
|
87
|
+
sources: ReadonlyMap<string, string>,
|
|
88
|
+
loadId?: number,
|
|
89
|
+
): string {
|
|
90
|
+
if (sources.has(target)) {
|
|
91
|
+
return `./${outputName(target)}${loadId === undefined ? '' : `?load=${loadId}`}`;
|
|
92
|
+
}
|
|
93
|
+
return pathToFileURL(target).href;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Node 22 does not execute `.ts` modules. Compile the trusted local module
|
|
98
|
+
* closure into one disposable ESM directory before importing it. Bare package
|
|
99
|
+
* imports are resolved by Node from the original source module and rewritten
|
|
100
|
+
* to their canonical file URL; relative imports are rewritten to the compiled
|
|
101
|
+
* closure by identity. This keeps unrelated nested node_modules directories
|
|
102
|
+
* (for example Vite's cache) from changing package resolution.
|
|
103
|
+
*/
|
|
104
|
+
function compileModuleClosure(entryPath: string, outputRoot: string, loadId?: number): string {
|
|
105
|
+
const pending = [resolve(entryPath)];
|
|
106
|
+
const sources = new Map<string, string>();
|
|
107
|
+
const imports = new Map<string, ReadonlyMap<string, string>>();
|
|
108
|
+
while (pending.length > 0) {
|
|
109
|
+
const path = pending.pop();
|
|
110
|
+
if (path === undefined || sources.has(path)) continue;
|
|
111
|
+
const source = readFileSync(path, 'utf8');
|
|
112
|
+
sources.set(path, source);
|
|
113
|
+
const resolvedImports = new Map<string, string>();
|
|
114
|
+
for (const imported of ts.preProcessFile(source, true, true).importedFiles) {
|
|
115
|
+
const target = imported.fileName.startsWith('.')
|
|
116
|
+
? resolveRelativeImport(path, imported.fileName)
|
|
117
|
+
: resolveBareImport(path, imported.fileName);
|
|
118
|
+
if (target === undefined) continue;
|
|
119
|
+
resolvedImports.set(imported.fileName, target);
|
|
120
|
+
if (imported.fileName.startsWith('.') || /\.(?:ts|tsx|mts|cts)$/.test(target)) {
|
|
121
|
+
pending.push(target);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
imports.set(path, resolvedImports);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
for (const [path, source] of sources) {
|
|
128
|
+
const replacements = imports.get(path) ?? new Map<string, string>();
|
|
129
|
+
const rewriteRelativeImports: ts.TransformerFactory<ts.SourceFile> = (context) => {
|
|
130
|
+
const visit: ts.Visitor = (node) => {
|
|
131
|
+
if (
|
|
132
|
+
(ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) &&
|
|
133
|
+
node.moduleSpecifier !== undefined &&
|
|
134
|
+
ts.isStringLiteral(node.moduleSpecifier)
|
|
135
|
+
) {
|
|
136
|
+
const target = replacements.get(node.moduleSpecifier.text);
|
|
137
|
+
if (target !== undefined) {
|
|
138
|
+
const moduleSpecifier = ts.factory.createStringLiteral(
|
|
139
|
+
resolvedModuleSpecifier(target, sources, loadId),
|
|
140
|
+
);
|
|
141
|
+
return ts.isImportDeclaration(node)
|
|
142
|
+
? ts.factory.updateImportDeclaration(
|
|
143
|
+
node,
|
|
144
|
+
node.modifiers,
|
|
145
|
+
node.importClause,
|
|
146
|
+
moduleSpecifier,
|
|
147
|
+
node.attributes,
|
|
148
|
+
)
|
|
149
|
+
: ts.factory.updateExportDeclaration(
|
|
150
|
+
node,
|
|
151
|
+
node.modifiers,
|
|
152
|
+
node.isTypeOnly,
|
|
153
|
+
node.exportClause,
|
|
154
|
+
moduleSpecifier,
|
|
155
|
+
node.attributes,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (
|
|
160
|
+
ts.isCallExpression(node) &&
|
|
161
|
+
node.expression.kind === ts.SyntaxKind.ImportKeyword &&
|
|
162
|
+
node.arguments.length === 1 &&
|
|
163
|
+
node.arguments[0] !== undefined &&
|
|
164
|
+
ts.isStringLiteral(node.arguments[0])
|
|
165
|
+
) {
|
|
166
|
+
const target = replacements.get(node.arguments[0].text);
|
|
167
|
+
if (target !== undefined) {
|
|
168
|
+
return ts.factory.updateCallExpression(node, node.expression, node.typeArguments, [
|
|
169
|
+
ts.factory.createStringLiteral(resolvedModuleSpecifier(target, sources, loadId)),
|
|
170
|
+
]);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return ts.visitEachChild(node, visit, context);
|
|
174
|
+
};
|
|
175
|
+
return (sourceFile) => ts.visitNode(sourceFile, visit) as ts.SourceFile;
|
|
176
|
+
};
|
|
177
|
+
const transpiled = ts.transpileModule(source, {
|
|
178
|
+
fileName: path,
|
|
179
|
+
compilerOptions: {
|
|
180
|
+
target: ts.ScriptTarget.ES2022,
|
|
181
|
+
module: ts.ModuleKind.ESNext,
|
|
182
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
183
|
+
isolatedModules: true,
|
|
184
|
+
sourceMap: false,
|
|
185
|
+
},
|
|
186
|
+
transformers: { before: [rewriteRelativeImports] },
|
|
187
|
+
reportDiagnostics: true,
|
|
188
|
+
});
|
|
189
|
+
const errors =
|
|
190
|
+
transpiled.diagnostics?.filter(
|
|
191
|
+
(diagnostic) => diagnostic.category === ts.DiagnosticCategory.Error,
|
|
192
|
+
) ?? [];
|
|
193
|
+
if (errors.length > 0) {
|
|
194
|
+
throw new Error(
|
|
195
|
+
`ScriptablePack TypeScript transpile failed for ${path}: ${errors
|
|
196
|
+
.map((diagnostic) => ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'))
|
|
197
|
+
.join('; ')}`,
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
writeFileSync(join(outputRoot, outputName(path)), transpiled.outputText);
|
|
201
|
+
}
|
|
202
|
+
return join(outputRoot, outputName(resolve(entryPath)));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function loadDefinition(task: {
|
|
206
|
+
readonly sourcePath: string;
|
|
207
|
+
readonly compileRoot: string;
|
|
208
|
+
readonly loadId?: number;
|
|
209
|
+
}): Promise<void> {
|
|
210
|
+
sourcePath = task.sourcePath;
|
|
211
|
+
compileRoot = task.compileRoot;
|
|
212
|
+
definition = undefined;
|
|
213
|
+
reads.clear();
|
|
214
|
+
const executablePath = SOURCE_EXTENSIONS.includes(
|
|
215
|
+
extname(task.sourcePath) as (typeof SOURCE_EXTENSIONS)[number],
|
|
216
|
+
)
|
|
217
|
+
? compileModuleClosure(task.sourcePath, task.compileRoot, task.loadId)
|
|
218
|
+
: task.sourcePath;
|
|
219
|
+
const loaded = (await import(
|
|
220
|
+
`${pathToFileURL(executablePath).href}${reusable ? `?load=${task.loadId ?? 0}` : ''}`
|
|
221
|
+
)) as {
|
|
222
|
+
readonly default?: unknown;
|
|
223
|
+
};
|
|
224
|
+
definition = loaded.default as LoadedDefinition | undefined;
|
|
225
|
+
port.postMessage({
|
|
226
|
+
kind: 'loaded',
|
|
227
|
+
...(task.loadId === undefined ? {} : { loadId: task.loadId }),
|
|
228
|
+
definition:
|
|
229
|
+
definition === undefined
|
|
230
|
+
? undefined
|
|
231
|
+
: {
|
|
232
|
+
schemaVersion: definition.schemaVersion,
|
|
233
|
+
packageId: definition.packageId,
|
|
234
|
+
name: definition.name,
|
|
235
|
+
assets: definition.assets,
|
|
236
|
+
sceneComponents: definition.sceneComponents,
|
|
237
|
+
externalAssets: definition.externalAssets,
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
async function handleMessage(message: unknown): Promise<void> {
|
|
243
|
+
if (message === null || typeof message !== 'object') return;
|
|
244
|
+
const value = message as Record<string, unknown>;
|
|
245
|
+
if (value.kind === 'asset-result' && typeof value.readId === 'number') {
|
|
246
|
+
reads.get(value.readId)?.(value.result);
|
|
247
|
+
reads.delete(value.readId);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (
|
|
251
|
+
reusable &&
|
|
252
|
+
value.kind === 'load' &&
|
|
253
|
+
typeof value.sourcePath === 'string' &&
|
|
254
|
+
typeof value.compileRoot === 'string' &&
|
|
255
|
+
typeof value.loadId === 'number'
|
|
256
|
+
) {
|
|
257
|
+
try {
|
|
258
|
+
await loadDefinition({
|
|
259
|
+
sourcePath: value.sourcePath,
|
|
260
|
+
compileRoot: value.compileRoot,
|
|
261
|
+
loadId: value.loadId,
|
|
262
|
+
});
|
|
263
|
+
} catch (error) {
|
|
264
|
+
port.postMessage({ kind: 'load-threw', loadId: value.loadId, error: failure(error) });
|
|
265
|
+
}
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (value.kind !== 'build' || typeof value.buildId !== 'number') return;
|
|
269
|
+
if (typeof definition?.build !== 'function') {
|
|
270
|
+
port.postMessage({
|
|
271
|
+
kind: 'build-result',
|
|
272
|
+
buildId: value.buildId,
|
|
273
|
+
result: { ok: false, error: { message: 'default export has no build function' } },
|
|
274
|
+
});
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
try {
|
|
278
|
+
const result = await definition.build({
|
|
279
|
+
readByGuid(guid: Uint8Array): Promise<unknown> {
|
|
280
|
+
const readId = nextReadId++;
|
|
281
|
+
port.postMessage({ kind: 'asset-read', buildId: value.buildId, readId, guid });
|
|
282
|
+
return new Promise((resolve) => reads.set(readId, resolve));
|
|
283
|
+
},
|
|
284
|
+
});
|
|
285
|
+
port.postMessage({
|
|
286
|
+
kind: 'build-result',
|
|
287
|
+
buildId: value.buildId,
|
|
288
|
+
result:
|
|
289
|
+
result !== null && typeof result === 'object' && 'ok' in result && !result.ok
|
|
290
|
+
? { ...result, error: failure(result.error) }
|
|
291
|
+
: result,
|
|
292
|
+
});
|
|
293
|
+
} catch (error) {
|
|
294
|
+
port.postMessage({ kind: 'build-threw', buildId: value.buildId, error: failure(error) });
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
port.on('message', (message: unknown) => {
|
|
299
|
+
void handleMessage(message);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
if (!reusable && sourcePath !== undefined && compileRoot !== undefined) {
|
|
303
|
+
try {
|
|
304
|
+
await loadDefinition({ sourcePath, compileRoot });
|
|
305
|
+
} catch (error) {
|
|
306
|
+
port.postMessage({ kind: 'load-threw', error: failure(error) });
|
|
307
|
+
}
|
|
308
|
+
}
|