@openelement/compiler 0.0.0-stage → 1.0.0-alpha.12
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 +20 -2
- package/package.json +48 -4
- package/src/compiler.d.ts +8 -0
- package/src/compiler.js +8 -0
- package/src/internal/compiler/plugin.d.ts +95 -0
- package/src/internal/compiler/plugin.js +133 -0
- package/src/internal/compiler/semantic-core/analyze-module.js +479 -0
- package/src/internal/compiler/semantic-core/compile.d.ts +39 -0
- package/src/internal/compiler/semantic-core/compile.js +98 -0
- package/src/internal/compiler/semantic-core/compiler-diagnostics.d.ts +31 -0
- package/src/internal/compiler/semantic-core/compiler-diagnostics.js +50 -0
- package/src/internal/compiler/semantic-core/diagnostics/index.d.ts +54 -0
- package/src/internal/compiler/semantic-core/diagnostics/index.js +97 -0
- package/src/internal/compiler/semantic-core/emit-module.js +259 -0
- package/src/internal/compiler/semantic-core/emit-program.js +121 -0
- package/src/internal/compiler/semantic-core/lower-program.js +815 -0
- package/src/internal/compiler/semantic-core/module-analysis.d.ts +115 -0
- package/src/internal/compiler/semantic-core/module-analysis.js +263 -0
- package/src/internal/compiler/semantic-core/parse-module.js +37 -0
- package/src/internal/compiler/semantic-core/source-map.d.ts +53 -0
- package/src/internal/compiler/semantic-core/source-map.js +93 -0
- package/src/internal/compiler/semantic-core/style-imports.js +87 -0
- package/src/internal/compiler/semantic-core/type-check.d.ts +50 -0
- package/src/internal/compiler/semantic-core/type-check.js +106 -0
- package/src/internal/compiler/style-requests.d.ts +53 -0
- package/src/internal/compiler/style-requests.js +50 -0
- package/src/vite.d.ts +2 -0
- package/src/vite.js +2 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
//#region src/internal/compiler/semantic-core/type-check.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* One diagnostic the type checker produced for the emitted module. `file` is
|
|
5
|
+
* the virtual module id the check was given; the remaining fields locate the
|
|
6
|
+
* range inside the EMITTED text.
|
|
7
|
+
*/
|
|
8
|
+
export interface EmittedModuleDiagnostic {
|
|
9
|
+
code: number;
|
|
10
|
+
message: string;
|
|
11
|
+
file: string;
|
|
12
|
+
line: number;
|
|
13
|
+
character: number;
|
|
14
|
+
}
|
|
15
|
+
/** How the emitted module's imports resolve, and any virtual files to add. */
|
|
16
|
+
export interface EmittedModuleTypeCheckOptions {
|
|
17
|
+
/**
|
|
18
|
+
* Module resolution map for the specifiers the emitted module imports, in
|
|
19
|
+
* TypeScript `paths` form (specifier → candidate files). A caller that
|
|
20
|
+
* resolves `@openelement/element` through the workspace passes that map;
|
|
21
|
+
* omitting it means imported specifiers resolve through `node_modules`, which
|
|
22
|
+
* is what a packed consumer has instead.
|
|
23
|
+
*/
|
|
24
|
+
paths?: Record<string, string[]>;
|
|
25
|
+
/** Extra compiler options, merged over the defaults below. */
|
|
26
|
+
compilerOptions?: ts.CompilerOptions;
|
|
27
|
+
/**
|
|
28
|
+
* Support files the emitted module imports, supplied as text for ids that do
|
|
29
|
+
* not exist on disk (a bundler's other virtual modules). The emitted module
|
|
30
|
+
* itself is added under `fileName`.
|
|
31
|
+
*/
|
|
32
|
+
extraFiles?: Readonly<Record<string, string>>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Type-check one emitted compiled module. Returns `[]` when the module
|
|
36
|
+
* type-checks; otherwise every diagnostic the checker reported, in emission
|
|
37
|
+
* order, so a build can print all of them instead of only the first.
|
|
38
|
+
*
|
|
39
|
+
* The `fileName` should be the module's real (virtual) id, not a placeholder:
|
|
40
|
+
* module resolution and `paths` matching both key off it, and the diagnostics
|
|
41
|
+
* quote it back.
|
|
42
|
+
*/
|
|
43
|
+
export declare function typeCheckEmittedModule(code: string, fileName: string, options?: EmittedModuleTypeCheckOptions): EmittedModuleDiagnostic[];
|
|
44
|
+
/**
|
|
45
|
+
* `true` when the emitted module type-checks. The boolean form exists so a
|
|
46
|
+
* caller that only needs a verdict does not destructure an empty array; the
|
|
47
|
+
* diagnostics form is for callers that must report what failed.
|
|
48
|
+
*/
|
|
49
|
+
export declare function emittedModuleTypeChecks(code: string, fileName: string, options?: EmittedModuleTypeCheckOptions): boolean;
|
|
50
|
+
//#endregion
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { CompilerErrorCode, frameworkError } from "@openelement/protocol/errors";
|
|
3
|
+
//#region src/internal/compiler/semantic-core/type-check.ts
|
|
4
|
+
/**
|
|
5
|
+
* Build-time type check for the emitted compiled module (#1386 item 2).
|
|
6
|
+
*
|
|
7
|
+
* The compiler emits the compiled module as TypeScript text and hands it to the
|
|
8
|
+
* bundler, which lowers it. Until this module existed, nothing type-checked
|
|
9
|
+
* that text: a compiler change could emit a program that fails `tsc` or a
|
|
10
|
+
* consumer's `tsgo` run, and the first evidence was a consumer's
|
|
11
|
+
* build — the reason the "strict emission" corpus in
|
|
12
|
+
* `__tests__/compiled-element-v1.test.ts` had to pin individual `static`
|
|
13
|
+
* annotations by string match.
|
|
14
|
+
*
|
|
15
|
+
* The check compiles the emitted text as a real TypeScript program and returns
|
|
16
|
+
* the diagnostics a type checker produces for it. It is a pure function of its
|
|
17
|
+
* inputs — the emitted text plus explicit compiler options and module
|
|
18
|
+
* resolution paths — because the semantic core is admitted no ambient state:
|
|
19
|
+
* it reads no clock, touches no network, and mutates no global. Callers own the
|
|
20
|
+
* environment (a Vite adapter passes its resolved paths; a test passes the
|
|
21
|
+
* workspace map).
|
|
22
|
+
*
|
|
23
|
+
* Scope: this is a verification boundary, not a second compiler. It never
|
|
24
|
+
* rewrites, downlevels, or evaluates the module; it answers exactly one
|
|
25
|
+
* question — does the program this compiler emits type-check? — so the answer
|
|
26
|
+
* can gate a build instead of surfacing in production.
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* The compiler options the emitted module is checked under. They describe the
|
|
30
|
+
* language level the compiled module targets and, deliberately, keep the check
|
|
31
|
+
* strict: the emitted text declares its own statics with explicit types, so a
|
|
32
|
+
* program that only type-checks under `strict: false` is a defect this gate is
|
|
33
|
+
* meant to catch.
|
|
34
|
+
*/
|
|
35
|
+
const DEFAULT_COMPILER_OPTIONS = {
|
|
36
|
+
allowImportingTsExtensions: true,
|
|
37
|
+
noEmit: true,
|
|
38
|
+
strict: true,
|
|
39
|
+
target: ts.ScriptTarget.ES2022,
|
|
40
|
+
module: ts.ModuleKind.ESNext,
|
|
41
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
42
|
+
skipLibCheck: true
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Type-check one emitted compiled module. Returns `[]` when the module
|
|
46
|
+
* type-checks; otherwise every diagnostic the checker reported, in emission
|
|
47
|
+
* order, so a build can print all of them instead of only the first.
|
|
48
|
+
*
|
|
49
|
+
* The `fileName` should be the module's real (virtual) id, not a placeholder:
|
|
50
|
+
* module resolution and `paths` matching both key off it, and the diagnostics
|
|
51
|
+
* quote it back.
|
|
52
|
+
*/
|
|
53
|
+
function typeCheckEmittedModule(code, fileName, options = {}) {
|
|
54
|
+
const compilerOptions = {
|
|
55
|
+
...DEFAULT_COMPILER_OPTIONS,
|
|
56
|
+
...options.compilerOptions
|
|
57
|
+
};
|
|
58
|
+
if (options.paths && Object.keys(options.paths).length > 0) {
|
|
59
|
+
compilerOptions.paths = { ...options.paths };
|
|
60
|
+
compilerOptions.baseUrl ??= ".";
|
|
61
|
+
}
|
|
62
|
+
const virtualFiles = /* @__PURE__ */ new Map([[fileName, code]]);
|
|
63
|
+
for (const [id, text] of Object.entries(options.extraFiles ?? {})) virtualFiles.set(id, text);
|
|
64
|
+
const baseHost = ts.createCompilerHost(compilerOptions, true);
|
|
65
|
+
const host = {
|
|
66
|
+
...baseHost,
|
|
67
|
+
readFile: (requested) => virtualFiles.get(requested) ?? baseHost.readFile(requested),
|
|
68
|
+
fileExists: (requested) => virtualFiles.has(requested) || baseHost.fileExists(requested),
|
|
69
|
+
getSourceFile: (requested, languageVersion, onError, shouldCreate) => {
|
|
70
|
+
const virtual = virtualFiles.get(requested);
|
|
71
|
+
if (virtual === void 0) return baseHost.getSourceFile(requested, languageVersion, onError, shouldCreate);
|
|
72
|
+
return ts.createSourceFile(requested, virtual, languageVersion, true, requested.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS);
|
|
73
|
+
},
|
|
74
|
+
writeFile: () => {
|
|
75
|
+
throw frameworkError(CompilerErrorCode.INVALID_SOURCE, "typeCheckEmittedModule never emits", { phase: "build" });
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const program = ts.createProgram({
|
|
79
|
+
rootNames: [...virtualFiles.keys()],
|
|
80
|
+
options: compilerOptions,
|
|
81
|
+
host
|
|
82
|
+
});
|
|
83
|
+
return ts.getPreEmitDiagnostics(program).filter((diagnostic) => {
|
|
84
|
+
return diagnostic.file !== void 0 && virtualFiles.has(diagnostic.file.fileName);
|
|
85
|
+
}).map((diagnostic) => {
|
|
86
|
+
const file = diagnostic.file;
|
|
87
|
+
const position = file.getLineAndCharacterOfPosition(diagnostic.start ?? 0);
|
|
88
|
+
return {
|
|
89
|
+
code: diagnostic.code,
|
|
90
|
+
message: ts.flattenDiagnosticMessageText(diagnostic.messageText, " "),
|
|
91
|
+
file: file.fileName,
|
|
92
|
+
line: position.line + 1,
|
|
93
|
+
character: position.character + 1
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* `true` when the emitted module type-checks. The boolean form exists so a
|
|
99
|
+
* caller that only needs a verdict does not destructure an empty array; the
|
|
100
|
+
* diagnostics form is for callers that must report what failed.
|
|
101
|
+
*/
|
|
102
|
+
function emittedModuleTypeChecks(code, fileName, options = {}) {
|
|
103
|
+
return typeCheckEmittedModule(code, fileName, options).length === 0;
|
|
104
|
+
}
|
|
105
|
+
//#endregion
|
|
106
|
+
export { emittedModuleTypeChecks, typeCheckEmittedModule };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//#region src/internal/compiler/style-requests.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Style-edge request channel (#1558).
|
|
4
|
+
*
|
|
5
|
+
* The compiler never writes files (three-owner seam split, #1553 §1): a
|
|
6
|
+
* compiled module's `.css` imports become registered style edges, and the
|
|
7
|
+
* host build's style-asset plugin intercepts those edges and owns the emitted
|
|
8
|
+
* `.css` artifacts — `emitFile`, content hash, adapter. The sheet's bytes live
|
|
9
|
+
* in exactly one place, the authored file (P6): no registry entry carries CSS
|
|
10
|
+
* text. This registry is how an edge travels from the compiled-element
|
|
11
|
+
* transform (the one writer) to the intercepting plugin (the one reader)
|
|
12
|
+
* within a build process — keyed by the edge's resolved module id (the
|
|
13
|
+
* importing module's directory plus the sheet's file name), the same id the
|
|
14
|
+
* bundler's resolver lands on. A `.css` import from a module with no
|
|
15
|
+
* registered edges is a plain app stylesheet and rides vite's own CSS
|
|
16
|
+
* channel; a `.css` import from a tracked module with no edge entry is a
|
|
17
|
+
* defect and fails closed at the intercept.
|
|
18
|
+
*/
|
|
19
|
+
/** One registered style edge: the authored import's build-graph coordinates. */
|
|
20
|
+
export interface RegisteredStyleRequest {
|
|
21
|
+
/** Resolved edge module id — the registry key, and what resolveId answers. */
|
|
22
|
+
readonly moduleId: string;
|
|
23
|
+
/** The importing module's id as the build graph tracks it. */
|
|
24
|
+
readonly importer: string;
|
|
25
|
+
/** The authored `.css` specifier the generated module imports verbatim. */
|
|
26
|
+
readonly specifier: string;
|
|
27
|
+
/** The sheet file's absolute path — where the intercepting build reads the bytes. */
|
|
28
|
+
readonly file: string;
|
|
29
|
+
}
|
|
30
|
+
/** Register one generated edge (idempotent per module id — recompiles overwrite). */
|
|
31
|
+
export declare function registerStyleRequest(request: RegisteredStyleRequest): void;
|
|
32
|
+
/** The edge a resolved sheet module id carries, if any. */
|
|
33
|
+
export declare function getStyleRequest(moduleId: string): RegisteredStyleRequest | undefined;
|
|
34
|
+
/** True when the importing module has registered edges (query suffixes ignored). */
|
|
35
|
+
export declare function hasStyleImporter(importerId: string): boolean;
|
|
36
|
+
/** Every edge whose sheet file is the given path — the dev HMR reverse lookup. */
|
|
37
|
+
export declare function styleEdgesForFile(file: string): RegisteredStyleRequest[];
|
|
38
|
+
/** Drop every registered edge (a build's bookkeeping is its own lifetime). */
|
|
39
|
+
export declare function clearStyleRequests(): void;
|
|
40
|
+
/**
|
|
41
|
+
* The registry key for one authored edge: the sheet file's resolved path.
|
|
42
|
+
* Query suffixes on the importer id (dev-time cache busting) are stripped,
|
|
43
|
+
* so recompiles re-register under one key; the full relative path keeps
|
|
44
|
+
* same-named sheets in sibling subdirectories distinct edges.
|
|
45
|
+
*/
|
|
46
|
+
export declare function styleRequestModuleId(importerId: string, specifier: string): string;
|
|
47
|
+
/**
|
|
48
|
+
* The sheet file's absolute path for one authored edge: the importing
|
|
49
|
+
* module's directory joined with the authored specifier. Relative specifiers
|
|
50
|
+
* only (the compiler refuses bare ones), so the join is total.
|
|
51
|
+
*/
|
|
52
|
+
export declare function styleRequestFile(importerId: string, specifier: string): string;
|
|
53
|
+
//#endregion
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
//#region src/internal/compiler/style-requests.ts
|
|
2
|
+
const requests = /* @__PURE__ */ new Map();
|
|
3
|
+
/** Second index: which importing modules carry edges (the intercept's gate). */
|
|
4
|
+
const importers = /* @__PURE__ */ new Set();
|
|
5
|
+
/** Register one generated edge (idempotent per module id — recompiles overwrite). */
|
|
6
|
+
function registerStyleRequest(request) {
|
|
7
|
+
requests.set(request.moduleId, request);
|
|
8
|
+
importers.add(cleanImporterId(request.importer));
|
|
9
|
+
}
|
|
10
|
+
/** The edge a resolved sheet module id carries, if any. */
|
|
11
|
+
function getStyleRequest(moduleId) {
|
|
12
|
+
return requests.get(moduleId);
|
|
13
|
+
}
|
|
14
|
+
/** True when the importing module has registered edges (query suffixes ignored). */
|
|
15
|
+
function hasStyleImporter(importerId) {
|
|
16
|
+
return importers.has(cleanImporterId(importerId));
|
|
17
|
+
}
|
|
18
|
+
/** Every edge whose sheet file is the given path — the dev HMR reverse lookup. */
|
|
19
|
+
function styleEdgesForFile(file) {
|
|
20
|
+
const clean = file.split("?", 1)[0];
|
|
21
|
+
return [...requests.values()].filter((request) => request.moduleId === clean);
|
|
22
|
+
}
|
|
23
|
+
/** Drop every registered edge (a build's bookkeeping is its own lifetime). */
|
|
24
|
+
function clearStyleRequests() {
|
|
25
|
+
requests.clear();
|
|
26
|
+
importers.clear();
|
|
27
|
+
}
|
|
28
|
+
function cleanImporterId(importerId) {
|
|
29
|
+
return importerId.split("?", 1)[0];
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The registry key for one authored edge: the sheet file's resolved path.
|
|
33
|
+
* Query suffixes on the importer id (dev-time cache busting) are stripped,
|
|
34
|
+
* so recompiles re-register under one key; the full relative path keeps
|
|
35
|
+
* same-named sheets in sibling subdirectories distinct edges.
|
|
36
|
+
*/
|
|
37
|
+
function styleRequestModuleId(importerId, specifier) {
|
|
38
|
+
return styleRequestFile(importerId, specifier);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The sheet file's absolute path for one authored edge: the importing
|
|
42
|
+
* module's directory joined with the authored specifier. Relative specifiers
|
|
43
|
+
* only (the compiler refuses bare ones), so the join is total.
|
|
44
|
+
*/
|
|
45
|
+
function styleRequestFile(importerId, specifier) {
|
|
46
|
+
const clean = cleanImporterId(importerId);
|
|
47
|
+
return `${clean.slice(0, clean.lastIndexOf("/"))}/${specifier.replace(/^\.\//, "")}`;
|
|
48
|
+
}
|
|
49
|
+
//#endregion
|
|
50
|
+
export { clearStyleRequests, getStyleRequest, hasStyleImporter, registerStyleRequest, styleEdgesForFile, styleRequestFile, styleRequestModuleId };
|
package/src/vite.d.ts
ADDED
package/src/vite.js
ADDED