@dimina-kit/compiler 0.0.1-dev.20260702173719
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 +21 -0
- package/README.md +363 -0
- package/dist/compile-core.browser.js +281945 -0
- package/dist/compile-core.node.js +3702 -0
- package/dist/pool.browser.js +99 -0
- package/dist/pool.node.js +3743 -0
- package/dist/stage-worker.browser.js +291085 -0
- package/dist/stage-worker.node.js +3097 -0
- package/dist/toolchain.browser.js +30 -0
- package/package.json +87 -0
- package/scripts/build-compiler.js +207 -0
- package/scripts/gen-bench-fixture.js +24 -0
- package/scripts/kit-resolve-hook.js +35 -0
- package/scripts/register-kit.js +2 -0
- package/scripts/test-appid-fallback.js +114 -0
- package/scripts/test-decompose.js +90 -0
- package/scripts/test-hardening.js +78 -0
- package/scripts/test-node.js +69 -0
- package/scripts/test-npm-scan.js +164 -0
- package/scripts/test-pool-hardening.js +65 -0
- package/scripts/test-pool-node.js +117 -0
- package/scripts/test-realm-reuse.js +77 -0
- package/src/browser-entry.js +25 -0
- package/src/compile-core.js +428 -0
- package/src/pool-node.js +170 -0
- package/src/pool.js +122 -0
- package/src/shims/esbuild-wasm.js +19 -0
- package/src/shims/fs-promises.js +20 -0
- package/src/shims/fs.js +59 -0
- package/src/shims/less.js +9 -0
- package/src/shims/os.js +11 -0
- package/src/shims/oxc-parser.js +21 -0
- package/src/shims/oxc-walker.js +29 -0
- package/src/shims/postcss-noop-plugin.js +9 -0
- package/src/shims/process.js +20 -0
- package/src/shims/url.js +4 -0
- package/src/shims/worker_threads.js +10 -0
- package/src/stage-worker-node.js +41 -0
- package/src/stage-worker.js +88 -0
- package/src/toolchain.js +49 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
globalThis.global ||= globalThis;
|
|
2
|
+
globalThis.process ||= { env: {}, cwd: () => "/" };
|
|
3
|
+
globalThis.process.cwd ||= () => "/";
|
|
4
|
+
|
|
5
|
+
// src/pool.js
|
|
6
|
+
var DEFAULT_STAGES = ["logic", "view", "style"];
|
|
7
|
+
function createCompilerPool(options = {}) {
|
|
8
|
+
const {
|
|
9
|
+
createWorker,
|
|
10
|
+
toolchainSetupURL,
|
|
11
|
+
stages = DEFAULT_STAGES,
|
|
12
|
+
workPath: defaultWorkPath = "/work",
|
|
13
|
+
onLog
|
|
14
|
+
} = options;
|
|
15
|
+
if (typeof createWorker !== "function") {
|
|
16
|
+
throw new Error("[compiler] createCompilerPool: options.createWorker (() => Worker) is required");
|
|
17
|
+
}
|
|
18
|
+
if (!toolchainSetupURL) {
|
|
19
|
+
throw new Error("[compiler] createCompilerPool: options.toolchainSetupURL is required");
|
|
20
|
+
}
|
|
21
|
+
const workers = stages.map((stage) => {
|
|
22
|
+
const w = createWorker();
|
|
23
|
+
const q = [];
|
|
24
|
+
w.onmessage = (e) => {
|
|
25
|
+
const d = e.data;
|
|
26
|
+
if (d && d.type === "log") {
|
|
27
|
+
if (onLog) {
|
|
28
|
+
try {
|
|
29
|
+
onLog({ level: d.level, message: d.message, stage });
|
|
30
|
+
} catch {
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const r = q.shift();
|
|
36
|
+
if (r) r(d);
|
|
37
|
+
};
|
|
38
|
+
w.onerror = (ev) => {
|
|
39
|
+
const msg = ev && (ev.message || ev.error && ev.error.message) || "worker failed to load or threw (no message \u2014 often a module-load / static-asset / cross-origin failure)";
|
|
40
|
+
const where = ev && ev.filename ? ` (${ev.filename}:${ev.lineno || 0})` : "";
|
|
41
|
+
const r = q.shift();
|
|
42
|
+
if (r) r({ type: "error", error: `[compiler] stage '${stage}' worker error: ${msg}${where}` });
|
|
43
|
+
};
|
|
44
|
+
return { stage, w, send: (m) => new Promise((res) => {
|
|
45
|
+
q.push(res);
|
|
46
|
+
w.postMessage(m);
|
|
47
|
+
}) };
|
|
48
|
+
});
|
|
49
|
+
let warmed = null;
|
|
50
|
+
async function warmup() {
|
|
51
|
+
if (!warmed) {
|
|
52
|
+
warmed = Promise.all(workers.map((x) => x.send({ type: "warmup", toolchainSetupURL }))).then((rs) => rs.forEach((r, i) => {
|
|
53
|
+
if (r && r.type === "error") throw new Error(r.error || `[compiler] stage '${workers[i].stage}' warmup failed`);
|
|
54
|
+
})).catch((err) => {
|
|
55
|
+
warmed = null;
|
|
56
|
+
throw err;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return warmed;
|
|
60
|
+
}
|
|
61
|
+
let chain = Promise.resolve();
|
|
62
|
+
function compile(input = {}) {
|
|
63
|
+
const run = chain.then(async () => {
|
|
64
|
+
await warmup();
|
|
65
|
+
const files = input.files || input;
|
|
66
|
+
if (!files || typeof files !== "object" || !Object.keys(files).length) {
|
|
67
|
+
throw new Error("[compiler] pool.compile expects { files: { relPath: content }, workPath? } (or a non-empty files map)");
|
|
68
|
+
}
|
|
69
|
+
const workPath = input.workPath || defaultWorkPath;
|
|
70
|
+
const parts = await Promise.all(workers.map((x) => x.send({ type: "compile-subset", files, workPath, stages: [x.stage] })));
|
|
71
|
+
const merged = {};
|
|
72
|
+
let appId, name;
|
|
73
|
+
for (let i = 0; i < parts.length; i++) {
|
|
74
|
+
const pr = parts[i];
|
|
75
|
+
if (!pr || pr.type === "error") throw new Error(pr && pr.error ? pr.error : `[compiler] stage '${workers[i].stage}' worker error`);
|
|
76
|
+
appId = pr.result.appId;
|
|
77
|
+
name = pr.result.name;
|
|
78
|
+
Object.assign(merged, pr.result.files);
|
|
79
|
+
}
|
|
80
|
+
return { appId, name, files: merged };
|
|
81
|
+
});
|
|
82
|
+
chain = run.then(() => {
|
|
83
|
+
}, () => {
|
|
84
|
+
});
|
|
85
|
+
return run;
|
|
86
|
+
}
|
|
87
|
+
function dispose() {
|
|
88
|
+
for (const x of workers) {
|
|
89
|
+
try {
|
|
90
|
+
x.w.terminate();
|
|
91
|
+
} catch {
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return { warmup, compile, dispose, stages: [...stages] };
|
|
96
|
+
}
|
|
97
|
+
export {
|
|
98
|
+
createCompilerPool
|
|
99
|
+
};
|