@analogjs/vite-plugin-angular 3.0.0-alpha.30 → 3.0.0-alpha.32
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 +4 -2
- package/src/lib/analog-compiler-plugin.d.ts +14 -0
- package/src/lib/analog-compiler-plugin.js +257 -0
- package/src/lib/analog-compiler-plugin.js.map +1 -0
- package/src/lib/angular-vite-plugin.d.ts +7 -0
- package/src/lib/angular-vite-plugin.js +49 -30
- package/src/lib/angular-vite-plugin.js.map +1 -1
- package/src/lib/utils/plugin-config.d.ts +30 -0
- package/src/lib/utils/plugin-config.js +64 -0
- package/src/lib/utils/plugin-config.js.map +1 -0
- package/src/lib/utils/virtual-ids.d.ts +8 -0
- package/src/lib/utils/virtual-ids.js +35 -0
- package/src/lib/utils/virtual-ids.js.map +1 -0
- package/src/lib/utils/virtual-resources.d.ts +31 -0
- package/src/lib/utils/virtual-resources.js +60 -0
- package/src/lib/utils/virtual-resources.js.map +1 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript file extension regex
|
|
3
|
+
* Match .(c or m)ts, .ts extensions with an optional ? for query params
|
|
4
|
+
* Ignore .tsx extensions
|
|
5
|
+
*/
|
|
6
|
+
export declare const TS_EXT_REGEX: unknown;
|
|
7
|
+
export interface TsConfigResolutionContext {
|
|
8
|
+
root: string;
|
|
9
|
+
isProd: boolean;
|
|
10
|
+
isLib: boolean;
|
|
11
|
+
}
|
|
12
|
+
export declare function getTsConfigPath(root: string, tsconfig: string, isProd: boolean, isTest: boolean, isLib: boolean);
|
|
13
|
+
export declare function createTsConfigGetter(tsconfigOrGetter?: string | (() => string));
|
|
14
|
+
export interface DepOptimizerOptions {
|
|
15
|
+
tsconfig: string;
|
|
16
|
+
isProd: boolean;
|
|
17
|
+
jit: boolean;
|
|
18
|
+
watchMode: boolean;
|
|
19
|
+
isTest: boolean;
|
|
20
|
+
isAstroIntegration: boolean;
|
|
21
|
+
}
|
|
22
|
+
export declare function createDepOptimizerConfig(opts: DepOptimizerOptions): {
|
|
23
|
+
optimizeDeps: {
|
|
24
|
+
include: unknown;
|
|
25
|
+
exclude: unknown;
|
|
26
|
+
};
|
|
27
|
+
resolve: {
|
|
28
|
+
conditions: unknown;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { createCompilerPlugin, createRolldownCompilerPlugin } from "../compiler-plugin.js";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { isAbsolute, resolve } from "node:path";
|
|
4
|
+
import * as vite from "vite";
|
|
5
|
+
//#region packages/vite-plugin-angular/src/lib/utils/plugin-config.ts
|
|
6
|
+
/**
|
|
7
|
+
* TypeScript file extension regex
|
|
8
|
+
* Match .(c or m)ts, .ts extensions with an optional ? for query params
|
|
9
|
+
* Ignore .tsx extensions
|
|
10
|
+
*/
|
|
11
|
+
var TS_EXT_REGEX = /\.[cm]?(ts)[^x]?\??/;
|
|
12
|
+
function getTsConfigPath(root, tsconfig, isProd, isTest, isLib) {
|
|
13
|
+
if (tsconfig && isAbsolute(tsconfig)) {
|
|
14
|
+
if (!existsSync(tsconfig)) console.error(`[@analogjs/vite-plugin-angular]: Unable to resolve tsconfig at ${tsconfig}. This causes compilation issues. Check the path or set the "tsconfig" property with an absolute path.`);
|
|
15
|
+
return tsconfig;
|
|
16
|
+
}
|
|
17
|
+
let tsconfigFilePath = "./tsconfig.app.json";
|
|
18
|
+
if (isLib) tsconfigFilePath = isProd ? "./tsconfig.lib.prod.json" : "./tsconfig.lib.json";
|
|
19
|
+
if (isTest) tsconfigFilePath = "./tsconfig.spec.json";
|
|
20
|
+
if (tsconfig) tsconfigFilePath = tsconfig;
|
|
21
|
+
const resolvedPath = resolve(root, tsconfigFilePath);
|
|
22
|
+
if (!existsSync(resolvedPath)) console.error(`[@analogjs/vite-plugin-angular]: Unable to resolve tsconfig at ${resolvedPath}. This causes compilation issues. Check the path or set the "tsconfig" property with an absolute path.`);
|
|
23
|
+
return resolvedPath;
|
|
24
|
+
}
|
|
25
|
+
function createTsConfigGetter(tsconfigOrGetter) {
|
|
26
|
+
if (typeof tsconfigOrGetter === "function") return tsconfigOrGetter;
|
|
27
|
+
return () => tsconfigOrGetter || "";
|
|
28
|
+
}
|
|
29
|
+
function createDepOptimizerConfig(opts) {
|
|
30
|
+
const defineOptions = {
|
|
31
|
+
ngJitMode: "false",
|
|
32
|
+
ngI18nClosureMode: "false",
|
|
33
|
+
...opts.watchMode ? {} : { ngDevMode: "false" }
|
|
34
|
+
};
|
|
35
|
+
const rolldownOptions = { plugins: [createRolldownCompilerPlugin({
|
|
36
|
+
tsconfig: opts.tsconfig,
|
|
37
|
+
sourcemap: !opts.isProd,
|
|
38
|
+
advancedOptimizations: opts.isProd,
|
|
39
|
+
jit: opts.jit,
|
|
40
|
+
incremental: opts.watchMode
|
|
41
|
+
})] };
|
|
42
|
+
const esbuildOptions = {
|
|
43
|
+
plugins: [createCompilerPlugin({
|
|
44
|
+
tsconfig: opts.tsconfig,
|
|
45
|
+
sourcemap: !opts.isProd,
|
|
46
|
+
advancedOptimizations: opts.isProd,
|
|
47
|
+
jit: opts.jit,
|
|
48
|
+
incremental: opts.watchMode
|
|
49
|
+
}, opts.isTest, !opts.isAstroIntegration)],
|
|
50
|
+
define: defineOptions
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
optimizeDeps: {
|
|
54
|
+
include: ["rxjs/operators", "rxjs"],
|
|
55
|
+
exclude: ["@angular/platform-server"],
|
|
56
|
+
...vite.rolldownVersion ? { rolldownOptions } : { esbuildOptions }
|
|
57
|
+
},
|
|
58
|
+
resolve: { conditions: ["style"] }
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
//#endregion
|
|
62
|
+
export { TS_EXT_REGEX, createDepOptimizerConfig, createTsConfigGetter, getTsConfigPath };
|
|
63
|
+
|
|
64
|
+
//# sourceMappingURL=plugin-config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin-config.js","names":[],"sources":["../../../../src/lib/utils/plugin-config.ts"],"sourcesContent":["import { existsSync } from 'node:fs';\nimport { isAbsolute, resolve } from 'node:path';\nimport * as vite from 'vite';\nimport { defaultClientConditions } from 'vite';\n\nimport {\n createCompilerPlugin,\n createRolldownCompilerPlugin,\n} from '../compiler-plugin.js';\n\n/**\n * TypeScript file extension regex\n * Match .(c or m)ts, .ts extensions with an optional ? for query params\n * Ignore .tsx extensions\n */\nexport const TS_EXT_REGEX = /\\.[cm]?(ts)[^x]?\\??/;\n\nexport interface TsConfigResolutionContext {\n root: string;\n isProd: boolean;\n isLib: boolean;\n}\n\nexport function getTsConfigPath(\n root: string,\n tsconfig: string,\n isProd: boolean,\n isTest: boolean,\n isLib: boolean,\n) {\n if (tsconfig && isAbsolute(tsconfig)) {\n if (!existsSync(tsconfig)) {\n console.error(\n `[@analogjs/vite-plugin-angular]: Unable to resolve tsconfig at ${tsconfig}. This causes compilation issues. Check the path or set the \"tsconfig\" property with an absolute path.`,\n );\n }\n\n return tsconfig;\n }\n\n let tsconfigFilePath = './tsconfig.app.json';\n\n if (isLib) {\n tsconfigFilePath = isProd\n ? './tsconfig.lib.prod.json'\n : './tsconfig.lib.json';\n }\n\n if (isTest) {\n tsconfigFilePath = './tsconfig.spec.json';\n }\n\n if (tsconfig) {\n tsconfigFilePath = tsconfig;\n }\n\n const resolvedPath = resolve(root, tsconfigFilePath);\n\n if (!existsSync(resolvedPath)) {\n console.error(\n `[@analogjs/vite-plugin-angular]: Unable to resolve tsconfig at ${resolvedPath}. This causes compilation issues. Check the path or set the \"tsconfig\" property with an absolute path.`,\n );\n }\n\n return resolvedPath;\n}\n\nexport function createTsConfigGetter(\n tsconfigOrGetter?: string | (() => string),\n) {\n if (typeof tsconfigOrGetter === 'function') {\n return tsconfigOrGetter;\n }\n\n return () => tsconfigOrGetter || '';\n}\n\nexport interface DepOptimizerOptions {\n tsconfig: string;\n isProd: boolean;\n jit: boolean;\n watchMode: boolean;\n isTest: boolean;\n isAstroIntegration: boolean;\n}\n\nexport function createDepOptimizerConfig(opts: DepOptimizerOptions) {\n const defineOptions = {\n ngJitMode: 'false',\n ngI18nClosureMode: 'false',\n ...(opts.watchMode ? {} : { ngDevMode: 'false' }),\n };\n\n const rolldownOptions: vite.DepOptimizationOptions['rolldownOptions'] = {\n plugins: [\n createRolldownCompilerPlugin({\n tsconfig: opts.tsconfig,\n sourcemap: !opts.isProd,\n advancedOptimizations: opts.isProd,\n jit: opts.jit,\n incremental: opts.watchMode,\n }),\n ],\n };\n\n const esbuildOptions: vite.DepOptimizationOptions['esbuildOptions'] = {\n plugins: [\n createCompilerPlugin(\n {\n tsconfig: opts.tsconfig,\n sourcemap: !opts.isProd,\n advancedOptimizations: opts.isProd,\n jit: opts.jit,\n incremental: opts.watchMode,\n },\n opts.isTest,\n !opts.isAstroIntegration,\n ),\n ],\n define: defineOptions,\n };\n\n return {\n optimizeDeps: {\n include: ['rxjs/operators', 'rxjs'],\n exclude: ['@angular/platform-server'],\n ...(vite.rolldownVersion ? { rolldownOptions } : { esbuildOptions }),\n },\n resolve: {\n conditions: ['style'],\n },\n };\n}\n"],"mappings":";;;;;;;;;;AAeA,IAAa,eAAe;AAQ5B,SAAgB,gBACd,MACA,UACA,QACA,QACA,OACA;AACA,KAAI,YAAY,WAAW,SAAS,EAAE;AACpC,MAAI,CAAC,WAAW,SAAS,CACvB,SAAQ,MACN,kEAAkE,SAAS,wGAC5E;AAGH,SAAO;;CAGT,IAAI,mBAAmB;AAEvB,KAAI,MACF,oBAAmB,SACf,6BACA;AAGN,KAAI,OACF,oBAAmB;AAGrB,KAAI,SACF,oBAAmB;CAGrB,MAAM,eAAe,QAAQ,MAAM,iBAAiB;AAEpD,KAAI,CAAC,WAAW,aAAa,CAC3B,SAAQ,MACN,kEAAkE,aAAa,wGAChF;AAGH,QAAO;;AAGT,SAAgB,qBACd,kBACA;AACA,KAAI,OAAO,qBAAqB,WAC9B,QAAO;AAGT,cAAa,oBAAoB;;AAYnC,SAAgB,yBAAyB,MAA2B;CAClE,MAAM,gBAAgB;EACpB,WAAW;EACX,mBAAmB;EACnB,GAAI,KAAK,YAAY,EAAE,GAAG,EAAE,WAAW,SAAS;EACjD;CAED,MAAM,kBAAkE,EACtE,SAAS,CACP,6BAA6B;EAC3B,UAAU,KAAK;EACf,WAAW,CAAC,KAAK;EACjB,uBAAuB,KAAK;EAC5B,KAAK,KAAK;EACV,aAAa,KAAK;EACnB,CAAC,CACH,EACF;CAED,MAAM,iBAAgE;EACpE,SAAS,CACP,qBACE;GACE,UAAU,KAAK;GACf,WAAW,CAAC,KAAK;GACjB,uBAAuB,KAAK;GAC5B,KAAK,KAAK;GACV,aAAa,KAAK;GACnB,EACD,KAAK,QACL,CAAC,KAAK,mBACP,CACF;EACD,QAAQ;EACT;AAED,QAAO;EACL,cAAc;GACZ,SAAS,CAAC,kBAAkB,OAAO;GACnC,SAAS,CAAC,2BAA2B;GACrC,GAAI,KAAK,kBAAkB,EAAE,iBAAiB,GAAG,EAAE,gBAAgB;GACpE;EACD,SAAS,EACP,YAAY,CAAC,QAAQ,EACtB;EACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const VIRTUAL_STYLE_PREFIX = "virtual:@analogjs/vite-plugin-angular:inline-style:";
|
|
2
|
+
export declare const VIRTUAL_RAW_PREFIX = "virtual:@analogjs/vite-plugin-angular:raw:";
|
|
3
|
+
export declare function toVirtualStyleId(absPath: string): string;
|
|
4
|
+
export declare function isVirtualStyleId(id: string): boolean;
|
|
5
|
+
export declare function fromVirtualStyleId(id: string): string;
|
|
6
|
+
export declare function toVirtualRawId(absPath: string): string;
|
|
7
|
+
export declare function isVirtualRawId(id: string): boolean;
|
|
8
|
+
export declare function fromVirtualRawId(id: string): string;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//#region packages/vite-plugin-angular/src/lib/utils/virtual-ids.ts
|
|
2
|
+
var VIRTUAL_STYLE_PREFIX = "virtual:@analogjs/vite-plugin-angular:inline-style:";
|
|
3
|
+
var VIRTUAL_RAW_PREFIX = "virtual:@analogjs/vite-plugin-angular:raw:";
|
|
4
|
+
function encode(absPath) {
|
|
5
|
+
return Buffer.from(absPath, "utf-8").toString("base64url");
|
|
6
|
+
}
|
|
7
|
+
function decode(encoded) {
|
|
8
|
+
return Buffer.from(encoded, "base64url").toString("utf-8");
|
|
9
|
+
}
|
|
10
|
+
function toVirtualStyleId(absPath) {
|
|
11
|
+
return `${VIRTUAL_STYLE_PREFIX}${encode(absPath)}`;
|
|
12
|
+
}
|
|
13
|
+
function isVirtualStyleId(id) {
|
|
14
|
+
return (id.startsWith("\0") ? id.slice(1) : id).startsWith(VIRTUAL_STYLE_PREFIX);
|
|
15
|
+
}
|
|
16
|
+
function fromVirtualStyleId(id) {
|
|
17
|
+
const normalizedId = id.startsWith("\0") ? id.slice(1) : id;
|
|
18
|
+
if (!normalizedId.startsWith("virtual:@analogjs/vite-plugin-angular:inline-style:")) throw new Error(`Invalid virtual style id: ${id}`);
|
|
19
|
+
return decode(normalizedId.slice(51));
|
|
20
|
+
}
|
|
21
|
+
function toVirtualRawId(absPath) {
|
|
22
|
+
return `${VIRTUAL_RAW_PREFIX}${encode(absPath)}`;
|
|
23
|
+
}
|
|
24
|
+
function isVirtualRawId(id) {
|
|
25
|
+
return (id.startsWith("\0") ? id.slice(1) : id).startsWith(VIRTUAL_RAW_PREFIX);
|
|
26
|
+
}
|
|
27
|
+
function fromVirtualRawId(id) {
|
|
28
|
+
const normalizedId = id.startsWith("\0") ? id.slice(1) : id;
|
|
29
|
+
if (!normalizedId.startsWith("virtual:@analogjs/vite-plugin-angular:raw:")) throw new Error(`Invalid virtual raw id: ${id}`);
|
|
30
|
+
return decode(normalizedId.slice(42));
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
export { VIRTUAL_RAW_PREFIX, VIRTUAL_STYLE_PREFIX, fromVirtualRawId, fromVirtualStyleId, isVirtualRawId, isVirtualStyleId, toVirtualRawId, toVirtualStyleId };
|
|
34
|
+
|
|
35
|
+
//# sourceMappingURL=virtual-ids.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"virtual-ids.js","names":[],"sources":["../../../../src/lib/utils/virtual-ids.ts"],"sourcesContent":["// Virtual module id helpers for external component resources (template and\n// style files). Routing through virtual ids keeps Vite's built-in plugins\n// (vite:css, vite:asset, the server.fs Denied ID check) out of the picture:\n// the id Vite sees has no file extension, so extension-based matchers never\n// fire, and it does not match the /[?&](raw|inline)\\b/ security regex that\n// blocks user-facing ?raw and ?inline queries.\n//\n// Both prefixes share the same shape — a literal prefix followed by a\n// base64url-encoded absolute file path — so load hooks can round-trip back\n// to the source file for reading and watching. See #2263 / #2283.\n\nexport const VIRTUAL_STYLE_PREFIX =\n 'virtual:@analogjs/vite-plugin-angular:inline-style:';\n\nexport const VIRTUAL_RAW_PREFIX = 'virtual:@analogjs/vite-plugin-angular:raw:';\n\nfunction encode(absPath: string): string {\n return Buffer.from(absPath, 'utf-8').toString('base64url');\n}\n\nfunction decode(encoded: string): string {\n return Buffer.from(encoded, 'base64url').toString('utf-8');\n}\n\nexport function toVirtualStyleId(absPath: string): string {\n return `${VIRTUAL_STYLE_PREFIX}${encode(absPath)}`;\n}\n\nexport function isVirtualStyleId(id: string): boolean {\n const stripped = id.startsWith('\\0') ? id.slice(1) : id;\n return stripped.startsWith(VIRTUAL_STYLE_PREFIX);\n}\n\nexport function fromVirtualStyleId(id: string): string {\n const normalizedId = id.startsWith('\\0') ? id.slice(1) : id;\n if (!normalizedId.startsWith(VIRTUAL_STYLE_PREFIX)) {\n throw new Error(`Invalid virtual style id: ${id}`);\n }\n return decode(normalizedId.slice(VIRTUAL_STYLE_PREFIX.length));\n}\n\nexport function toVirtualRawId(absPath: string): string {\n return `${VIRTUAL_RAW_PREFIX}${encode(absPath)}`;\n}\n\nexport function isVirtualRawId(id: string): boolean {\n const stripped = id.startsWith('\\0') ? id.slice(1) : id;\n return stripped.startsWith(VIRTUAL_RAW_PREFIX);\n}\n\nexport function fromVirtualRawId(id: string): string {\n const normalizedId = id.startsWith('\\0') ? id.slice(1) : id;\n if (!normalizedId.startsWith(VIRTUAL_RAW_PREFIX)) {\n throw new Error(`Invalid virtual raw id: ${id}`);\n }\n return decode(normalizedId.slice(VIRTUAL_RAW_PREFIX.length));\n}\n"],"mappings":";AAWA,IAAa,uBACX;AAEF,IAAa,qBAAqB;AAElC,SAAS,OAAO,SAAyB;AACvC,QAAO,OAAO,KAAK,SAAS,QAAQ,CAAC,SAAS,YAAY;;AAG5D,SAAS,OAAO,SAAyB;AACvC,QAAO,OAAO,KAAK,SAAS,YAAY,CAAC,SAAS,QAAQ;;AAG5D,SAAgB,iBAAiB,SAAyB;AACxD,QAAO,GAAG,uBAAuB,OAAO,QAAQ;;AAGlD,SAAgB,iBAAiB,IAAqB;AAEpD,SADiB,GAAG,WAAW,KAAK,GAAG,GAAG,MAAM,EAAE,GAAG,IACrC,WAAW,qBAAqB;;AAGlD,SAAgB,mBAAmB,IAAoB;CACrD,MAAM,eAAe,GAAG,WAAW,KAAK,GAAG,GAAG,MAAM,EAAE,GAAG;AACzD,KAAI,CAAC,aAAa,WAAA,sDAAgC,CAChD,OAAM,IAAI,MAAM,6BAA6B,KAAK;AAEpD,QAAO,OAAO,aAAa,MAAM,GAA4B,CAAC;;AAGhE,SAAgB,eAAe,SAAyB;AACtD,QAAO,GAAG,qBAAqB,OAAO,QAAQ;;AAGhD,SAAgB,eAAe,IAAqB;AAElD,SADiB,GAAG,WAAW,KAAK,GAAG,GAAG,MAAM,EAAE,GAAG,IACrC,WAAW,mBAAmB;;AAGhD,SAAgB,iBAAiB,IAAoB;CACnD,MAAM,eAAe,GAAG,WAAW,KAAK,GAAG,GAAG,MAAM,EAAE,GAAG;AACzD,KAAI,CAAC,aAAa,WAAA,6CAA8B,CAC9C,OAAM,IAAI,MAAM,2BAA2B,KAAK;AAElD,QAAO,OAAO,aAAa,MAAM,GAA0B,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type ResolvedConfig } from "vite";
|
|
2
|
+
interface PluginContextLike {
|
|
3
|
+
addWatchFile(path: string): void;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Rewrite a user `.html?raw` import to a virtual raw id. Returns undefined
|
|
7
|
+
* when the id doesn't match, so callers can fall through to the next check.
|
|
8
|
+
*
|
|
9
|
+
* Routed through a virtual id (rather than `?analog-raw`) so the path Vite
|
|
10
|
+
* sees has no file extension — keeps vite:asset / vite:css from re-tagging
|
|
11
|
+
* the id before our load hook runs.
|
|
12
|
+
*/
|
|
13
|
+
export declare function rewriteHtmlRawImport(id: string, importer: string | undefined): string | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Rewrite a user `.scss?inline` / `.css?inline` / … import to a virtual
|
|
16
|
+
* style id. Returns undefined when the id doesn't match.
|
|
17
|
+
*/
|
|
18
|
+
export declare function rewriteInlineStyleImport(id: string, importer: string | undefined): string | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Load a virtual raw module: reads the backing file, registers it for HMR
|
|
21
|
+
* watching, and returns its content as a default-exported string. Returns
|
|
22
|
+
* undefined when the id is not a virtual raw id.
|
|
23
|
+
*/
|
|
24
|
+
export declare function loadVirtualRawModule(ctx: PluginContextLike, id: string): Promise<string | undefined>;
|
|
25
|
+
/**
|
|
26
|
+
* Load a virtual style module: reads the backing stylesheet, runs it through
|
|
27
|
+
* Vite's CSS preprocessor, and returns the result as a default-exported
|
|
28
|
+
* string. Returns undefined when the id is not a virtual style id.
|
|
29
|
+
*/
|
|
30
|
+
export declare function loadVirtualStyleModule(ctx: PluginContextLike, id: string, resolvedConfig: ResolvedConfig): Promise<string | undefined>;
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { fromVirtualRawId, fromVirtualStyleId, isVirtualRawId, isVirtualStyleId, toVirtualRawId, toVirtualStyleId } from "./virtual-ids.js";
|
|
2
|
+
import { promises } from "node:fs";
|
|
3
|
+
import { dirname, isAbsolute, resolve } from "node:path";
|
|
4
|
+
import { normalizePath, preprocessCSS } from "vite";
|
|
5
|
+
//#region packages/vite-plugin-angular/src/lib/utils/virtual-resources.ts
|
|
6
|
+
var INLINE_STYLE_QUERY_RE = /\.(css|scss|sass|less)\?inline$/;
|
|
7
|
+
function resolveImportPath(id, importer) {
|
|
8
|
+
const filePath = id.split("?")[0];
|
|
9
|
+
return isAbsolute(filePath) ? normalizePath(filePath) : importer ? normalizePath(resolve(dirname(importer), filePath)) : void 0;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Rewrite a user `.html?raw` import to a virtual raw id. Returns undefined
|
|
13
|
+
* when the id doesn't match, so callers can fall through to the next check.
|
|
14
|
+
*
|
|
15
|
+
* Routed through a virtual id (rather than `?analog-raw`) so the path Vite
|
|
16
|
+
* sees has no file extension — keeps vite:asset / vite:css from re-tagging
|
|
17
|
+
* the id before our load hook runs.
|
|
18
|
+
*/
|
|
19
|
+
function rewriteHtmlRawImport(id, importer) {
|
|
20
|
+
if (!id.includes(".html?raw")) return void 0;
|
|
21
|
+
const resolved = resolveImportPath(id, importer);
|
|
22
|
+
return resolved ? toVirtualRawId(resolved) : void 0;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Rewrite a user `.scss?inline` / `.css?inline` / … import to a virtual
|
|
26
|
+
* style id. Returns undefined when the id doesn't match.
|
|
27
|
+
*/
|
|
28
|
+
function rewriteInlineStyleImport(id, importer) {
|
|
29
|
+
if (!INLINE_STYLE_QUERY_RE.test(id)) return void 0;
|
|
30
|
+
const resolved = resolveImportPath(id, importer);
|
|
31
|
+
return resolved ? toVirtualStyleId(resolved) : void 0;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Load a virtual raw module: reads the backing file, registers it for HMR
|
|
35
|
+
* watching, and returns its content as a default-exported string. Returns
|
|
36
|
+
* undefined when the id is not a virtual raw id.
|
|
37
|
+
*/
|
|
38
|
+
async function loadVirtualRawModule(ctx, id) {
|
|
39
|
+
if (!isVirtualRawId(id)) return void 0;
|
|
40
|
+
const filePath = fromVirtualRawId(id);
|
|
41
|
+
ctx.addWatchFile(filePath);
|
|
42
|
+
const content = await promises.readFile(filePath, "utf-8");
|
|
43
|
+
return `export default ${JSON.stringify(content)}`;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Load a virtual style module: reads the backing stylesheet, runs it through
|
|
47
|
+
* Vite's CSS preprocessor, and returns the result as a default-exported
|
|
48
|
+
* string. Returns undefined when the id is not a virtual style id.
|
|
49
|
+
*/
|
|
50
|
+
async function loadVirtualStyleModule(ctx, id, resolvedConfig) {
|
|
51
|
+
if (!isVirtualStyleId(id)) return void 0;
|
|
52
|
+
const filePath = fromVirtualStyleId(id);
|
|
53
|
+
ctx.addWatchFile(filePath);
|
|
54
|
+
const result = await preprocessCSS(await promises.readFile(filePath, "utf-8"), filePath, resolvedConfig);
|
|
55
|
+
return `export default ${JSON.stringify(result.code)}`;
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
export { loadVirtualRawModule, loadVirtualStyleModule, rewriteHtmlRawImport, rewriteInlineStyleImport };
|
|
59
|
+
|
|
60
|
+
//# sourceMappingURL=virtual-resources.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"virtual-resources.js","names":[],"sources":["../../../../src/lib/utils/virtual-resources.ts"],"sourcesContent":["// Shared Vite plugin helpers for routing component resources (templates,\n// external styles) through virtual module ids. Both angular-vite-plugin and\n// analog-compiler-plugin use these so the rewriting + loading behavior stays\n// in sync between them.\n\nimport { promises as fsPromises } from 'node:fs';\nimport { dirname, isAbsolute, resolve } from 'node:path';\nimport { normalizePath, preprocessCSS, type ResolvedConfig } from 'vite';\n\nimport {\n fromVirtualRawId,\n fromVirtualStyleId,\n isVirtualRawId,\n isVirtualStyleId,\n toVirtualRawId,\n toVirtualStyleId,\n} from './virtual-ids.js';\n\nconst INLINE_STYLE_QUERY_RE = /\\.(css|scss|sass|less)\\?inline$/;\n\ninterface PluginContextLike {\n addWatchFile(path: string): void;\n}\n\nfunction resolveImportPath(\n id: string,\n importer: string | undefined,\n): string | undefined {\n const filePath = id.split('?')[0];\n return isAbsolute(filePath)\n ? normalizePath(filePath)\n : importer\n ? normalizePath(resolve(dirname(importer), filePath))\n : undefined;\n}\n\n/**\n * Rewrite a user `.html?raw` import to a virtual raw id. Returns undefined\n * when the id doesn't match, so callers can fall through to the next check.\n *\n * Routed through a virtual id (rather than `?analog-raw`) so the path Vite\n * sees has no file extension — keeps vite:asset / vite:css from re-tagging\n * the id before our load hook runs.\n */\nexport function rewriteHtmlRawImport(\n id: string,\n importer: string | undefined,\n): string | undefined {\n if (!id.includes('.html?raw')) return undefined;\n const resolved = resolveImportPath(id, importer);\n return resolved ? toVirtualRawId(resolved) : undefined;\n}\n\n/**\n * Rewrite a user `.scss?inline` / `.css?inline` / … import to a virtual\n * style id. Returns undefined when the id doesn't match.\n */\nexport function rewriteInlineStyleImport(\n id: string,\n importer: string | undefined,\n): string | undefined {\n if (!INLINE_STYLE_QUERY_RE.test(id)) return undefined;\n const resolved = resolveImportPath(id, importer);\n return resolved ? toVirtualStyleId(resolved) : undefined;\n}\n\n/**\n * Load a virtual raw module: reads the backing file, registers it for HMR\n * watching, and returns its content as a default-exported string. Returns\n * undefined when the id is not a virtual raw id.\n */\nexport async function loadVirtualRawModule(\n ctx: PluginContextLike,\n id: string,\n): Promise<string | undefined> {\n if (!isVirtualRawId(id)) return undefined;\n const filePath = fromVirtualRawId(id);\n ctx.addWatchFile(filePath);\n const content = await fsPromises.readFile(filePath, 'utf-8');\n return `export default ${JSON.stringify(content)}`;\n}\n\n/**\n * Load a virtual style module: reads the backing stylesheet, runs it through\n * Vite's CSS preprocessor, and returns the result as a default-exported\n * string. Returns undefined when the id is not a virtual style id.\n */\nexport async function loadVirtualStyleModule(\n ctx: PluginContextLike,\n id: string,\n resolvedConfig: ResolvedConfig,\n): Promise<string | undefined> {\n if (!isVirtualStyleId(id)) return undefined;\n const filePath = fromVirtualStyleId(id);\n ctx.addWatchFile(filePath);\n const code = await fsPromises.readFile(filePath, 'utf-8');\n const result = await preprocessCSS(code, filePath, resolvedConfig);\n return `export default ${JSON.stringify(result.code)}`;\n}\n"],"mappings":";;;;;AAkBA,IAAM,wBAAwB;AAM9B,SAAS,kBACP,IACA,UACoB;CACpB,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC;AAC/B,QAAO,WAAW,SAAS,GACvB,cAAc,SAAS,GACvB,WACE,cAAc,QAAQ,QAAQ,SAAS,EAAE,SAAS,CAAC,GACnD,KAAA;;;;;;;;;;AAWR,SAAgB,qBACd,IACA,UACoB;AACpB,KAAI,CAAC,GAAG,SAAS,YAAY,CAAE,QAAO,KAAA;CACtC,MAAM,WAAW,kBAAkB,IAAI,SAAS;AAChD,QAAO,WAAW,eAAe,SAAS,GAAG,KAAA;;;;;;AAO/C,SAAgB,yBACd,IACA,UACoB;AACpB,KAAI,CAAC,sBAAsB,KAAK,GAAG,CAAE,QAAO,KAAA;CAC5C,MAAM,WAAW,kBAAkB,IAAI,SAAS;AAChD,QAAO,WAAW,iBAAiB,SAAS,GAAG,KAAA;;;;;;;AAQjD,eAAsB,qBACpB,KACA,IAC6B;AAC7B,KAAI,CAAC,eAAe,GAAG,CAAE,QAAO,KAAA;CAChC,MAAM,WAAW,iBAAiB,GAAG;AACrC,KAAI,aAAa,SAAS;CAC1B,MAAM,UAAU,MAAM,SAAW,SAAS,UAAU,QAAQ;AAC5D,QAAO,kBAAkB,KAAK,UAAU,QAAQ;;;;;;;AAQlD,eAAsB,uBACpB,KACA,IACA,gBAC6B;AAC7B,KAAI,CAAC,iBAAiB,GAAG,CAAE,QAAO,KAAA;CAClC,MAAM,WAAW,mBAAmB,GAAG;AACvC,KAAI,aAAa,SAAS;CAE1B,MAAM,SAAS,MAAM,cADR,MAAM,SAAW,SAAS,UAAU,QAAQ,EAChB,UAAU,eAAe;AAClE,QAAO,kBAAkB,KAAK,UAAU,OAAO,KAAK"}
|