@teacss/engine 0.8.5-alpha.4 → 0.9.0-alpha.4

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.
@@ -1,56 +0,0 @@
1
- // @ts-check
2
- import { ENGINE_INSTANCE } from "./constants.mjs";
3
-
4
- /**
5
- * Replays v02.2 `TransformResult.edits` on the host MagicString. A zero-length range is an insert:
6
- * magic-string throws on a zero-length `update`.
7
- * @param {{ update(start: number, end: number, text: string): unknown, appendLeft(index: number, text: string): unknown }} s
8
- * @param {{ start: number, end: number, text: string }[]} edits
9
- */
10
- export function replayEdits(s, edits) {
11
- for (const { start, end, text } of edits) {
12
- if (start === end) s.appendLeft(start, text);
13
- else s.update(start, end, text);
14
- }
15
- }
16
-
17
- /** @param {any} ctx */
18
- const engineOf = (ctx) => ctx?.generator?.[ENGINE_INSTANCE] ?? null;
19
-
20
- /**
21
- * The object shape of preset-standard/transformers/compile-class.ts:178-274, backed by Rust
22
- * `transformSource`. It has no `idFilter`: the runtime's pipeline filter picks the modules.
23
- */
24
- export function createCompileClassTransformer() {
25
- return {
26
- name: "@teacss/transformer-compile-class",
27
- enforce: "pre",
28
- /** @param {any} s @param {string} id @param {any} ctx */
29
- async transform(s, id, ctx) {
30
- const engine = engineOf(ctx);
31
- if (!engine) return;
32
- const result = engine.transformSource(id, s.original);
33
- if (!result) return;
34
- replayEdits(s, result.edits);
35
- if (ctx.tokens) for (const name of result.compiledClasses) ctx.tokens.add(name);
36
- if (result.definitionsChanged) ctx.invalidate?.();
37
- return { highlightAnnotations: result.highlightAnnotations };
38
- },
39
- /**
40
- * Releases the module's definitions only: the runtime also calls this when a module stays
41
- * (context switches, failed transforms), so its tokens are kept (v02.9 §6.1 step 8).
42
- * @param {string} id @param {any} ctx
43
- */
44
- onModuleRemoved(id, ctx) {
45
- const engine = engineOf(ctx);
46
- if (!engine) return;
47
- let changed = false;
48
- try {
49
- changed = engine.clearModuleDefinitions(id);
50
- } catch (error) {
51
- if (/** @type {any} */ (error)?.code !== "E_DISPOSED") throw error;
52
- }
53
- if (changed) ctx.invalidate?.();
54
- },
55
- };
56
- }
@@ -1,88 +0,0 @@
1
- // @ts-check
2
- // The lazily created TypeScript generator behind the facade (v02.9 §3.3).
3
-
4
- import { ENGINE_INSTANCE } from "./constants.mjs";
5
- import { tsRequiredError } from "./errors.mjs";
6
-
7
- /** @typedef {{ version: string, createGenerator: (config?: object, defaults?: object) => Promise<any>, extractorSplit: object }} CoreBinding */
8
-
9
- /** Test seam that replaces the dynamic import of `@teacss/core`. */
10
- export const coreLoader = {
11
- /** @type {(() => Promise<CoreBinding>) | null} */
12
- load: null,
13
- };
14
-
15
- /** @param {string} member @returns {Promise<CoreBinding>} rejects with `E_TS_REQUIRED` when the peer is missing */
16
- export async function importCore(member) {
17
- try {
18
- // @ts-ignore optional peer
19
- return await (coreLoader.load ? coreLoader.load() : import("@teacss/core"));
20
- } catch {
21
- throw tsRequiredError(member);
22
- }
23
- }
24
-
25
- /**
26
- * `engine: "ts"` goes into the defaults, which take precedence over `config.engine`, so the TS
27
- * generator does not enter the hook again.
28
- * @param {{ core?: CoreBinding | undefined }} host
29
- * @param {{ userConfig: object, defaults: object }} committed the configuration committed at construction
30
- */
31
- export function createTsFallback(host, committed) {
32
- /** @type {Promise<any> | null} */
33
- let generatorPromise = null;
34
- /** @type {any} */
35
- let resolved = null;
36
- /** @param {string} member */
37
- const core = (member) => (host.core ? Promise.resolve(host.core) : importCore(member));
38
- /**
39
- * A TypeScript generator. The hook's `createTsGenerator` bypasses it; `createGenerator` (an older
40
- * core, or `@teacss/core` imported without a hook) re-enters it when TEACSS_ENGINE overrides
41
- * `engine: "ts"`, and returns a facade for a configuration Rust can serve.
42
- * @param {any} binding @param {object} userConfig @param {object} defaults
43
- */
44
- const createTs = (binding, userConfig, defaults) => {
45
- const tsDefaults = { ...defaults, engine: "ts" };
46
- return typeof binding.createTsGenerator === "function" ? binding.createTsGenerator(userConfig, tsDefaults) : binding.createGenerator(userConfig, tsDefaults);
47
- };
48
- return {
49
- /** Called after a successful `commitConfig`; drops the lazy instance. @param {object} userConfig @param {object} defaults */
50
- invalidate(userConfig, defaults) {
51
- committed = { userConfig, defaults };
52
- generatorPromise = null;
53
- resolved = null;
54
- },
55
- /** The lazy TS generator; a rejection is not cached. @param {string} [member] */
56
- generator(member = "materialize") {
57
- generatorPromise ??= (async () => {
58
- const binding = await core(member);
59
- const generator = await createTs(binding, committed.userConfig, committed.defaults);
60
- resolved = generator;
61
- return generator;
62
- })().catch((error) => {
63
- generatorPromise = null;
64
- throw error;
65
- });
66
- return generatorPromise;
67
- },
68
- /** The ready instance, or `null` before `generator()` resolves. */
69
- peek() {
70
- return resolved;
71
- },
72
- /**
73
- * A TS candidate for a configuration (v02.9 §5.2), made by the TS generator's own
74
- * `prepareConfig` so it tracks in-place edits of its config as an oracle candidate does
75
- * (generator.ts:1986-2011). The lazy instance is untouched.
76
- * @param {object} userConfig @param {object} defaults
77
- */
78
- async create(userConfig, defaults) {
79
- const binding = await core("prepareConfig");
80
- const tsDefaults = { ...defaults, engine: "ts" };
81
- // Without `createTsGenerator` the base is built from the configuration itself: Rust cannot
82
- // translate it, so `createGenerator` resolves to TS and creates no Engine.
83
- const base = await createTs(binding, typeof binding.createTsGenerator === "function" ? {} : userConfig, defaults);
84
- if (ENGINE_INSTANCE in Object(base) || typeof base?.prepareConfig !== "function") return base;
85
- return (await base.prepareConfig(userConfig, tsDefaults)).generator;
86
- },
87
- };
88
- }