@analogjs/vite-plugin-angular 3.0.0-alpha.24 → 3.0.0-alpha.26
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/package.json +1 -1
- package/src/index.d.ts +0 -1
- package/src/index.js.map +1 -1
- package/src/lib/angular-jit-plugin.js +8 -2
- package/src/lib/angular-jit-plugin.js.map +1 -1
- package/src/lib/angular-vite-plugin.d.ts +69 -3
- package/src/lib/angular-vite-plugin.js +1084 -92
- package/src/lib/angular-vite-plugin.js.map +1 -1
- package/src/lib/component-resolvers.d.ts +16 -0
- package/src/lib/component-resolvers.js +77 -16
- package/src/lib/component-resolvers.js.map +1 -1
- package/src/lib/host.d.ts +3 -3
- package/src/lib/host.js +43 -19
- package/src/lib/host.js.map +1 -1
- package/src/lib/plugins/file-replacements.plugin.js +6 -1
- package/src/lib/plugins/file-replacements.plugin.js.map +1 -1
- package/src/lib/style-pipeline.d.ts +15 -0
- package/src/lib/style-pipeline.js +31 -0
- package/src/lib/style-pipeline.js.map +1 -0
- package/src/lib/style-preprocessor.d.ts +35 -1
- package/src/lib/style-preprocessor.js +35 -0
- package/src/lib/style-preprocessor.js.map +1 -0
- package/src/lib/stylesheet-registry.d.ts +73 -0
- package/src/lib/stylesheet-registry.js +168 -0
- package/src/lib/stylesheet-registry.js.map +1 -0
- package/src/lib/utils/debug.d.ts +7 -2
- package/src/lib/utils/debug.js +13 -3
- package/src/lib/utils/debug.js.map +1 -1
- package/src/lib/utils/devkit.d.ts +4 -4
- package/src/lib/utils/devkit.js.map +1 -1
|
@@ -1,7 +1,23 @@
|
|
|
1
|
+
export interface AngularComponentMetadata {
|
|
2
|
+
className: string;
|
|
3
|
+
selector?: string;
|
|
4
|
+
styleUrls: string[];
|
|
5
|
+
templateUrls: string[];
|
|
6
|
+
inlineTemplates: string[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Extract Angular component identities from raw source code before Angular's
|
|
10
|
+
* compilation pipeline strips decorators. This is used for dev-time
|
|
11
|
+
* diagnostics such as duplicate selectors, duplicate component class names,
|
|
12
|
+
* selectorless shared components, and inline-template validation.
|
|
13
|
+
*/
|
|
14
|
+
export declare function getAngularComponentMetadata(code: string): AngularComponentMetadata[];
|
|
1
15
|
/** Extract all `styleUrl` / `styleUrls` values from Angular component source. */
|
|
2
16
|
export declare function getStyleUrls(code: string): string[];
|
|
3
17
|
/** Extract all `templateUrl` values from Angular component source. */
|
|
4
18
|
export declare function getTemplateUrls(code: string): string[];
|
|
19
|
+
/** Extract inline `template` strings from Angular component source. */
|
|
20
|
+
export declare function getInlineTemplates(code: string): string[];
|
|
5
21
|
export declare class StyleUrlsResolver {
|
|
6
22
|
private readonly styleUrlsCache;
|
|
7
23
|
resolve(code: string, id: string): string[];
|
|
@@ -34,27 +34,88 @@ function collectComponentUrls(code) {
|
|
|
34
34
|
const { program } = parseSync("cmp.ts", code);
|
|
35
35
|
const styleUrls = [];
|
|
36
36
|
const templateUrls = [];
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
if (
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
37
|
+
const inlineTemplates = [];
|
|
38
|
+
new Visitor({ ClassDeclaration(node) {
|
|
39
|
+
const decorators = node.decorators ?? [];
|
|
40
|
+
for (const decorator of decorators) {
|
|
41
|
+
const expression = decorator.expression;
|
|
42
|
+
if (expression?.type !== "CallExpression" || expression.callee?.type !== "Identifier" || expression.callee.name !== "Component") continue;
|
|
43
|
+
const componentArg = expression.arguments?.[0];
|
|
44
|
+
if (componentArg?.type !== "ObjectExpression") continue;
|
|
45
|
+
for (const property of componentArg.properties ?? []) {
|
|
46
|
+
if (property?.type !== "Property" || property.key?.type !== "Identifier") continue;
|
|
47
|
+
const name = property.key.name;
|
|
48
|
+
if (name === "styleUrls" && property.value?.type === "ArrayExpression") for (const el of property.value.elements) {
|
|
49
|
+
const val = getStringValue(el);
|
|
50
|
+
if (val !== void 0) styleUrls.push(val);
|
|
51
|
+
}
|
|
52
|
+
if (name === "styleUrl") {
|
|
53
|
+
const val = getStringValue(property.value);
|
|
54
|
+
if (val !== void 0) styleUrls.push(val);
|
|
55
|
+
}
|
|
56
|
+
if (name === "templateUrl") {
|
|
57
|
+
const val = getStringValue(property.value);
|
|
58
|
+
if (val !== void 0) templateUrls.push(val);
|
|
59
|
+
}
|
|
60
|
+
if (name === "template") {
|
|
61
|
+
const val = getStringValue(property.value);
|
|
62
|
+
if (val !== void 0) inlineTemplates.push(val);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
51
65
|
}
|
|
52
66
|
} }).visit(program);
|
|
53
67
|
return {
|
|
54
68
|
styleUrls,
|
|
55
|
-
templateUrls
|
|
69
|
+
templateUrls,
|
|
70
|
+
inlineTemplates
|
|
56
71
|
};
|
|
57
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Extract Angular component identities from raw source code before Angular's
|
|
75
|
+
* compilation pipeline strips decorators. This is used for dev-time
|
|
76
|
+
* diagnostics such as duplicate selectors, duplicate component class names,
|
|
77
|
+
* selectorless shared components, and inline-template validation.
|
|
78
|
+
*/
|
|
79
|
+
function getAngularComponentMetadata(code) {
|
|
80
|
+
const { program } = parseSync("cmp.ts", code);
|
|
81
|
+
const components = [];
|
|
82
|
+
new Visitor({ ClassDeclaration(node) {
|
|
83
|
+
const decorators = node.decorators ?? [];
|
|
84
|
+
for (const decorator of decorators) {
|
|
85
|
+
const expression = decorator.expression;
|
|
86
|
+
if (expression?.type !== "CallExpression" || expression.callee?.type !== "Identifier" || expression.callee.name !== "Component") continue;
|
|
87
|
+
const componentArg = expression.arguments?.[0];
|
|
88
|
+
if (componentArg?.type !== "ObjectExpression") continue;
|
|
89
|
+
const metadata = {
|
|
90
|
+
className: node.id?.name ?? "(anonymous)",
|
|
91
|
+
styleUrls: [],
|
|
92
|
+
templateUrls: [],
|
|
93
|
+
inlineTemplates: []
|
|
94
|
+
};
|
|
95
|
+
for (const property of componentArg.properties ?? []) {
|
|
96
|
+
if (property?.type !== "Property" || property.key?.type !== "Identifier") continue;
|
|
97
|
+
const name = property.key.name;
|
|
98
|
+
if (name === "selector") metadata.selector = getStringValue(property.value);
|
|
99
|
+
else if (name === "styleUrl") {
|
|
100
|
+
const val = getStringValue(property.value);
|
|
101
|
+
if (val !== void 0) metadata.styleUrls.push(val);
|
|
102
|
+
} else if (name === "styleUrls" && property.value?.type === "ArrayExpression") for (const el of property.value.elements ?? []) {
|
|
103
|
+
const val = getStringValue(el);
|
|
104
|
+
if (val !== void 0) metadata.styleUrls.push(val);
|
|
105
|
+
}
|
|
106
|
+
else if (name === "templateUrl") {
|
|
107
|
+
const val = getStringValue(property.value);
|
|
108
|
+
if (val !== void 0) metadata.templateUrls.push(val);
|
|
109
|
+
} else if (name === "template") {
|
|
110
|
+
const val = getStringValue(property.value);
|
|
111
|
+
if (val !== void 0) metadata.inlineTemplates.push(val);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
components.push(metadata);
|
|
115
|
+
}
|
|
116
|
+
} }).visit(program);
|
|
117
|
+
return components;
|
|
118
|
+
}
|
|
58
119
|
/** Extract all `styleUrl` / `styleUrls` values from Angular component source. */
|
|
59
120
|
function getStyleUrls(code) {
|
|
60
121
|
return collectComponentUrls(code).styleUrls;
|
|
@@ -93,6 +154,6 @@ var TemplateUrlsResolver = class {
|
|
|
93
154
|
}
|
|
94
155
|
};
|
|
95
156
|
//#endregion
|
|
96
|
-
export { StyleUrlsResolver, TemplateUrlsResolver };
|
|
157
|
+
export { StyleUrlsResolver, TemplateUrlsResolver, getAngularComponentMetadata };
|
|
97
158
|
|
|
98
159
|
//# sourceMappingURL=component-resolvers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"component-resolvers.js","names":[],"sources":["../../../src/lib/component-resolvers.ts"],"sourcesContent":["import { dirname, resolve } from 'node:path';\n// OXC parser (native Rust, NAPI-RS) replaces ts-morph for AST extraction.\n// It is ~10-50x faster for the narrow task of pulling property values from\n// Angular component decorators. The Visitor helper from Rolldown walks the\n// ESTree-compatible AST that OXC produces.\nimport { parseSync } from 'oxc-parser';\nimport { Visitor } from 'rolldown/utils';\nimport { normalizePath } from 'vite';\n\n// ---------------------------------------------------------------------------\n// AST helpers\n// ---------------------------------------------------------------------------\n\n/**\n * Extracts a string value from an ESTree AST node.\n *\n * Handles three forms that Angular decorators may use:\n * - `Literal` with a string value → `'./foo.css'` / `\"./foo.css\"`\n * - `StringLiteral` (OXC-specific) → same representation\n * - `TemplateLiteral` with zero expressions → `` `./foo.css` ``\n *\n * Uses `any` because OXC's AST mixes standard ESTree nodes with\n * OXC-specific variants (e.g. `StringLiteral`), and the project's\n * tsconfig enforces `noPropertyAccessFromIndexSignature`.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction getStringValue(node: any): string | undefined {\n if (!node) return undefined;\n // Standard ESTree Literal (string value)\n if (node.type === 'Literal' && typeof node.value === 'string') {\n return node.value;\n }\n // OXC-specific StringLiteral node\n if (node.type === 'StringLiteral') {\n return node.value;\n }\n // Template literal with no interpolation (e.g., `./foo.css`)\n if (\n node.type === 'TemplateLiteral' &&\n node.expressions.length === 0 &&\n node.quasis.length === 1\n ) {\n return node.quasis[0].value.cooked ?? node.quasis[0].value.raw;\n }\n return undefined;\n}\n\n/**\n * Parses TypeScript/JS source with OXC and collects `styleUrl`, `styleUrls`,\n * and `templateUrl` property values from Angular `@Component()` decorators\n * in a single AST pass.\n *\n * This replaces the previous ts-morph implementation — OXC parses natively\n * via Rust NAPI bindings, avoiding the overhead of spinning up a full\n * TypeScript `Project` for each file.\n */\nfunction collectComponentUrls(code: string): {\n styleUrls: string[];\n templateUrls: string[];\n} {\n const { program } = parseSync('cmp.ts', code);\n const styleUrls: string[] = [];\n const templateUrls: string[] = [];\n\n const visitor = new Visitor({\n // The Visitor callback receives raw ESTree nodes. We use `any`\n // because OXC's AST includes non-standard node variants and the\n // project tsconfig enforces `noPropertyAccessFromIndexSignature`.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n Property(node: any) {\n if (node.key?.type !== 'Identifier') return;\n const name: string = node.key.name;\n\n if (name === 'styleUrls' && node.value?.type === 'ArrayExpression') {\n for (const el of node.value.elements) {\n const val = getStringValue(el);\n if (val !== undefined) styleUrls.push(val);\n }\n }\n\n if (name === 'styleUrl') {\n const val = getStringValue(node.value);\n if (val !== undefined) styleUrls.push(val);\n }\n\n if (name === 'templateUrl') {\n const val = getStringValue(node.value);\n if (val !== undefined) templateUrls.push(val);\n }\n },\n });\n visitor.visit(program);\n\n return { styleUrls, templateUrls };\n}\n\n/** Extract all `styleUrl` / `styleUrls` values from Angular component source. */\nexport function getStyleUrls(code: string): string[] {\n return collectComponentUrls(code).styleUrls;\n}\n\n/** Extract all `templateUrl` values from Angular component source. */\nexport function getTemplateUrls(code: string): string[] {\n return collectComponentUrls(code).templateUrls;\n}\n\n// ---------------------------------------------------------------------------\n// Resolver caches\n// ---------------------------------------------------------------------------\n\ninterface StyleUrlsCacheEntry {\n matchedStyleUrls: string[];\n styleUrls: string[];\n}\n\nexport class StyleUrlsResolver {\n // These resolvers may be called multiple times during the same\n // compilation for the same files. Caching is required because these\n // resolvers use synchronous system calls to the filesystem, which can\n // degrade performance when running compilations for multiple files.\n private readonly styleUrlsCache = new Map<string, StyleUrlsCacheEntry>();\n\n resolve(code: string, id: string): string[] {\n const matchedStyleUrls = getStyleUrls(code);\n const entry = this.styleUrlsCache.get(id);\n // We're using `matchedStyleUrls` as a key because the code may be changing continuously,\n // resulting in the resolver being called multiple times. While the code changes, the\n // `styleUrls` may remain constant, which means we should always return the previously\n // resolved style URLs.\n if (entry && entry.matchedStyleUrls === matchedStyleUrls) {\n return entry.styleUrls;\n }\n\n const styleUrls = matchedStyleUrls.map((styleUrlPath) => {\n return `${styleUrlPath}|${normalizePath(\n resolve(dirname(id), styleUrlPath),\n )}`;\n });\n\n this.styleUrlsCache.set(id, { styleUrls, matchedStyleUrls });\n return styleUrls;\n }\n}\n\ninterface TemplateUrlsCacheEntry {\n code: string;\n templateUrlPaths: string[];\n}\n\nexport class TemplateUrlsResolver {\n private readonly templateUrlsCache = new Map<\n string,\n TemplateUrlsCacheEntry\n >();\n\n resolve(code: string, id: string): string[] {\n const entry = this.templateUrlsCache.get(id);\n if (entry?.code === code) {\n return entry.templateUrlPaths;\n }\n\n const templateUrlPaths = getTemplateUrls(code).map(\n (url) => `${url}|${normalizePath(resolve(dirname(id), url))}`,\n );\n\n this.templateUrlsCache.set(id, { code, templateUrlPaths });\n return templateUrlPaths;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA0BA,SAAS,eAAe,MAA+B;AACrD,KAAI,CAAC,KAAM,QAAO,KAAA;AAElB,KAAI,KAAK,SAAS,aAAa,OAAO,KAAK,UAAU,SACnD,QAAO,KAAK;AAGd,KAAI,KAAK,SAAS,gBAChB,QAAO,KAAK;AAGd,KACE,KAAK,SAAS,qBACd,KAAK,YAAY,WAAW,KAC5B,KAAK,OAAO,WAAW,EAEvB,QAAO,KAAK,OAAO,GAAG,MAAM,UAAU,KAAK,OAAO,GAAG,MAAM;;;;;;;;;;;AAc/D,SAAS,qBAAqB,MAG5B;CACA,MAAM,EAAE,YAAY,UAAU,UAAU,KAAK;CAC7C,MAAM,YAAsB,EAAE;CAC9B,MAAM,eAAyB,EAAE;AAEjB,KAAI,QAAQ,EAK1B,SAAS,MAAW;AAClB,MAAI,KAAK,KAAK,SAAS,aAAc;EACrC,MAAM,OAAe,KAAK,IAAI;AAE9B,MAAI,SAAS,eAAe,KAAK,OAAO,SAAS,kBAC/C,MAAK,MAAM,MAAM,KAAK,MAAM,UAAU;GACpC,MAAM,MAAM,eAAe,GAAG;AAC9B,OAAI,QAAQ,KAAA,EAAW,WAAU,KAAK,IAAI;;AAI9C,MAAI,SAAS,YAAY;GACvB,MA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":["import { dirname, resolve } from 'node:path';\n// OXC parser (native Rust, NAPI-RS) replaces ts-morph for AST extraction.\n// It is ~10-50x faster for the narrow task of pulling property values from\n// Angular component decorators. The Visitor helper from Rolldown walks the\n// ESTree-compatible AST that OXC produces.\nimport { parseSync } from 'oxc-parser';\nimport { Visitor } from 'rolldown/utils';\nimport { normalizePath } from 'vite';\n\n// ---------------------------------------------------------------------------\n// AST helpers\n// ---------------------------------------------------------------------------\n\n/**\n * Extracts a string value from an ESTree AST node.\n *\n * Handles three forms that Angular decorators may use:\n * - `Literal` with a string value → `'./foo.css'` / `\"./foo.css\"`\n * - `StringLiteral` (OXC-specific) → same representation\n * - `TemplateLiteral` with zero expressions → `` `./foo.css` ``\n *\n * Uses `any` because OXC's AST mixes standard ESTree nodes with\n * OXC-specific variants (e.g. `StringLiteral`), and the project's\n * tsconfig enforces `noPropertyAccessFromIndexSignature`.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction getStringValue(node: any): string | undefined {\n if (!node) return undefined;\n // Standard ESTree Literal (string value)\n if (node.type === 'Literal' && typeof node.value === 'string') {\n return node.value;\n }\n // OXC-specific StringLiteral node\n if (node.type === 'StringLiteral') {\n return node.value;\n }\n // Template literal with no interpolation (e.g., `./foo.css`)\n if (\n node.type === 'TemplateLiteral' &&\n node.expressions.length === 0 &&\n node.quasis.length === 1\n ) {\n return node.quasis[0].value.cooked ?? node.quasis[0].value.raw;\n }\n return undefined;\n}\n\n/**\n * Parses TypeScript/JS source with OXC and collects `styleUrl`, `styleUrls`,\n * and `templateUrl` property values from Angular `@Component()` decorators\n * in a single AST pass.\n *\n * This replaces the previous ts-morph implementation — OXC parses natively\n * via Rust NAPI bindings, avoiding the overhead of spinning up a full\n * TypeScript `Project` for each file.\n */\nfunction collectComponentUrls(code: string): {\n styleUrls: string[];\n templateUrls: string[];\n inlineTemplates: string[];\n} {\n const { program } = parseSync('cmp.ts', code);\n const styleUrls: string[] = [];\n const templateUrls: string[] = [];\n const inlineTemplates: string[] = [];\n\n const visitor = new Visitor({\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ClassDeclaration(node: any) {\n const decorators = node.decorators ?? [];\n for (const decorator of decorators) {\n const expression = decorator.expression;\n if (\n expression?.type !== 'CallExpression' ||\n expression.callee?.type !== 'Identifier' ||\n expression.callee.name !== 'Component'\n ) {\n continue;\n }\n\n const componentArg = expression.arguments?.[0];\n if (componentArg?.type !== 'ObjectExpression') {\n continue;\n }\n\n for (const property of componentArg.properties ?? []) {\n if (\n property?.type !== 'Property' ||\n property.key?.type !== 'Identifier'\n ) {\n continue;\n }\n\n const name = property.key.name;\n\n if (\n name === 'styleUrls' &&\n property.value?.type === 'ArrayExpression'\n ) {\n for (const el of property.value.elements) {\n const val = getStringValue(el);\n if (val !== undefined) styleUrls.push(val);\n }\n }\n\n if (name === 'styleUrl') {\n const val = getStringValue(property.value);\n if (val !== undefined) styleUrls.push(val);\n }\n\n if (name === 'templateUrl') {\n const val = getStringValue(property.value);\n if (val !== undefined) templateUrls.push(val);\n }\n\n if (name === 'template') {\n const val = getStringValue(property.value);\n if (val !== undefined) inlineTemplates.push(val);\n }\n }\n }\n },\n });\n visitor.visit(program);\n\n return { styleUrls, templateUrls, inlineTemplates };\n}\n\nexport interface AngularComponentMetadata {\n className: string;\n selector?: string;\n styleUrls: string[];\n templateUrls: string[];\n inlineTemplates: string[];\n}\n\n/**\n * Extract Angular component identities from raw source code before Angular's\n * compilation pipeline strips decorators. This is used for dev-time\n * diagnostics such as duplicate selectors, duplicate component class names,\n * selectorless shared components, and inline-template validation.\n */\nexport function getAngularComponentMetadata(\n code: string,\n): AngularComponentMetadata[] {\n const { program } = parseSync('cmp.ts', code);\n const components: AngularComponentMetadata[] = [];\n\n const visitor = new Visitor({\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ClassDeclaration(node: any) {\n const decorators = node.decorators ?? [];\n for (const decorator of decorators) {\n const expression = decorator.expression;\n if (\n expression?.type !== 'CallExpression' ||\n expression.callee?.type !== 'Identifier' ||\n expression.callee.name !== 'Component'\n ) {\n continue;\n }\n\n const componentArg = expression.arguments?.[0];\n if (componentArg?.type !== 'ObjectExpression') {\n continue;\n }\n\n const metadata: AngularComponentMetadata = {\n className: node.id?.name ?? '(anonymous)',\n styleUrls: [],\n templateUrls: [],\n inlineTemplates: [],\n };\n\n for (const property of componentArg.properties ?? []) {\n if (\n property?.type !== 'Property' ||\n property.key?.type !== 'Identifier'\n ) {\n continue;\n }\n\n const name = property.key.name;\n if (name === 'selector') {\n metadata.selector = getStringValue(property.value);\n } else if (name === 'styleUrl') {\n const val = getStringValue(property.value);\n if (val !== undefined) {\n metadata.styleUrls.push(val);\n }\n } else if (\n name === 'styleUrls' &&\n property.value?.type === 'ArrayExpression'\n ) {\n for (const el of property.value.elements ?? []) {\n const val = getStringValue(el);\n if (val !== undefined) {\n metadata.styleUrls.push(val);\n }\n }\n } else if (name === 'templateUrl') {\n const val = getStringValue(property.value);\n if (val !== undefined) {\n metadata.templateUrls.push(val);\n }\n } else if (name === 'template') {\n const val = getStringValue(property.value);\n if (val !== undefined) {\n metadata.inlineTemplates.push(val);\n }\n }\n }\n\n components.push(metadata);\n }\n },\n });\n visitor.visit(program);\n\n return components;\n}\n\n/** Extract all `styleUrl` / `styleUrls` values from Angular component source. */\nexport function getStyleUrls(code: string): string[] {\n return collectComponentUrls(code).styleUrls;\n}\n\n/** Extract all `templateUrl` values from Angular component source. */\nexport function getTemplateUrls(code: string): string[] {\n return collectComponentUrls(code).templateUrls;\n}\n\n/** Extract inline `template` strings from Angular component source. */\nexport function getInlineTemplates(code: string): string[] {\n return collectComponentUrls(code).inlineTemplates;\n}\n\n// ---------------------------------------------------------------------------\n// Resolver caches\n// ---------------------------------------------------------------------------\n\ninterface StyleUrlsCacheEntry {\n matchedStyleUrls: string[];\n styleUrls: string[];\n}\n\nexport class StyleUrlsResolver {\n // These resolvers may be called multiple times during the same\n // compilation for the same files. Caching is required because these\n // resolvers use synchronous system calls to the filesystem, which can\n // degrade performance when running compilations for multiple files.\n private readonly styleUrlsCache = new Map<string, StyleUrlsCacheEntry>();\n\n resolve(code: string, id: string): string[] {\n const matchedStyleUrls = getStyleUrls(code);\n const entry = this.styleUrlsCache.get(id);\n // We're using `matchedStyleUrls` as a key because the code may be changing continuously,\n // resulting in the resolver being called multiple times. While the code changes, the\n // `styleUrls` may remain constant, which means we should always return the previously\n // resolved style URLs.\n if (entry && entry.matchedStyleUrls === matchedStyleUrls) {\n return entry.styleUrls;\n }\n\n const styleUrls = matchedStyleUrls.map((styleUrlPath) => {\n return `${styleUrlPath}|${normalizePath(\n resolve(dirname(id), styleUrlPath),\n )}`;\n });\n\n this.styleUrlsCache.set(id, { styleUrls, matchedStyleUrls });\n return styleUrls;\n }\n}\n\ninterface TemplateUrlsCacheEntry {\n code: string;\n templateUrlPaths: string[];\n}\n\nexport class TemplateUrlsResolver {\n private readonly templateUrlsCache = new Map<\n string,\n TemplateUrlsCacheEntry\n >();\n\n resolve(code: string, id: string): string[] {\n const entry = this.templateUrlsCache.get(id);\n if (entry?.code === code) {\n return entry.templateUrlPaths;\n }\n\n const templateUrlPaths = getTemplateUrls(code).map(\n (url) => `${url}|${normalizePath(resolve(dirname(id), url))}`,\n );\n\n this.templateUrlsCache.set(id, { code, templateUrlPaths });\n return templateUrlPaths;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA0BA,SAAS,eAAe,MAA+B;AACrD,KAAI,CAAC,KAAM,QAAO,KAAA;AAElB,KAAI,KAAK,SAAS,aAAa,OAAO,KAAK,UAAU,SACnD,QAAO,KAAK;AAGd,KAAI,KAAK,SAAS,gBAChB,QAAO,KAAK;AAGd,KACE,KAAK,SAAS,qBACd,KAAK,YAAY,WAAW,KAC5B,KAAK,OAAO,WAAW,EAEvB,QAAO,KAAK,OAAO,GAAG,MAAM,UAAU,KAAK,OAAO,GAAG,MAAM;;;;;;;;;;;AAc/D,SAAS,qBAAqB,MAI5B;CACA,MAAM,EAAE,YAAY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tD,QAAO,qBAAqB,KAAK,CAAC;;AAiBpC,IAAa,oBAAb,MAA+B;CAK7B,iCAAkC,IAAI,KAAkC;CAExE,QAAQ,MAAc,IAAsB;EAC1C,MAAM,mBAAmB,aAAa,KAAK;EAC3C,MAAM,QAAQ,KAAK,eAAe,IAAI,GAAG;AAKzC,MAAI,SAAS,MAAM,qBAAqB,iBACtC,QAAO,MAAM;EAGf,MAAM,YAAY,iBAAiB,KAAK,iBAAiB;AACvD,UAAO,GAAG,aAAa,GAAG,cACxB,QAAQ,QAAQ,GAAG,EAAE,aAAa,CACnC;IACD;AAEF,OAAK,eAAe,IAAI,IAAI;GAAE;GAAW;GAAkB,CAAC;AAC5D,SAAO;;;AASX,IAAa,uBAAb,MAAkC;CAChC,oCAAqC,IAAI,KAGtC;CAEH,QAAQ,MAAc,IAAsB;EAC1C,MAAM,QAAQ,KAAK,kBAAkB,IAAI,GAAG;AAC5C,MAAI,OAAO,SAAS,KAClB,QAAO,MAAM;EAGf,MAAM,mBAAmB,gBAAgB,KAAK,CAAC,KAC5C,QAAQ,GAAG,IAAI,GAAG,cAAc,QAAQ,QAAQ,GAAG,EAAE,IAAI,CAAC,GAC5D;AAED,OAAK,kBAAkB,IAAI,IAAI;GAAE;GAAM;GAAkB,CAAC;AAC1D,SAAO"}
|
package/src/lib/host.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import * as ts from "typescript";
|
|
2
|
-
import type
|
|
2
|
+
import { type StylePreprocessor } from "./style-preprocessor.js";
|
|
3
|
+
import { AnalogStylesheetRegistry } from "./stylesheet-registry.js";
|
|
3
4
|
import type { SourceFileCache } from "./utils/source-file-cache.js";
|
|
4
5
|
export declare function augmentHostWithResources(host: ts.CompilerHost, transform: (code: string, id: string, options?: {
|
|
5
6
|
ssr?: boolean;
|
|
6
7
|
}) => ReturnType<any> | null, options: {
|
|
7
8
|
inlineStylesExtension: string;
|
|
8
9
|
isProd?: boolean;
|
|
9
|
-
|
|
10
|
-
externalComponentStyles?: Map<string, string>;
|
|
10
|
+
stylesheetRegistry?: AnalogStylesheetRegistry;
|
|
11
11
|
sourceFileCache?: SourceFileCache;
|
|
12
12
|
stylePreprocessor?: StylePreprocessor;
|
|
13
13
|
}): void;
|
package/src/lib/host.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { debugStyles } from "./utils/debug.js";
|
|
2
|
+
import { normalizeStylesheetDependencies } from "./style-preprocessor.js";
|
|
3
|
+
import { preprocessStylesheetResult, registerStylesheetContent } from "./stylesheet-registry.js";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
2
5
|
import path from "node:path";
|
|
3
6
|
import { normalizePath } from "vite";
|
|
4
|
-
import { createHash } from "node:crypto";
|
|
5
7
|
//#region packages/vite-plugin-angular/src/lib/host.ts
|
|
6
8
|
function augmentHostWithResources(host, transform, options) {
|
|
7
9
|
const resourceHost = host;
|
|
@@ -17,36 +19,58 @@ function augmentHostWithResources(host, transform, options) {
|
|
|
17
19
|
resourceHost.transformResource = async function(data, context) {
|
|
18
20
|
if (context.type !== "style") return null;
|
|
19
21
|
const filename = context.resourceFile ?? context.containingFile.replace(".ts", `.${options?.inlineStylesExtension}`);
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
const preprocessed = preprocessStylesheetResult(data, filename, options.stylePreprocessor, {
|
|
23
|
+
filename,
|
|
24
|
+
containingFile: context.containingFile,
|
|
25
|
+
resourceFile: context.resourceFile ?? void 0,
|
|
26
|
+
className: context.className,
|
|
27
|
+
order: context.order,
|
|
28
|
+
inline: !context.resourceFile
|
|
29
|
+
});
|
|
30
|
+
if (options.stylesheetRegistry) {
|
|
31
|
+
const stylesheetId = registerStylesheetContent(options.stylesheetRegistry, {
|
|
32
|
+
code: preprocessed.code,
|
|
33
|
+
dependencies: normalizeStylesheetDependencies(preprocessed.dependencies),
|
|
34
|
+
diagnostics: preprocessed.diagnostics,
|
|
35
|
+
tags: preprocessed.tags,
|
|
36
|
+
containingFile: context.containingFile,
|
|
37
|
+
className: context.className,
|
|
38
|
+
order: context.order,
|
|
39
|
+
inlineStylesExtension: options.inlineStylesExtension,
|
|
40
|
+
resourceFile: context.resourceFile ?? void 0
|
|
41
|
+
});
|
|
42
|
+
debugStyles("NgtscProgram: stylesheet deferred to Vite pipeline", {
|
|
25
43
|
stylesheetId,
|
|
26
|
-
resourceFile: context.resourceFile ?? "(inline)"
|
|
44
|
+
resourceFile: context.resourceFile ?? "(inline)",
|
|
45
|
+
dependencies: preprocessed.dependencies,
|
|
46
|
+
diagnostics: preprocessed.diagnostics,
|
|
47
|
+
tags: preprocessed.tags
|
|
27
48
|
});
|
|
28
49
|
return { content: stylesheetId };
|
|
29
50
|
}
|
|
30
|
-
debugStyles("NgtscProgram: stylesheet processed inline via transform
|
|
51
|
+
debugStyles("NgtscProgram: stylesheet processed inline via transform", {
|
|
31
52
|
filename,
|
|
32
53
|
resourceFile: context.resourceFile ?? "(inline)",
|
|
33
|
-
dataLength:
|
|
54
|
+
dataLength: preprocessed.code.length
|
|
34
55
|
});
|
|
35
56
|
let stylesheetResult;
|
|
36
57
|
try {
|
|
37
|
-
stylesheetResult = await transform(
|
|
58
|
+
stylesheetResult = await transform(preprocessed.code, `${filename}?direct`);
|
|
38
59
|
} catch (e) {
|
|
39
|
-
|
|
60
|
+
debugStyles("NgtscProgram: stylesheet transform error", {
|
|
61
|
+
filename,
|
|
62
|
+
resourceFile: context.resourceFile ?? "(inline)",
|
|
63
|
+
error: String(e)
|
|
64
|
+
});
|
|
40
65
|
}
|
|
41
|
-
|
|
66
|
+
if (!stylesheetResult?.code) return null;
|
|
67
|
+
return { content: stylesheetResult.code };
|
|
42
68
|
};
|
|
43
|
-
resourceHost.resourceNameToFileName = function(resourceName, containingFile) {
|
|
44
|
-
const resolvedPath = path.join(path.dirname(containingFile), resourceName);
|
|
45
|
-
if (!options.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const filename = externalId + path.extname(resolvedPath);
|
|
49
|
-
options.externalComponentStyles.set(filename, resolvedPath);
|
|
69
|
+
resourceHost.resourceNameToFileName = function(resourceName, containingFile, fallbackResolve) {
|
|
70
|
+
const resolvedPath = normalizePath(fallbackResolve ? fallbackResolve(path.dirname(containingFile), resourceName) : path.join(path.dirname(containingFile), resourceName));
|
|
71
|
+
if (!options.stylesheetRegistry || !hasStyleExtension(resolvedPath)) return resolvedPath;
|
|
72
|
+
const filename = createHash("sha256").update(resolvedPath).digest("hex") + path.extname(resolvedPath);
|
|
73
|
+
options.stylesheetRegistry.registerExternalRequest(filename, resolvedPath);
|
|
50
74
|
debugStyles("NgtscProgram: external stylesheet ID mapped for resolveId", {
|
|
51
75
|
resourceName,
|
|
52
76
|
resolvedPath,
|
package/src/lib/host.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"host.js","names":[],"sources":["../../../src/lib/host.ts"],"sourcesContent":["import type { CompilerHost } from '@angular/compiler-cli';\nimport { normalizePath } from 'vite';\n\nimport * as ts from 'typescript';\n\nimport { createHash } from 'node:crypto';\nimport path from 'node:path';\nimport type
|
|
1
|
+
{"version":3,"file":"host.js","names":[],"sources":["../../../src/lib/host.ts"],"sourcesContent":["import type { CompilerHost } from '@angular/compiler-cli';\nimport { normalizePath } from 'vite';\n\nimport * as ts from 'typescript';\n\nimport { createHash } from 'node:crypto';\nimport path from 'node:path';\nimport {\n normalizeStylesheetDependencies,\n type StylePreprocessor,\n} from './style-preprocessor.js';\nimport {\n AnalogStylesheetRegistry,\n preprocessStylesheetResult,\n registerStylesheetContent,\n} from './stylesheet-registry.js';\nimport { debugStyles } from './utils/debug.js';\nimport type { SourceFileCache } from './utils/source-file-cache.js';\n\nexport function augmentHostWithResources(\n host: ts.CompilerHost,\n transform: (\n code: string,\n id: string,\n options?: { ssr?: boolean },\n ) => ReturnType<any> | null,\n options: {\n inlineStylesExtension: string;\n isProd?: boolean;\n stylesheetRegistry?: AnalogStylesheetRegistry;\n sourceFileCache?: SourceFileCache;\n stylePreprocessor?: StylePreprocessor;\n },\n): void {\n const resourceHost = host as CompilerHost;\n\n resourceHost.readResource = async function (fileName: string) {\n const filePath = normalizePath(fileName);\n\n const content = (this as any).readFile(filePath);\n\n if (content === undefined) {\n throw new Error('Unable to locate component resource: ' + fileName);\n }\n\n return content;\n };\n\n resourceHost.getModifiedResourceFiles = function () {\n return options?.sourceFileCache?.modifiedFiles;\n };\n\n resourceHost.transformResource = async function (data, context) {\n // Only style resources are supported currently\n if (context.type !== 'style') {\n return null;\n }\n\n const filename =\n context.resourceFile ??\n context.containingFile.replace(\n '.ts',\n `.${options?.inlineStylesExtension}`,\n );\n const preprocessed = preprocessStylesheetResult(\n data,\n filename,\n options.stylePreprocessor,\n {\n filename,\n containingFile: context.containingFile,\n resourceFile: context.resourceFile ?? undefined,\n className: context.className,\n order: context.order,\n inline: !context.resourceFile,\n },\n );\n\n // Externalized path: store preprocessed CSS for Vite's serve-time pipeline.\n // CSS must NOT be transformed here — the load hook returns it into\n // Vite's transform pipeline where PostCSS / Tailwind process it once.\n if (options.stylesheetRegistry) {\n const stylesheetId = registerStylesheetContent(\n options.stylesheetRegistry,\n {\n code: preprocessed.code,\n dependencies: normalizeStylesheetDependencies(\n preprocessed.dependencies,\n ),\n diagnostics: preprocessed.diagnostics,\n tags: preprocessed.tags,\n containingFile: context.containingFile,\n className: context.className,\n order: context.order,\n inlineStylesExtension: options.inlineStylesExtension,\n resourceFile: context.resourceFile ?? undefined,\n },\n );\n debugStyles('NgtscProgram: stylesheet deferred to Vite pipeline', {\n stylesheetId,\n resourceFile: context.resourceFile ?? '(inline)',\n dependencies: preprocessed.dependencies,\n diagnostics: preprocessed.diagnostics,\n tags: preprocessed.tags,\n });\n return { content: stylesheetId };\n }\n\n // Non-externalized: CSS is returned directly to the Angular compiler\n // and never re-enters Vite's pipeline, so transform eagerly.\n debugStyles('NgtscProgram: stylesheet processed inline via transform', {\n filename,\n resourceFile: context.resourceFile ?? '(inline)',\n dataLength: preprocessed.code.length,\n });\n let stylesheetResult;\n\n try {\n stylesheetResult = await transform(\n preprocessed.code,\n `${filename}?direct`,\n );\n } catch (e) {\n debugStyles('NgtscProgram: stylesheet transform error', {\n filename,\n resourceFile: context.resourceFile ?? '(inline)',\n error: String(e),\n });\n }\n\n if (!stylesheetResult?.code) {\n return null;\n }\n\n return { content: stylesheetResult.code };\n };\n\n resourceHost.resourceNameToFileName = function (\n resourceName,\n containingFile,\n fallbackResolve,\n ) {\n const resolvedPath = normalizePath(\n fallbackResolve\n ? fallbackResolve(path.dirname(containingFile), resourceName)\n : path.join(path.dirname(containingFile), resourceName),\n );\n\n // All resource names that have template file extensions are assumed to be templates\n if (!options.stylesheetRegistry || !hasStyleExtension(resolvedPath)) {\n return resolvedPath;\n }\n\n // For external stylesheets, create a unique identifier and store the mapping\n const externalId = createHash('sha256').update(resolvedPath).digest('hex');\n const filename = externalId + path.extname(resolvedPath);\n\n options.stylesheetRegistry.registerExternalRequest(filename, resolvedPath);\n debugStyles('NgtscProgram: external stylesheet ID mapped for resolveId', {\n resourceName,\n resolvedPath,\n filename,\n });\n\n return filename;\n };\n}\n\nexport function augmentProgramWithVersioning(program: ts.Program): void {\n const baseGetSourceFiles = program.getSourceFiles;\n program.getSourceFiles = function (...parameters) {\n const files: readonly (ts.SourceFile & { version?: string })[] =\n baseGetSourceFiles(...parameters);\n\n for (const file of files) {\n file.version ??= createHash('sha256').update(file.text).digest('hex');\n }\n\n return files;\n };\n}\n\nexport function augmentHostWithCaching(\n host: ts.CompilerHost,\n cache: Map<string, ts.SourceFile>,\n): void {\n const baseGetSourceFile = host.getSourceFile;\n host.getSourceFile = function (\n fileName,\n languageVersion,\n onError,\n shouldCreateNewSourceFile,\n ...parameters\n ) {\n if (!shouldCreateNewSourceFile && cache.has(fileName)) {\n return cache.get(fileName);\n }\n\n const file = baseGetSourceFile.call(\n host,\n fileName,\n languageVersion,\n onError,\n true,\n ...parameters,\n );\n\n if (file) {\n cache.set(fileName, file);\n }\n\n return file;\n };\n}\n\nexport function mergeTransformers(\n first: ts.CustomTransformers,\n second: ts.CustomTransformers,\n): ts.CustomTransformers {\n const result: ts.CustomTransformers = {};\n\n if (first.before || second.before) {\n result.before = [...(first.before || []), ...(second.before || [])];\n }\n\n if (first.after || second.after) {\n result.after = [...(first.after || []), ...(second.after || [])];\n }\n\n if (first.afterDeclarations || second.afterDeclarations) {\n result.afterDeclarations = [\n ...(first.afterDeclarations || []),\n ...(second.afterDeclarations || []),\n ];\n }\n\n return result;\n}\n\nfunction hasStyleExtension(file: string): boolean {\n const extension = path.extname(file).toLowerCase();\n\n switch (extension) {\n case '.css':\n case '.scss':\n return true;\n default:\n return false;\n }\n}\n"],"mappings":";;;;;;;AAmBA,SAAgB,yBACd,MACA,WAKA,SAOM;CACN,MAAM,eAAe;AAErB,cAAa,eAAe,eAAgB,UAAkB;EAC5D,MAAM,WAAW,cAAc,SAAS;EAExC,MAAM,UAAW,KAAa,SAAS,SAAS;AAEhD,MAAI,YAAY,KAAA,EACd,OAAM,IAAI,MAAM,0CAA0C,SAAS;AAGrE,SAAO;;AAGT,cAAa,2BAA2B,WAAY;AAClD,SAAO,SAAS,iBAAiB;;AAGnC,cAAa,oBAAoB,eAAgB,MAAM,SAAS;AAE9D,MAAI,QAAQ,SAAS,QACnB,QAAO;EAGT,MAAM,WACJ,QAAQ,gBACR,QAAQ,eAAe,QACrB,OACA,IAAI,SAAS,wBACd;EACH,MAAM,eAAe,2BACnB,MACA,UACA,QAAQ,mBACR;GACE;GACA,gBAAgB,QAAQ;GACxB,cAAc,QAAQ,gBAAgB,KAAA;GACtC,WAAW,QAAQ;GACnB,OAAO,QAAQ;GACf,QAAQ,CAAC,QAAQ;GAClB,CACF;AAKD,MAAI,QAAQ,oBAAoB;GAC9B,MAAM,eAAe,0BACnB,QAAQ,oBACR;IACE,MAAM,aAAa;IACnB,cAAc,gCACZ,aAAa,aACd;IACD,aAAa,aAAa;IAC1B,MAAM,a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AK,YAAY,WAAW,SAAS,CAAC,OAAO,KAAK,KAAK,CAAC,OAAO,MAAM;AAGvE,SAAO;;;AAIX,SAAgB,uBACd,MACA,OACM;CACN,MAAM,oBAAoB,KAAK;AAC/B,MAAK,gBAAgB,SACnB,UACA,iBACA,SACA,2BACA,GAAG,YACH;AACA,MAAI,CAAC,6BAA6B,MAAM,IAAI,SAAS,CACnD,QAAO,MAAM,IAAI,SAAS;EAG5B,MAAM,OAAO,kBAAkB,KAC7B,MACA,UACA,iBACA,SACA,MACA,GAAG,WACJ;AAED,MAAI,KACF,OAAM,IAAI,UAAU,KAAK;AAG3B,SAAO;;;AAIX,SAAgB,kBACd,OACA,QACuB;CACvB,MAAM,SAAgC,EAAE;AAExC,KAAI,MAAM,UAAU,OAAO,OACzB,QAAO,SAAS,CAAC,GAAI,MAAM,UAAU,EAAE,EAAG,GAAI,OAAO,UAAU,EAAE,CAAE;AAGrE,KAAI,MAAM,SAAS,OAAO,MACxB,QAAO,QAAQ,CAAC,GAAI,MAAM,SAAS,EAAE,EAAG,GAAI,OAAO,SAAS,EAAE,CAAE;AAGlE,KAAI,MAAM,qBAAqB,OAAO,kBACpC,QAAO,oBAAoB,CACzB,GAAI,MAAM,qBAAqB,EAAE,EACjC,GAAI,OAAO,qBAAqB,EAAE,CACnC;AAGH,QAAO;;AAGT,SAAS,kBAAkB,MAAuB;AAGhD,SAFkB,KAAK,QAAQ,KAAK,CAAC,aAAa,EAElD;EACE,KAAK;EACL,KAAK,QACH,QAAO;EACT,QACE,QAAO"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { debugCompiler } from "../utils/debug.js";
|
|
1
2
|
import { isAbsolute, resolve } from "node:path";
|
|
2
3
|
//#region packages/vite-plugin-angular/src/lib/plugins/file-replacements.plugin.ts
|
|
3
4
|
function replaceFiles(replacements, workspaceRoot) {
|
|
@@ -24,7 +25,11 @@ function replaceFiles(replacements, workspaceRoot) {
|
|
|
24
25
|
else if (foundReplace.ssr) return null;
|
|
25
26
|
return { id: foundReplace.with };
|
|
26
27
|
} catch (err) {
|
|
27
|
-
|
|
28
|
+
debugCompiler("file replacement error", {
|
|
29
|
+
error: String(err),
|
|
30
|
+
source,
|
|
31
|
+
importer
|
|
32
|
+
});
|
|
28
33
|
return null;
|
|
29
34
|
}
|
|
30
35
|
return null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-replacements.plugin.js","names":[],"sources":["../../../../src/lib/plugins/file-replacements.plugin.ts"],"sourcesContent":["// source: https://github.com/Myrmod/vitejs-theming/blob/master/build-plugins/rollup/replace-files.js\nimport { isAbsolute, resolve } from 'node:path';\nimport { Plugin } from 'vite';\n\nexport function replaceFiles(\n replacements: FileReplacement[],\n workspaceRoot: string,\n): Plugin | false {\n if (!replacements?.length) {\n return false;\n }\n\n return {\n name: 'rollup-plugin-replace-files',\n enforce: 'pre',\n async resolveId(source, importer, options) {\n const resolved = await this.resolve(source, importer, {\n ...options,\n skipSelf: true,\n });\n /**\n * The reason we're using endsWith here is because the resolved id\n * will be the absolute path to the file. We want to check if the\n * file ends with the file we're trying to replace, which will be essentially\n * the path from the root of our workspace.\n */\n const mappedReplacements = replacements.map((fr: FileReplacement) => {\n const frSSR = fr as FileReplacementSSR;\n const frWith = fr as FileReplacementWith;\n\n return {\n ...fr,\n ssr: frSSR.ssr\n ? isAbsolute(frSSR.ssr)\n ? frSSR.ssr\n : resolve(workspaceRoot, frSSR.ssr)\n : '',\n with: frWith.with\n ? isAbsolute(frWith.with)\n ? frWith.with\n : resolve(workspaceRoot, frWith.with)\n : '',\n };\n });\n const foundReplace = mappedReplacements.find((replacement) =>\n resolved?.id?.endsWith(replacement.replace),\n );\n if (foundReplace) {\n try {\n if (this.environment.name === 'ssr' && foundReplace.ssr) {\n // return new file id for ssr\n return {\n id: foundReplace.ssr,\n };\n } else if (foundReplace.ssr) {\n return null;\n }\n\n // return new file id\n return {\n id: foundReplace.with,\n };\n } catch (err) {\n
|
|
1
|
+
{"version":3,"file":"file-replacements.plugin.js","names":[],"sources":["../../../../src/lib/plugins/file-replacements.plugin.ts"],"sourcesContent":["// source: https://github.com/Myrmod/vitejs-theming/blob/master/build-plugins/rollup/replace-files.js\nimport { isAbsolute, resolve } from 'node:path';\nimport { Plugin } from 'vite';\nimport { debugCompiler } from '../utils/debug.js';\n\nexport function replaceFiles(\n replacements: FileReplacement[],\n workspaceRoot: string,\n): Plugin | false {\n if (!replacements?.length) {\n return false;\n }\n\n return {\n name: 'rollup-plugin-replace-files',\n enforce: 'pre',\n async resolveId(source, importer, options) {\n const resolved = await this.resolve(source, importer, {\n ...options,\n skipSelf: true,\n });\n /**\n * The reason we're using endsWith here is because the resolved id\n * will be the absolute path to the file. We want to check if the\n * file ends with the file we're trying to replace, which will be essentially\n * the path from the root of our workspace.\n */\n const mappedReplacements = replacements.map((fr: FileReplacement) => {\n const frSSR = fr as FileReplacementSSR;\n const frWith = fr as FileReplacementWith;\n\n return {\n ...fr,\n ssr: frSSR.ssr\n ? isAbsolute(frSSR.ssr)\n ? frSSR.ssr\n : resolve(workspaceRoot, frSSR.ssr)\n : '',\n with: frWith.with\n ? isAbsolute(frWith.with)\n ? frWith.with\n : resolve(workspaceRoot, frWith.with)\n : '',\n };\n });\n const foundReplace = mappedReplacements.find((replacement) =>\n resolved?.id?.endsWith(replacement.replace),\n );\n if (foundReplace) {\n try {\n if (this.environment.name === 'ssr' && foundReplace.ssr) {\n // return new file id for ssr\n return {\n id: foundReplace.ssr,\n };\n } else if (foundReplace.ssr) {\n return null;\n }\n\n // return new file id\n return {\n id: foundReplace.with,\n };\n } catch (err) {\n debugCompiler('file replacement error', {\n error: String(err),\n source,\n importer,\n });\n return null;\n }\n }\n return null;\n },\n };\n}\n\nexport type FileReplacement = FileReplacementWith | FileReplacementSSR;\n\nexport interface FileReplacementBase {\n replace: string;\n}\nexport interface FileReplacementWith extends FileReplacementBase {\n with: string;\n}\n\nexport interface FileReplacementSSR extends FileReplacementBase {\n ssr: string;\n}\n"],"mappings":";;;AAKA,SAAgB,aACd,cACA,eACgB;AAChB,KAAI,CAAC,cAAc,OACjB,QAAO;AAGT,QAAO;EACL,MAAM;EACN,SAAS;EACT,MAAM,UAAU,QAAQ,UAAU,SAAS;GACzC,MAAM,WAAW,MAAM,KAAK,QAAQ,QAAQ,UAAU;IACpD,GAAG;IACH,UAAU;IACX,CAAC;GAyBF,MAAM,eAlBqB,aAAa,KAAK,OAAwB;IACnE,MAAM,QAAQ;IACd,MAAM,SAAS;AAEf,WAAO;KACL,GAAG;KACH,KAAK,MAAM,MACP,WAAW,MAAM,IAAI,GACnB,MAAM,MACN,QAAQ,eAAe,MAAM,IAAI,GACnC;KACJ,MAAM,OAAO,OACT,WAAW,OAAO,KAAK,GACrB,OAAO,OACP,QAAQ,eAAe,OAAO,KAAK,GACrC;KACL;KACD,CACsC,MAAM,gBAC5C,UAAU,IAAI,SAAS,YAAY,QAAQ,CAC5C;AACD,OAAI,aACF,KAAI;AACF,QAAI,KAAK,YAAY,SAAS,SAAS,aAAa,IAElD,QAAO,EACL,IAAI,aAAa,KAClB;aACQ,aAAa,IACtB,QAAO;AAIT,WAAO,EACL,IAAI,aAAa,MAClB;YACM,KAAK;AACZ,kBAAc,0BAA0B;KACtC,OAAO,OAAO,IAAI;KAClB;KACA;KACD,CAAC;AACF,WAAO;;AAGX,UAAO;;EAEV"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { AnalogStylesheetRegistry } from "./stylesheet-registry.js";
|
|
2
|
+
import type { StylePipelineStylesheetRegistry, StylePreprocessor, StylesheetTransformContext, StylesheetTransformResult } from "./style-preprocessor.js";
|
|
3
|
+
export interface AngularStylePipelineContext {
|
|
4
|
+
workspaceRoot: string;
|
|
5
|
+
}
|
|
6
|
+
export interface AngularStylePipelinePlugin {
|
|
7
|
+
name: string;
|
|
8
|
+
preprocessStylesheet?: (code: string, context: StylesheetTransformContext) => string | StylesheetTransformResult | undefined;
|
|
9
|
+
configureStylesheetRegistry?: (registry: StylePipelineStylesheetRegistry, context: AngularStylePipelineContext) => void;
|
|
10
|
+
}
|
|
11
|
+
export interface AngularStylePipelineOptions {
|
|
12
|
+
plugins: AngularStylePipelinePlugin[];
|
|
13
|
+
}
|
|
14
|
+
export declare function stylePipelinePreprocessorFromPlugins(options: AngularStylePipelineOptions | undefined): StylePreprocessor | undefined;
|
|
15
|
+
export declare function configureStylePipelineRegistry(options: AngularStylePipelineOptions | undefined, registry: AnalogStylesheetRegistry, context: AngularStylePipelineContext): void;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { debugStylePipeline } from "./utils/debug.js";
|
|
2
|
+
import { normalizeStylesheetTransformResult } from "./style-preprocessor.js";
|
|
3
|
+
//#region packages/vite-plugin-angular/src/lib/style-pipeline.ts
|
|
4
|
+
function stylePipelinePreprocessorFromPlugins(options) {
|
|
5
|
+
const preprocessors = options?.plugins.map((plugin) => plugin.preprocessStylesheet).filter((preprocessor) => !!preprocessor) ?? [];
|
|
6
|
+
if (!preprocessors.length) return;
|
|
7
|
+
return (code, filename, context) => {
|
|
8
|
+
if (!context) {
|
|
9
|
+
debugStylePipeline("skipping community stylesheet preprocessors because Angular did not provide a stylesheet context", { filename });
|
|
10
|
+
return code;
|
|
11
|
+
}
|
|
12
|
+
let current = normalizeStylesheetTransformResult(void 0, code);
|
|
13
|
+
for (const preprocess of preprocessors) {
|
|
14
|
+
const next = normalizeStylesheetTransformResult(preprocess(current.code, context), current.code);
|
|
15
|
+
current = {
|
|
16
|
+
code: next.code,
|
|
17
|
+
dependencies: [...current.dependencies ?? [], ...next.dependencies ?? []],
|
|
18
|
+
diagnostics: [...current.diagnostics ?? [], ...next.diagnostics ?? []],
|
|
19
|
+
tags: [...current.tags ?? [], ...next.tags ?? []]
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
return current;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function configureStylePipelineRegistry(options, registry, context) {
|
|
26
|
+
for (const plugin of options?.plugins ?? []) plugin.configureStylesheetRegistry?.(registry, context);
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
export { configureStylePipelineRegistry, stylePipelinePreprocessorFromPlugins };
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=style-pipeline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"style-pipeline.js","names":[],"sources":["../../../src/lib/style-pipeline.ts"],"sourcesContent":["import type { AnalogStylesheetRegistry } from './stylesheet-registry.js';\nimport type {\n StylePipelineStylesheetRegistry,\n StylePreprocessor,\n StylesheetTransformContext,\n StylesheetTransformResult,\n} from './style-preprocessor.js';\nimport { normalizeStylesheetTransformResult } from './style-preprocessor.js';\nimport { debugStylePipeline } from './utils/debug.js';\n\nexport interface AngularStylePipelineContext {\n workspaceRoot: string;\n}\n\nexport interface AngularStylePipelinePlugin {\n name: string;\n preprocessStylesheet?: (\n code: string,\n context: StylesheetTransformContext,\n ) => string | StylesheetTransformResult | undefined;\n configureStylesheetRegistry?: (\n registry: StylePipelineStylesheetRegistry,\n context: AngularStylePipelineContext,\n ) => void;\n}\n\nexport interface AngularStylePipelineOptions {\n plugins: AngularStylePipelinePlugin[];\n}\n\nexport function stylePipelinePreprocessorFromPlugins(\n options: AngularStylePipelineOptions | undefined,\n): StylePreprocessor | undefined {\n const preprocessors =\n options?.plugins\n .map((plugin) => plugin.preprocessStylesheet)\n .filter((preprocessor) => !!preprocessor) ?? [];\n\n if (!preprocessors.length) {\n return undefined;\n }\n\n return (code, filename, context) => {\n if (!context) {\n debugStylePipeline(\n 'skipping community stylesheet preprocessors because Angular did not provide a stylesheet context',\n {\n filename,\n },\n );\n return code;\n }\n\n let current = normalizeStylesheetTransformResult(undefined, code);\n for (const preprocess of preprocessors) {\n const next = normalizeStylesheetTransformResult(\n preprocess(current.code, context),\n current.code,\n );\n current = {\n code: next.code,\n dependencies: [\n ...(current.dependencies ?? []),\n ...(next.dependencies ?? []),\n ],\n diagnostics: [\n ...(current.diagnostics ?? []),\n ...(next.diagnostics ?? []),\n ],\n tags: [...(current.tags ?? []), ...(next.tags ?? [])],\n };\n }\n\n return current;\n };\n}\n\nexport function configureStylePipelineRegistry(\n options: AngularStylePipelineOptions | undefined,\n registry: AnalogStylesheetRegistry,\n context: AngularStylePipelineContext,\n): void {\n for (const plugin of options?.plugins ?? []) {\n plugin.configureStylesheetRegistry?.(registry, context);\n }\n}\n"],"mappings":";;;AA8BA,SAAgB,qCACd,SAC+B;CAC/B,MAAM,gBACJ,SAAS,QACN,KAAK,WAAW,OAAO,qBAAqB,CAC5C,QAAQ,iBAAiB,CAAC,CAAC,aAAa,IAAI,EAAE;AAEnD,KAAI,CAAC,cAAc,OACjB;AAGF,SAAQ,MAAM,UAAU,YAAY;AAClC,MAAI,CAAC,SAAS;AACZ,sBACE,oGACA,EACE,UACD,CACF;AACD,UAAO;;EAGT,IAAI,UAAU,mCAAmC,KAAA,GAAW,KAAK;AACjE,OAAK,MAAM,cAAc,eAAe;GACtC,MAAM,OAAO,mCACX,WAAW,QAAQ,MAAM,QAAQ,EACjC,QAAQ,KACT;AACD,aAAU;IACR,MAAM,KAAK;IACX,cAAc,CACZ,GAAI,QAAQ,gBAAgB,EAAE,EAC9B,GAAI,KAAK,gBAAgB,EAAE,CAC5B;IACD,aAAa,CACX,GAAI,QAAQ,eAAe,EAAE,EAC7B,GAAI,KAAK,eAAe,EAAE,CAC3B;IACD,MAAM,CAAC,GAAI,QAAQ,QAAQ,EAAE,EAAG,GAAI,KAAK,QAAQ,EAAE,CAAE;IACtD;;AAGH,SAAO;;;AAIX,SAAgB,+BACd,SACA,UACA,SACM;AACN,MAAK,MAAM,UAAU,SAAS,WAAW,EAAE,CACzC,QAAO,8BAA8B,UAAU,QAAQ"}
|
|
@@ -1 +1,35 @@
|
|
|
1
|
-
export
|
|
1
|
+
export interface StylesheetTransformContext {
|
|
2
|
+
filename: string;
|
|
3
|
+
containingFile?: string;
|
|
4
|
+
resourceFile?: string;
|
|
5
|
+
className?: string;
|
|
6
|
+
order?: number;
|
|
7
|
+
inline: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface StylesheetDependency {
|
|
10
|
+
id: string;
|
|
11
|
+
kind?: "file" | "virtual" | "token" | "bridge" | "manifest" | "runtime";
|
|
12
|
+
owner?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface StylesheetDiagnostic {
|
|
15
|
+
severity: "warning" | "error";
|
|
16
|
+
code: string;
|
|
17
|
+
message: string;
|
|
18
|
+
}
|
|
19
|
+
export interface StylesheetTransformResult {
|
|
20
|
+
code: string;
|
|
21
|
+
dependencies?: Array<string | StylesheetDependency>;
|
|
22
|
+
diagnostics?: StylesheetDiagnostic[];
|
|
23
|
+
tags?: string[];
|
|
24
|
+
}
|
|
25
|
+
export interface StylePipelineStylesheetRegistry {
|
|
26
|
+
getPublicIdsForSource(sourcePath: string): string[];
|
|
27
|
+
getRequestIdsForSource(sourcePath: string): string[];
|
|
28
|
+
getDependenciesForSource(sourcePath: string): StylesheetDependency[];
|
|
29
|
+
getDiagnosticsForSource(sourcePath: string): StylesheetDiagnostic[];
|
|
30
|
+
getTagsForSource(sourcePath: string): string[];
|
|
31
|
+
}
|
|
32
|
+
export type StylePreprocessor = (code: string, filename: string, context?: StylesheetTransformContext) => string | StylesheetTransformResult;
|
|
33
|
+
export declare function normalizeStylesheetTransformResult(value: string | StylesheetTransformResult | undefined, fallbackCode: string): StylesheetTransformResult;
|
|
34
|
+
export declare function normalizeStylesheetDependencies(dependencies: Array<string | StylesheetDependency> | undefined): StylesheetDependency[];
|
|
35
|
+
export declare function composeStylePreprocessors(preprocessors: Array<StylePreprocessor | false | null | undefined>): StylePreprocessor | undefined;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//#region packages/vite-plugin-angular/src/lib/style-preprocessor.ts
|
|
2
|
+
function normalizeStylesheetTransformResult(value, fallbackCode) {
|
|
3
|
+
if (value == null) return { code: fallbackCode };
|
|
4
|
+
if (typeof value === "string") return { code: value };
|
|
5
|
+
return {
|
|
6
|
+
code: value.code ?? fallbackCode,
|
|
7
|
+
dependencies: value.dependencies ?? [],
|
|
8
|
+
diagnostics: value.diagnostics ?? [],
|
|
9
|
+
tags: value.tags ?? []
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function normalizeStylesheetDependencies(dependencies) {
|
|
13
|
+
return (dependencies ?? []).map((dependency) => typeof dependency === "string" ? { id: dependency } : dependency);
|
|
14
|
+
}
|
|
15
|
+
function composeStylePreprocessors(preprocessors) {
|
|
16
|
+
const active = preprocessors.filter((preprocessor) => !!preprocessor);
|
|
17
|
+
if (!active.length) return;
|
|
18
|
+
return (code, filename, context) => {
|
|
19
|
+
let current = normalizeStylesheetTransformResult(void 0, code);
|
|
20
|
+
for (const preprocessor of active) {
|
|
21
|
+
const next = normalizeStylesheetTransformResult(preprocessor(current.code, filename, context), current.code);
|
|
22
|
+
current = {
|
|
23
|
+
code: next.code,
|
|
24
|
+
dependencies: [...current.dependencies ?? [], ...next.dependencies ?? []],
|
|
25
|
+
diagnostics: [...current.diagnostics ?? [], ...next.diagnostics ?? []],
|
|
26
|
+
tags: [...current.tags ?? [], ...next.tags ?? []]
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return current;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
export { composeStylePreprocessors, normalizeStylesheetDependencies, normalizeStylesheetTransformResult };
|
|
34
|
+
|
|
35
|
+
//# sourceMappingURL=style-preprocessor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"style-preprocessor.js","names":[],"sources":["../../../src/lib/style-preprocessor.ts"],"sourcesContent":["export interface StylesheetTransformContext {\n filename: string;\n containingFile?: string;\n resourceFile?: string;\n className?: string;\n order?: number;\n inline: boolean;\n}\n\nexport interface StylesheetDependency {\n id: string;\n kind?: 'file' | 'virtual' | 'token' | 'bridge' | 'manifest' | 'runtime';\n owner?: string;\n}\n\nexport interface StylesheetDiagnostic {\n severity: 'warning' | 'error';\n code: string;\n message: string;\n}\n\nexport interface StylesheetTransformResult {\n code: string;\n dependencies?: Array<string | StylesheetDependency>;\n diagnostics?: StylesheetDiagnostic[];\n tags?: string[];\n}\n\nexport interface StylePipelineStylesheetRegistry {\n getPublicIdsForSource(sourcePath: string): string[];\n getRequestIdsForSource(sourcePath: string): string[];\n getDependenciesForSource(sourcePath: string): StylesheetDependency[];\n getDiagnosticsForSource(sourcePath: string): StylesheetDiagnostic[];\n getTagsForSource(sourcePath: string): string[];\n}\n\nexport type StylePreprocessor = (\n code: string,\n filename: string,\n context?: StylesheetTransformContext,\n) => string | StylesheetTransformResult;\n\nexport function normalizeStylesheetTransformResult(\n value: string | StylesheetTransformResult | undefined,\n fallbackCode: string,\n): StylesheetTransformResult {\n if (value == null) {\n return { code: fallbackCode };\n }\n\n if (typeof value === 'string') {\n return { code: value };\n }\n\n return {\n code: value.code ?? fallbackCode,\n dependencies: value.dependencies ?? [],\n diagnostics: value.diagnostics ?? [],\n tags: value.tags ?? [],\n };\n}\n\nexport function normalizeStylesheetDependencies(\n dependencies: Array<string | StylesheetDependency> | undefined,\n): StylesheetDependency[] {\n return (dependencies ?? []).map((dependency) =>\n typeof dependency === 'string' ? { id: dependency } : dependency,\n );\n}\n\nexport function composeStylePreprocessors(\n preprocessors: Array<StylePreprocessor | false | null | undefined>,\n): StylePreprocessor | undefined {\n const active = preprocessors.filter(\n (preprocessor): preprocessor is StylePreprocessor => !!preprocessor,\n );\n\n if (!active.length) {\n return undefined;\n }\n\n return (code, filename, context) => {\n let current = normalizeStylesheetTransformResult(undefined, code);\n\n for (const preprocessor of active) {\n const next = normalizeStylesheetTransformResult(\n preprocessor(current.code, filename, context),\n current.code,\n );\n current = {\n code: next.code,\n dependencies: [\n ...(current.dependencies ?? []),\n ...(next.dependencies ?? []),\n ],\n diagnostics: [\n ...(current.diagnostics ?? []),\n ...(next.diagnostics ?? []),\n ],\n tags: [...(current.tags ?? []), ...(next.tags ?? [])],\n };\n }\n\n return current;\n };\n}\n"],"mappings":";AA0CA,SAAgB,mCACd,OACA,cAC2B;AAC3B,KAAI,SAAS,KACX,QAAO,EAAE,MAAM,cAAc;AAG/B,KAAI,OAAO,UAAU,SACnB,QAAO,EAAE,MAAM,OAAO;AAGxB,QAAO;EACL,MAAM,MAAM,QAAQ;EACpB,cAAc,MAAM,gBAAgB,EAAE;EACtC,aAAa,MAAM,eAAe,EAAE;EACpC,MAAM,MAAM,QAAQ,EAAE;EACvB;;AAGH,SAAgB,gCACd,cACwB;AACxB,SAAQ,gBAAgB,EAAE,EAAE,KAAK,eAC/B,OAAO,eAAe,WAAW,EAAE,IAAI,YAAY,GAAG,WACvD;;AAGH,SAAgB,0BACd,eAC+B;CAC/B,MAAM,SAAS,cAAc,QAC1B,iBAAoD,CAAC,CAAC,aACxD;AAED,KAAI,CAAC,OAAO,OACV;AAGF,SAAQ,MAAM,UAAU,YAAY;EAClC,IAAI,UAAU,mCAAmC,KAAA,GAAW,KAAK;AAEjE,OAAK,MAAM,gBAAgB,QAAQ;GACjC,MAAM,OAAO,mCACX,aAAa,QAAQ,MAAM,UAAU,QAAQ,EAC7C,QAAQ,KACT;AACD,aAAU;IACR,MAAM,KAAK;IACX,cAAc,CACZ,GAAI,QAAQ,gBAAgB,EAAE,EAC9B,GAAI,KAAK,gBAAgB,EAAE,CAC5B;IACD,aAAa,CACX,GAAI,QAAQ,eAAe,EAAE,EAC7B,GAAI,KAAK,eAAe,EAAE,CAC3B;IACD,MAAM,CAAC,GAAI,QAAQ,QAAQ,EAAE,EAAG,GAAI,KAAK,QAAQ,EAAE,CAAE;IACtD;;AAGH,SAAO"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { StylePreprocessor, StylesheetDependency, StylesheetDiagnostic, StylesheetTransformResult, StylesheetTransformContext } from "./style-preprocessor.js";
|
|
2
|
+
export interface AnalogStylesheetRecord {
|
|
3
|
+
publicId: string;
|
|
4
|
+
sourcePath?: string;
|
|
5
|
+
originalCode?: string;
|
|
6
|
+
normalizedCode: string;
|
|
7
|
+
dependencies?: StylesheetDependency[];
|
|
8
|
+
diagnostics?: StylesheetDiagnostic[];
|
|
9
|
+
tags?: string[];
|
|
10
|
+
}
|
|
11
|
+
export declare class AnalogStylesheetRegistry {
|
|
12
|
+
private servedById;
|
|
13
|
+
private servedAliasToId;
|
|
14
|
+
private externalRequestToSource;
|
|
15
|
+
/**
|
|
16
|
+
* Maps a real source stylesheet path back to the generated public stylesheet
|
|
17
|
+
* ids Analog serves for Angular. This is stable across requests and lets HMR
|
|
18
|
+
* reason about "which virtual stylesheet came from this source file?"
|
|
19
|
+
*/
|
|
20
|
+
private sourceToPublicIds;
|
|
21
|
+
/**
|
|
22
|
+
* Tracks the live request ids Vite/Angular have actually served for a source
|
|
23
|
+
* stylesheet, including both `?direct&ngcomp=...` CSS modules and
|
|
24
|
+
* `?ngcomp=...` JS wrapper modules. HMR must use these live request ids
|
|
25
|
+
* because Angular component styles are no longer addressed by their original
|
|
26
|
+
* file paths once externalized.
|
|
27
|
+
*/
|
|
28
|
+
private sourceToRequestIds;
|
|
29
|
+
private sourceToDependencies;
|
|
30
|
+
private sourceToDiagnostics;
|
|
31
|
+
private sourceToTags;
|
|
32
|
+
/**
|
|
33
|
+
* Canonicalizes browser-facing stylesheet request ids so Vite timestamp
|
|
34
|
+
* variants (`?t=...`) and path-shape variants (`abc.css?...` vs
|
|
35
|
+
* `/abc.css?...`) all collapse onto one logical module identity.
|
|
36
|
+
*
|
|
37
|
+
* This is critical for Angular component stylesheet HMR because the browser
|
|
38
|
+
* can keep both timestamped and non-timestamped requests alive for the same
|
|
39
|
+
* externalized stylesheet. If Analog tracks them as distinct resources, HMR
|
|
40
|
+
* can update one module while the browser continues rendering another stale
|
|
41
|
+
* module for the same public stylesheet id.
|
|
42
|
+
*/
|
|
43
|
+
private normalizeRequestId;
|
|
44
|
+
get servedCount(): number;
|
|
45
|
+
get externalCount(): number;
|
|
46
|
+
hasServed(requestId: string): boolean;
|
|
47
|
+
getServedContent(requestId: string): string | undefined;
|
|
48
|
+
resolveExternalSource(requestId: string): string | undefined;
|
|
49
|
+
getPublicIdsForSource(sourcePath: string): string[];
|
|
50
|
+
getRequestIdsForSource(sourcePath: string): string[];
|
|
51
|
+
getDependenciesForSource(sourcePath: string): StylesheetDependency[];
|
|
52
|
+
getDiagnosticsForSource(sourcePath: string): StylesheetDiagnostic[];
|
|
53
|
+
getTagsForSource(sourcePath: string): string[];
|
|
54
|
+
registerExternalRequest(requestId: string, sourcePath: string): void;
|
|
55
|
+
registerActiveRequest(requestId: string): void;
|
|
56
|
+
registerServedStylesheet(record: AnalogStylesheetRecord, aliases?: string[]): void;
|
|
57
|
+
private recomputeSourceMetadata;
|
|
58
|
+
private resolveServedRecord;
|
|
59
|
+
}
|
|
60
|
+
export declare function preprocessStylesheet(code: string, filename: string, stylePreprocessor?: StylePreprocessor, context?: StylesheetTransformContext): string;
|
|
61
|
+
export declare function preprocessStylesheetResult(code: string, filename: string, stylePreprocessor?: StylePreprocessor, context?: StylesheetTransformContext): StylesheetTransformResult;
|
|
62
|
+
export declare function rewriteRelativeCssImports(code: string, filename: string): string;
|
|
63
|
+
export declare function registerStylesheetContent(registry: AnalogStylesheetRegistry, { code, dependencies, diagnostics, tags, containingFile, className, order, inlineStylesExtension, resourceFile }: {
|
|
64
|
+
code: string;
|
|
65
|
+
dependencies?: StylesheetDependency[];
|
|
66
|
+
diagnostics?: StylesheetDiagnostic[];
|
|
67
|
+
tags?: string[];
|
|
68
|
+
containingFile: string;
|
|
69
|
+
className?: string;
|
|
70
|
+
order?: number;
|
|
71
|
+
inlineStylesExtension: string;
|
|
72
|
+
resourceFile?: string;
|
|
73
|
+
}): string;
|