angular-rust-plugins 0.1.0
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/README.md +118 -0
- package/binding/angular-binding.darwin-arm64.node +0 -0
- package/binding/index.d.ts +13 -0
- package/binding/index.js +316 -0
- package/binding/package.json +1 -0
- package/compiler/index.cjs +135 -0
- package/compiler/index.cjs.map +1 -0
- package/compiler/index.d.cts +12 -0
- package/compiler/index.d.ts +12 -0
- package/compiler/index.js +107 -0
- package/compiler/index.js.map +1 -0
- package/compiler/vite.cjs +135 -0
- package/compiler/vite.cjs.map +1 -0
- package/compiler/vite.d.cts +41 -0
- package/compiler/vite.d.ts +41 -0
- package/compiler/vite.js +109 -0
- package/compiler/vite.js.map +1 -0
- package/index.cjs +479 -0
- package/index.cjs.map +1 -0
- package/index.d.cts +8 -0
- package/index.d.ts +8 -0
- package/index.js +438 -0
- package/index.js.map +1 -0
- package/linker/esbuild.cjs +125 -0
- package/linker/esbuild.cjs.map +1 -0
- package/linker/esbuild.d.cts +39 -0
- package/linker/esbuild.d.ts +39 -0
- package/linker/esbuild.js +99 -0
- package/linker/esbuild.js.map +1 -0
- package/linker/index.cjs +372 -0
- package/linker/index.cjs.map +1 -0
- package/linker/index.d.cts +6 -0
- package/linker/index.d.ts +6 -0
- package/linker/index.js +333 -0
- package/linker/index.js.map +1 -0
- package/linker/rolldown.cjs +135 -0
- package/linker/rolldown.cjs.map +1 -0
- package/linker/rolldown.d.cts +36 -0
- package/linker/rolldown.d.ts +36 -0
- package/linker/rolldown.js +109 -0
- package/linker/rolldown.js.map +1 -0
- package/linker/vite.cjs +184 -0
- package/linker/vite.cjs.map +1 -0
- package/linker/vite.d.cts +58 -0
- package/linker/vite.d.ts +58 -0
- package/linker/vite.js +155 -0
- package/linker/vite.js.map +1 -0
- package/package.json +77 -0
- package/types-BTaYbdhr.d.cts +45 -0
- package/types-BTaYbdhr.d.ts +45 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// src/linker/rolldown.ts
|
|
2
|
+
import { createRequire } from "module";
|
|
3
|
+
import { dirname, join } from "path";
|
|
4
|
+
import { fileURLToPath } from "url";
|
|
5
|
+
|
|
6
|
+
// src/linker/types.ts
|
|
7
|
+
function needsLinking(code) {
|
|
8
|
+
return code.includes("\u0275\u0275ngDeclare");
|
|
9
|
+
}
|
|
10
|
+
function isAngularPackage(id) {
|
|
11
|
+
return id.includes("@angular") || id.includes("/@angular/");
|
|
12
|
+
}
|
|
13
|
+
function isJsFile(id) {
|
|
14
|
+
const cleanId = id.split("?")[0];
|
|
15
|
+
return cleanId.endsWith(".mjs") || cleanId.endsWith(".js");
|
|
16
|
+
}
|
|
17
|
+
function cleanModuleId(id) {
|
|
18
|
+
return id.split("?")[0];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/linker/rolldown.ts
|
|
22
|
+
var compilerInstance = null;
|
|
23
|
+
function getCompiler(options) {
|
|
24
|
+
if (compilerInstance) {
|
|
25
|
+
return compilerInstance;
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
let binding;
|
|
29
|
+
if (options?.bindingPath) {
|
|
30
|
+
const require2 = createRequire(import.meta.url);
|
|
31
|
+
binding = require2(options.bindingPath);
|
|
32
|
+
} else {
|
|
33
|
+
const currentFileUrl = import.meta.url;
|
|
34
|
+
const currentFilePath = fileURLToPath(currentFileUrl);
|
|
35
|
+
const currentDir = dirname(currentFilePath);
|
|
36
|
+
const require2 = createRequire(currentFileUrl);
|
|
37
|
+
const possiblePaths = [
|
|
38
|
+
join(currentDir, "..", "binding"),
|
|
39
|
+
// dist/linker/../binding
|
|
40
|
+
join(currentDir, "binding"),
|
|
41
|
+
// same directory
|
|
42
|
+
join(currentDir, "..", "..", "binding")
|
|
43
|
+
// deeper nesting
|
|
44
|
+
];
|
|
45
|
+
let loadedBinding = null;
|
|
46
|
+
let lastError = null;
|
|
47
|
+
for (const bindingPath of possiblePaths) {
|
|
48
|
+
try {
|
|
49
|
+
loadedBinding = require2(bindingPath);
|
|
50
|
+
break;
|
|
51
|
+
} catch (e) {
|
|
52
|
+
lastError = e;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (!loadedBinding) {
|
|
56
|
+
throw lastError || new Error("Could not find binding in any expected location");
|
|
57
|
+
}
|
|
58
|
+
binding = loadedBinding;
|
|
59
|
+
}
|
|
60
|
+
compilerInstance = new binding.Compiler();
|
|
61
|
+
return compilerInstance;
|
|
62
|
+
} catch (e) {
|
|
63
|
+
throw new Error(`Failed to load Angular Rust binding. Error: ${e}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function angularLinkerRolldownPlugin(options) {
|
|
67
|
+
const debug = options?.debug ?? false;
|
|
68
|
+
let compiler;
|
|
69
|
+
return {
|
|
70
|
+
name: "angular-linker-rolldown",
|
|
71
|
+
async transform(code, id) {
|
|
72
|
+
if (!compiler) {
|
|
73
|
+
compiler = getCompiler(options);
|
|
74
|
+
}
|
|
75
|
+
const isInNodeModules = id.includes("node_modules");
|
|
76
|
+
const cleanId = cleanModuleId(id);
|
|
77
|
+
if (!isAngularPackage(id) || !isInNodeModules || !isJsFile(id)) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
if (!needsLinking(code)) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
if (debug) {
|
|
84
|
+
console.log(`[Angular Linker] Linking: ${cleanId}`);
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
const result = compiler.linkFile(cleanId, code);
|
|
88
|
+
if (result.startsWith("/* Linker Error")) {
|
|
89
|
+
console.error(`[Angular Linker Error] ${id}:
|
|
90
|
+
${result}`);
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
if (debug) {
|
|
94
|
+
console.log(`[Angular Linker] Successfully linked: ${cleanId}`);
|
|
95
|
+
}
|
|
96
|
+
return { code: result, map: null };
|
|
97
|
+
} catch (e) {
|
|
98
|
+
console.error(`[Angular Linker Failed] ${id}:`, e);
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
var rolldown_default = angularLinkerRolldownPlugin;
|
|
105
|
+
export {
|
|
106
|
+
angularLinkerRolldownPlugin,
|
|
107
|
+
rolldown_default as default
|
|
108
|
+
};
|
|
109
|
+
//# sourceMappingURL=rolldown.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/linker/rolldown.ts","../../src/linker/types.ts"],"sourcesContent":["/**\n * Angular Linker Plugin for Rolldown\n *\n * Use this plugin with rolldown-vite or standalone Rolldown.\n *\n * @example\n * ```js\n * import { angularLinkerRolldownPlugin } from 'vite-plugin-angular-rust/rolldown';\n * import { defineConfig } from 'vite';\n *\n * export default defineConfig({\n * plugins: [angularLinkerRolldownPlugin()],\n * optimizeDeps: {\n * exclude: [\n * '@angular/core',\n * '@angular/common',\n * '@angular/platform-browser',\n * '@angular/router',\n * ],\n * },\n * });\n * ```\n */\n\nimport { createRequire } from \"module\";\nimport { dirname, join } from \"path\";\nimport { fileURLToPath } from \"url\";\nimport type { CompilerBinding, LinkerOptions, LinkerResult } from \"./types\";\nimport {\n needsLinking,\n isAngularPackage,\n isJsFile,\n cleanModuleId,\n} from \"./types\";\n\nlet compilerInstance: CompilerBinding | null = null;\n\nfunction getCompiler(options?: LinkerOptions): CompilerBinding {\n if (compilerInstance) {\n return compilerInstance;\n }\n\n try {\n let binding: { Compiler: new () => CompilerBinding };\n\n if (options?.bindingPath) {\n const require = createRequire(import.meta.url);\n binding = require(options.bindingPath);\n } else {\n // Load from bundled binding directory\n const currentFileUrl = import.meta.url;\n const currentFilePath = fileURLToPath(currentFileUrl);\n const currentDir = dirname(currentFilePath);\n const require = createRequire(currentFileUrl);\n\n // Try multiple possible binding locations\n const possiblePaths = [\n join(currentDir, \"..\", \"binding\"), // dist/linker/../binding\n join(currentDir, \"binding\"), // same directory\n join(currentDir, \"..\", \"..\", \"binding\"), // deeper nesting\n ];\n\n let loadedBinding: { Compiler: new () => CompilerBinding } | null = null;\n let lastError: unknown = null;\n\n for (const bindingPath of possiblePaths) {\n try {\n loadedBinding = require(bindingPath);\n break;\n } catch (e) {\n lastError = e;\n }\n }\n\n if (!loadedBinding) {\n throw (\n lastError ||\n new Error(\"Could not find binding in any expected location\")\n );\n }\n\n binding = loadedBinding;\n }\n\n compilerInstance = new binding.Compiler();\n return compilerInstance;\n } catch (e) {\n throw new Error(`Failed to load Angular Rust binding. Error: ${e}`);\n }\n}\n\nexport interface RolldownPlugin {\n name: string;\n transform(\n code: string,\n id: string\n ): Promise<LinkerResult | null> | LinkerResult | null;\n}\n\n/**\n * Creates a Rolldown-compatible plugin for Angular linker\n */\nexport function angularLinkerRolldownPlugin(\n options?: LinkerOptions\n): RolldownPlugin {\n const debug = options?.debug ?? false;\n let compiler: CompilerBinding;\n\n return {\n name: \"angular-linker-rolldown\",\n async transform(code: string, id: string): Promise<LinkerResult | null> {\n // Lazy initialize compiler\n if (!compiler) {\n compiler = getCompiler(options);\n }\n\n // Only process @angular packages with .mjs or .js extensions\n const isInNodeModules = id.includes(\"node_modules\");\n const cleanId = cleanModuleId(id);\n\n if (!isAngularPackage(id) || !isInNodeModules || !isJsFile(id)) {\n return null;\n }\n\n // Check if file contains partial declarations\n if (!needsLinking(code)) {\n return null;\n }\n\n if (debug) {\n console.log(`[Angular Linker] Linking: ${cleanId}`);\n }\n\n try {\n const result = compiler.linkFile(cleanId, code);\n\n if (result.startsWith(\"/* Linker Error\")) {\n console.error(`[Angular Linker Error] ${id}:\\n${result}`);\n return null;\n }\n\n if (debug) {\n console.log(`[Angular Linker] Successfully linked: ${cleanId}`);\n }\n\n return { code: result, map: null };\n } catch (e) {\n console.error(`[Angular Linker Failed] ${id}:`, e);\n return null;\n }\n },\n };\n}\n\nexport default angularLinkerRolldownPlugin;\n","/**\n * Angular Linker Plugin Types\n */\n\nexport interface LinkerOptions {\n /**\n * Enable debug logging\n * @default false\n */\n debug?: boolean;\n\n /**\n * Custom path to the Angular Rust binding package\n * If not specified, will try to resolve @anthropic/angular-rust-binding\n */\n bindingPath?: string;\n}\n\nexport interface LinkerResult {\n code: string;\n map: null | undefined;\n}\n\nexport interface CompilerBinding {\n linkFile(filePath: string, code: string): string;\n compile?(filePath: string, code: string): string;\n}\n\n/**\n * Check if file contains Angular partial declarations that need linking\n */\nexport function needsLinking(code: string): boolean {\n return code.includes(\"ɵɵngDeclare\");\n}\n\n/**\n * Check if file is an Angular package file\n */\nexport function isAngularPackage(id: string): boolean {\n return id.includes(\"@angular\") || id.includes(\"/@angular/\");\n}\n\n/**\n * Check if file is a JavaScript/MJS file\n */\nexport function isJsFile(id: string): boolean {\n const cleanId = id.split(\"?\")[0];\n return cleanId.endsWith(\".mjs\") || cleanId.endsWith(\".js\");\n}\n\n/**\n * Clean module ID by removing query strings\n */\nexport function cleanModuleId(id: string): string {\n return id.split(\"?\")[0];\n}\n\n/**\n * Default Angular packages to exclude from pre-bundling\n */\nexport const ANGULAR_PACKAGES = [\n \"@angular/core\",\n \"@angular/common\",\n \"@angular/platform-browser\",\n \"@angular/platform-browser-dynamic\",\n \"@angular/router\",\n \"@angular/forms\",\n \"@angular/animations\",\n \"@angular/cdk\",\n \"@angular/material\",\n];\n\n/**\n * Packages that don't need linking and should be included in pre-bundling\n */\nexport const NON_ANGULAR_PACKAGES = [\"zone.js\", \"rxjs\", \"rxjs/operators\"];\n"],"mappings":";AAwBA,SAAS,qBAAqB;AAC9B,SAAS,SAAS,YAAY;AAC9B,SAAS,qBAAqB;;;ACKvB,SAAS,aAAa,MAAuB;AAClD,SAAO,KAAK,SAAS,uBAAa;AACpC;AAKO,SAAS,iBAAiB,IAAqB;AACpD,SAAO,GAAG,SAAS,UAAU,KAAK,GAAG,SAAS,YAAY;AAC5D;AAKO,SAAS,SAAS,IAAqB;AAC5C,QAAM,UAAU,GAAG,MAAM,GAAG,EAAE,CAAC;AAC/B,SAAO,Q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;AAGA,YAAM,kBAAkB,GAAG,SAAS,cAAc;AAClD,YAAM,UAAU,cAAc,EAAE;AAEhC,UAAI,CAAC,iBAAiB,EAAE,KAAK,CAAC,mBAAmB,CAAC,SAAS,EAAE,GAAG;AAC9D,eAAO;AAAA,MACT;AAGA,UAAI,CAAC,aAAa,IAAI,GAAG;AACvB,eAAO;AAAA,MACT;AAEA,UAAI,OAAO;AACT,gBAAQ,IAAI,6BAA6B,OAAO,EAAE;AAAA,MACpD;AAEA,UAAI;AACF,cAAM,SAAS,SAAS,SAAS,SAAS,IAAI;AAE9C,YAAI,OAAO,WAAW,iBAAiB,GAAG;AACxC,kBAAQ,MAAM,0BAA0B,EAAE;AAAA,EAAM,MAAM,EAAE;AACxD,iBAAO;AAAA,QACT;AAEA,YAAI,OAAO;AACT,kBAAQ,IAAI,yCAAyC,OAAO,EAAE;AAAA,QAChE;AAEA,eAAO,EAAE,MAAM,QAAQ,KAAK,KAAK;AAAA,MACnC,SAAS,GAAG;AACV,gBAAQ,MAAM,2BAA2B,EAAE,KAAK,CAAC;AACjD,eAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAO,mBAAQ;","names":["require"]}
|
package/linker/vite.cjs
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/linker/vite.ts
|
|
21
|
+
var vite_exports = {};
|
|
22
|
+
__export(vite_exports, {
|
|
23
|
+
ANGULAR_PACKAGES: () => ANGULAR_PACKAGES,
|
|
24
|
+
NON_ANGULAR_PACKAGES: () => NON_ANGULAR_PACKAGES,
|
|
25
|
+
angularLinkerVitePlugin: () => angularLinkerVitePlugin,
|
|
26
|
+
default: () => vite_default,
|
|
27
|
+
getAngularViteConfig: () => getAngularViteConfig
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(vite_exports);
|
|
30
|
+
var import_module = require("module");
|
|
31
|
+
var import_path = require("path");
|
|
32
|
+
var import_url = require("url");
|
|
33
|
+
|
|
34
|
+
// src/linker/types.ts
|
|
35
|
+
function needsLinking(code) {
|
|
36
|
+
return code.includes("\u0275\u0275ngDeclare");
|
|
37
|
+
}
|
|
38
|
+
function isAngularPackage(id) {
|
|
39
|
+
return id.includes("@angular") || id.includes("/@angular/");
|
|
40
|
+
}
|
|
41
|
+
function isJsFile(id) {
|
|
42
|
+
const cleanId = id.split("?")[0];
|
|
43
|
+
return cleanId.endsWith(".mjs") || cleanId.endsWith(".js");
|
|
44
|
+
}
|
|
45
|
+
function cleanModuleId(id) {
|
|
46
|
+
return id.split("?")[0];
|
|
47
|
+
}
|
|
48
|
+
var ANGULAR_PACKAGES = [
|
|
49
|
+
"@angular/core",
|
|
50
|
+
"@angular/common",
|
|
51
|
+
"@angular/platform-browser",
|
|
52
|
+
"@angular/platform-browser-dynamic",
|
|
53
|
+
"@angular/router",
|
|
54
|
+
"@angular/forms",
|
|
55
|
+
"@angular/animations",
|
|
56
|
+
"@angular/cdk",
|
|
57
|
+
"@angular/material"
|
|
58
|
+
];
|
|
59
|
+
var NON_ANGULAR_PACKAGES = ["zone.js", "rxjs", "rxjs/operators"];
|
|
60
|
+
|
|
61
|
+
// src/linker/vite.ts
|
|
62
|
+
var import_meta = {};
|
|
63
|
+
var compilerInstance = null;
|
|
64
|
+
function getCompiler(options) {
|
|
65
|
+
if (compilerInstance) {
|
|
66
|
+
return compilerInstance;
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
let binding;
|
|
70
|
+
if (options?.bindingPath) {
|
|
71
|
+
const require2 = (0, import_module.createRequire)(import_meta.url);
|
|
72
|
+
binding = require2(options.bindingPath);
|
|
73
|
+
} else {
|
|
74
|
+
const currentFileUrl = import_meta.url;
|
|
75
|
+
const currentFilePath = (0, import_url.fileURLToPath)(currentFileUrl);
|
|
76
|
+
const currentDir = (0, import_path.dirname)(currentFilePath);
|
|
77
|
+
const require2 = (0, import_module.createRequire)(currentFileUrl);
|
|
78
|
+
const possiblePaths = [
|
|
79
|
+
(0, import_path.join)(currentDir, "..", "binding"),
|
|
80
|
+
// dist/linker/../binding
|
|
81
|
+
(0, import_path.join)(currentDir, "binding"),
|
|
82
|
+
// same directory
|
|
83
|
+
(0, import_path.join)(currentDir, "..", "..", "binding")
|
|
84
|
+
// deeper nesting
|
|
85
|
+
];
|
|
86
|
+
let loadedBinding = null;
|
|
87
|
+
let lastError = null;
|
|
88
|
+
for (const bindingPath of possiblePaths) {
|
|
89
|
+
try {
|
|
90
|
+
loadedBinding = require2(bindingPath);
|
|
91
|
+
break;
|
|
92
|
+
} catch (e) {
|
|
93
|
+
lastError = e;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (!loadedBinding) {
|
|
97
|
+
throw lastError || new Error("Could not find binding in any expected location");
|
|
98
|
+
}
|
|
99
|
+
binding = loadedBinding;
|
|
100
|
+
}
|
|
101
|
+
compilerInstance = new binding.Compiler();
|
|
102
|
+
return compilerInstance;
|
|
103
|
+
} catch (e) {
|
|
104
|
+
throw new Error(`Failed to load Angular Rust binding. Error: ${e}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function angularLinkerVitePlugin(options) {
|
|
108
|
+
const debug = options?.debug ?? false;
|
|
109
|
+
let compiler;
|
|
110
|
+
return {
|
|
111
|
+
name: "angular-linker-vite",
|
|
112
|
+
enforce: "pre",
|
|
113
|
+
config(config) {
|
|
114
|
+
const excludePackages = [
|
|
115
|
+
...ANGULAR_PACKAGES,
|
|
116
|
+
...options?.excludePackages ?? []
|
|
117
|
+
];
|
|
118
|
+
const includePackages = [
|
|
119
|
+
...NON_ANGULAR_PACKAGES,
|
|
120
|
+
...options?.includePackages ?? []
|
|
121
|
+
];
|
|
122
|
+
return {
|
|
123
|
+
optimizeDeps: {
|
|
124
|
+
exclude: excludePackages,
|
|
125
|
+
include: includePackages
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
},
|
|
129
|
+
transform(code, id) {
|
|
130
|
+
if (!compiler) {
|
|
131
|
+
compiler = getCompiler(options);
|
|
132
|
+
}
|
|
133
|
+
if (!id.includes("node_modules")) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
if (!isAngularPackage(id)) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
if (!isJsFile(id)) {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
if (!needsLinking(code)) {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
const cleanId = cleanModuleId(id);
|
|
146
|
+
if (debug) {
|
|
147
|
+
console.log(`[Angular Linker] Linking: ${cleanId}`);
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
const result = compiler.linkFile(cleanId, code);
|
|
151
|
+
if (result.startsWith("/* Linker Error")) {
|
|
152
|
+
console.error(`[Angular Linker Error] ${id}:
|
|
153
|
+
${result}`);
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
if (debug) {
|
|
157
|
+
console.log(`[Angular Linker] Successfully linked: ${cleanId}`);
|
|
158
|
+
}
|
|
159
|
+
return { code: result, map: null };
|
|
160
|
+
} catch (e) {
|
|
161
|
+
console.error(`[Angular Linker Failed] ${id}:`, e);
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
function getAngularViteConfig() {
|
|
168
|
+
return {
|
|
169
|
+
plugins: [angularLinkerVitePlugin()],
|
|
170
|
+
optimizeDeps: {
|
|
171
|
+
exclude: ANGULAR_PACKAGES,
|
|
172
|
+
include: NON_ANGULAR_PACKAGES
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
var vite_default = angularLinkerVitePlugin;
|
|
177
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
178
|
+
0 && (module.exports = {
|
|
179
|
+
ANGULAR_PACKAGES,
|
|
180
|
+
NON_ANGULAR_PACKAGES,
|
|
181
|
+
angularLinkerVitePlugin,
|
|
182
|
+
getAngularViteConfig
|
|
183
|
+
});
|
|
184
|
+
//# sourceMappingURL=vite.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/linker/vite.ts","../../src/linker/types.ts"],"sourcesContent":["/**\n * Angular Linker Plugin for Vite\n *\n * This plugin handles Angular linking for both Vite's dev server (with rolldown/esbuild)\n * and production builds.\n *\n * @example\n * ```js\n * import { angularLinkerVitePlugin } from 'vite-plugin-angular-rust/vite';\n * import { defineConfig } from 'vite';\n *\n * export default defineConfig({\n * plugins: [angularLinkerVitePlugin()],\n * optimizeDeps: {\n * exclude: [\n * '@angular/core',\n * '@angular/common',\n * '@angular/platform-browser',\n * '@angular/router',\n * '@angular/forms',\n * ],\n * include: ['zone.js', 'rxjs', 'rxjs/operators'],\n * },\n * });\n * ```\n */\n\nimport type { Plugin } from \"vite\";\nimport { createRequire } from \"module\";\nimport { dirname, join } from \"path\";\nimport { fileURLToPath } from \"url\";\nimport type { CompilerBinding, LinkerOptions } from \"./types\";\nimport {\n needsLinking,\n isAngularPackage,\n isJsFile,\n cleanModuleId,\n ANGULAR_PACKAGES,\n NON_ANGULAR_PACKAGES,\n} from \"./types\";\n\nlet compilerInstance: CompilerBinding | null = null;\n\nfunction getCompiler(options?: LinkerOptions): CompilerBinding {\n if (compilerInstance) {\n return compilerInstance;\n }\n\n try {\n let binding: { Compiler: new () => CompilerBinding };\n\n if (options?.bindingPath) {\n const require = createRequire(import.meta.url);\n binding = require(options.bindingPath);\n } else {\n // Load from bundled binding directory\n const currentFileUrl = import.meta.url;\n const currentFilePath = fileURLToPath(currentFileUrl);\n const currentDir = dirname(currentFilePath);\n const require = createRequire(currentFileUrl);\n\n // Try multiple possible binding locations\n const possiblePaths = [\n join(currentDir, \"..\", \"binding\"), // dist/linker/../binding\n join(currentDir, \"binding\"), // same directory\n join(currentDir, \"..\", \"..\", \"binding\"), // deeper nesting\n ];\n\n let loadedBinding: { Compiler: new () => CompilerBinding } | null = null;\n let lastError: unknown = null;\n\n for (const bindingPath of possiblePaths) {\n try {\n loadedBinding = require(bindingPath);\n break;\n } catch (e) {\n lastError = e;\n }\n }\n\n if (!loadedBinding) {\n throw (\n lastError ||\n new Error(\"Could not find binding in any expected location\")\n );\n }\n\n binding = loadedBinding;\n }\n\n compilerInstance = new binding.Compiler();\n return compilerInstance;\n } catch (e) {\n throw new Error(`Failed to load Angular Rust binding. Error: ${e}`);\n }\n}\n\nexport interface ViteLinkerPluginOptions extends LinkerOptions {\n /**\n * Additional packages to exclude from pre-bundling\n */\n excludePackages?: string[];\n\n /**\n * Additional packages to include in pre-bundling (non-Angular packages)\n */\n includePackages?: string[];\n}\n\n/**\n * Creates a Vite plugin for Angular linker\n * Works with both rolldown-vite and standard Vite (esbuild)\n */\nexport function angularLinkerVitePlugin(\n options?: ViteLinkerPluginOptions\n): Plugin {\n const debug = options?.debug ?? false;\n let compiler: CompilerBinding;\n\n return {\n name: \"angular-linker-vite\",\n enforce: \"pre\",\n\n config(config) {\n // Merge optimizeDeps configuration\n const excludePackages = [\n ...ANGULAR_PACKAGES,\n ...(options?.excludePackages ?? []),\n ];\n const includePackages = [\n ...NON_ANGULAR_PACKAGES,\n ...(options?.includePackages ?? []),\n ];\n\n return {\n optimizeDeps: {\n exclude: excludePackages,\n include: includePackages,\n },\n };\n },\n\n transform(code: string, id: string) {\n // Lazy initialize compiler\n if (!compiler) {\n compiler = getCompiler(options);\n }\n\n // Only process node_modules\n if (!id.includes(\"node_modules\")) {\n return null;\n }\n\n // Only process Angular packages\n if (!isAngularPackage(id)) {\n return null;\n }\n\n // Only process JS files\n if (!isJsFile(id)) {\n return null;\n }\n\n // Check if file contains partial declarations\n if (!needsLinking(code)) {\n return null;\n }\n\n const cleanId = cleanModuleId(id);\n\n if (debug) {\n console.log(`[Angular Linker] Linking: ${cleanId}`);\n }\n\n try {\n const result = compiler.linkFile(cleanId, code);\n\n if (result.startsWith(\"/* Linker Error\")) {\n console.error(`[Angular Linker Error] ${id}:\\n${result}`);\n return null;\n }\n\n if (debug) {\n console.log(`[Angular Linker] Successfully linked: ${cleanId}`);\n }\n\n return { code: result, map: null };\n } catch (e) {\n console.error(`[Angular Linker Failed] ${id}:`, e);\n return null;\n }\n },\n };\n}\n\n/**\n * Get recommended Vite config for Angular with Rust linker\n */\nexport function getAngularViteConfig() {\n return {\n plugins: [angularLinkerVitePlugin()],\n optimizeDeps: {\n exclude: ANGULAR_PACKAGES,\n include: NON_ANGULAR_PACKAGES,\n },\n };\n}\n\nexport { ANGULAR_PACKAGES, NON_ANGULAR_PACKAGES };\nexport default angularLinkerVitePlugin;\n","/**\n * Angular Linker Plugin Types\n */\n\nexport interface LinkerOptions {\n /**\n * Enable debug logging\n * @default false\n */\n debug?: boolean;\n\n /**\n * Custom path to the Angular Rust binding package\n * If not specified, will try to resolve @anthropic/angular-rust-binding\n */\n bindingPath?: string;\n}\n\nexport interface LinkerResult {\n code: string;\n map: null | undefined;\n}\n\nexport interface CompilerBinding {\n linkFile(filePath: string, code: string): string;\n compile?(filePath: string, code: string): string;\n}\n\n/**\n * Check if file contains Angular partial declarations that need linking\n */\nexport function needsLinking(code: string): boolean {\n return code.includes(\"ɵɵngDeclare\");\n}\n\n/**\n * Check if file is an Angular package file\n */\nexport function isAngularPackage(id: string): boolean {\n return id.includes(\"@angular\") || id.includes(\"/@angular/\");\n}\n\n/**\n * Check if file is a JavaScript/MJS file\n */\nexport function isJsFile(id: string): boolean {\n const cleanId = id.split(\"?\")[0];\n return cleanId.endsWith(\".mjs\") || cleanId.endsWith(\".js\");\n}\n\n/**\n * Clean module ID by removing query strings\n */\nexport function cleanModuleId(id: string): string {\n return id.split(\"?\")[0];\n}\n\n/**\n * Default Angular packages to exclude from pre-bundling\n */\nexport const ANGULAR_PACKAGES = [\n \"@angular/core\",\n \"@angular/common\",\n \"@angular/platform-browser\",\n \"@angular/platform-browser-dynamic\",\n \"@angular/router\",\n \"@angular/forms\",\n \"@angular/animations\",\n \"@angular/cdk\",\n \"@angular/material\",\n];\n\n/**\n * Packages that don't need linking and should be included in pre-bundling\n */\nexport const NON_ANGULAR_PACKAGES = [\"zone.js\", \"rxjs\", \"rxjs/operators\"];\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA4BA,oBAA8B;AAC9B,kBAA8B;AAC9B,iBAA8B;;;ACCvB,SAAS,aAAa,MAAuB;AAClD,SAAO,KAAK,SAAS,uBAAa;AACpC;AAKO,SAAS,iBAAiB,IAAqB;AACpD,SAAO,GAAG,SAAS,UAAU,KAAK,GAAG,SAAS,YAAY;AAC5D;AAKO,SAAS,SAAS,IAAqB;AAC5C,QAAM,UAAU,GAAG,MAAM,GAAG,EAAE,CAAC;AAC/B,SAAO,QAAQ,SAAS,MAAM,KAAK,QAAQ,SAAS,KAAK;AAC3D;AAKO,SAAS,cAAc,IAAoB;AAChD,SAAO,GAAG,MAAM,GAAG,EAAE,CAAC;AACxB;A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AAA,MACT,SAAS;AAAA,IACX;AAAA,EACF;AACF;AAGA,IAAO,eAAQ;","names":["require"]}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
import { L as LinkerOptions } from '../types-BTaYbdhr.cjs';
|
|
3
|
+
export { A as ANGULAR_PACKAGES, N as NON_ANGULAR_PACKAGES } from '../types-BTaYbdhr.cjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Angular Linker Plugin for Vite
|
|
7
|
+
*
|
|
8
|
+
* This plugin handles Angular linking for both Vite's dev server (with rolldown/esbuild)
|
|
9
|
+
* and production builds.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```js
|
|
13
|
+
* import { angularLinkerVitePlugin } from 'vite-plugin-angular-rust/vite';
|
|
14
|
+
* import { defineConfig } from 'vite';
|
|
15
|
+
*
|
|
16
|
+
* export default defineConfig({
|
|
17
|
+
* plugins: [angularLinkerVitePlugin()],
|
|
18
|
+
* optimizeDeps: {
|
|
19
|
+
* exclude: [
|
|
20
|
+
* '@angular/core',
|
|
21
|
+
* '@angular/common',
|
|
22
|
+
* '@angular/platform-browser',
|
|
23
|
+
* '@angular/router',
|
|
24
|
+
* '@angular/forms',
|
|
25
|
+
* ],
|
|
26
|
+
* include: ['zone.js', 'rxjs', 'rxjs/operators'],
|
|
27
|
+
* },
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
interface ViteLinkerPluginOptions extends LinkerOptions {
|
|
33
|
+
/**
|
|
34
|
+
* Additional packages to exclude from pre-bundling
|
|
35
|
+
*/
|
|
36
|
+
excludePackages?: string[];
|
|
37
|
+
/**
|
|
38
|
+
* Additional packages to include in pre-bundling (non-Angular packages)
|
|
39
|
+
*/
|
|
40
|
+
includePackages?: string[];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Creates a Vite plugin for Angular linker
|
|
44
|
+
* Works with both rolldown-vite and standard Vite (esbuild)
|
|
45
|
+
*/
|
|
46
|
+
declare function angularLinkerVitePlugin(options?: ViteLinkerPluginOptions): Plugin;
|
|
47
|
+
/**
|
|
48
|
+
* Get recommended Vite config for Angular with Rust linker
|
|
49
|
+
*/
|
|
50
|
+
declare function getAngularViteConfig(): {
|
|
51
|
+
plugins: Plugin<any>[];
|
|
52
|
+
optimizeDeps: {
|
|
53
|
+
exclude: string[];
|
|
54
|
+
include: string[];
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export { type ViteLinkerPluginOptions, angularLinkerVitePlugin, angularLinkerVitePlugin as default, getAngularViteConfig };
|
package/linker/vite.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
import { L as LinkerOptions } from '../types-BTaYbdhr.js';
|
|
3
|
+
export { A as ANGULAR_PACKAGES, N as NON_ANGULAR_PACKAGES } from '../types-BTaYbdhr.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Angular Linker Plugin for Vite
|
|
7
|
+
*
|
|
8
|
+
* This plugin handles Angular linking for both Vite's dev server (with rolldown/esbuild)
|
|
9
|
+
* and production builds.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```js
|
|
13
|
+
* import { angularLinkerVitePlugin } from 'vite-plugin-angular-rust/vite';
|
|
14
|
+
* import { defineConfig } from 'vite';
|
|
15
|
+
*
|
|
16
|
+
* export default defineConfig({
|
|
17
|
+
* plugins: [angularLinkerVitePlugin()],
|
|
18
|
+
* optimizeDeps: {
|
|
19
|
+
* exclude: [
|
|
20
|
+
* '@angular/core',
|
|
21
|
+
* '@angular/common',
|
|
22
|
+
* '@angular/platform-browser',
|
|
23
|
+
* '@angular/router',
|
|
24
|
+
* '@angular/forms',
|
|
25
|
+
* ],
|
|
26
|
+
* include: ['zone.js', 'rxjs', 'rxjs/operators'],
|
|
27
|
+
* },
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
interface ViteLinkerPluginOptions extends LinkerOptions {
|
|
33
|
+
/**
|
|
34
|
+
* Additional packages to exclude from pre-bundling
|
|
35
|
+
*/
|
|
36
|
+
excludePackages?: string[];
|
|
37
|
+
/**
|
|
38
|
+
* Additional packages to include in pre-bundling (non-Angular packages)
|
|
39
|
+
*/
|
|
40
|
+
includePackages?: string[];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Creates a Vite plugin for Angular linker
|
|
44
|
+
* Works with both rolldown-vite and standard Vite (esbuild)
|
|
45
|
+
*/
|
|
46
|
+
declare function angularLinkerVitePlugin(options?: ViteLinkerPluginOptions): Plugin;
|
|
47
|
+
/**
|
|
48
|
+
* Get recommended Vite config for Angular with Rust linker
|
|
49
|
+
*/
|
|
50
|
+
declare function getAngularViteConfig(): {
|
|
51
|
+
plugins: Plugin<any>[];
|
|
52
|
+
optimizeDeps: {
|
|
53
|
+
exclude: string[];
|
|
54
|
+
include: string[];
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export { type ViteLinkerPluginOptions, angularLinkerVitePlugin, angularLinkerVitePlugin as default, getAngularViteConfig };
|
package/linker/vite.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// src/linker/vite.ts
|
|
2
|
+
import { createRequire } from "module";
|
|
3
|
+
import { dirname, join } from "path";
|
|
4
|
+
import { fileURLToPath } from "url";
|
|
5
|
+
|
|
6
|
+
// src/linker/types.ts
|
|
7
|
+
function needsLinking(code) {
|
|
8
|
+
return code.includes("\u0275\u0275ngDeclare");
|
|
9
|
+
}
|
|
10
|
+
function isAngularPackage(id) {
|
|
11
|
+
return id.includes("@angular") || id.includes("/@angular/");
|
|
12
|
+
}
|
|
13
|
+
function isJsFile(id) {
|
|
14
|
+
const cleanId = id.split("?")[0];
|
|
15
|
+
return cleanId.endsWith(".mjs") || cleanId.endsWith(".js");
|
|
16
|
+
}
|
|
17
|
+
function cleanModuleId(id) {
|
|
18
|
+
return id.split("?")[0];
|
|
19
|
+
}
|
|
20
|
+
var ANGULAR_PACKAGES = [
|
|
21
|
+
"@angular/core",
|
|
22
|
+
"@angular/common",
|
|
23
|
+
"@angular/platform-browser",
|
|
24
|
+
"@angular/platform-browser-dynamic",
|
|
25
|
+
"@angular/router",
|
|
26
|
+
"@angular/forms",
|
|
27
|
+
"@angular/animations",
|
|
28
|
+
"@angular/cdk",
|
|
29
|
+
"@angular/material"
|
|
30
|
+
];
|
|
31
|
+
var NON_ANGULAR_PACKAGES = ["zone.js", "rxjs", "rxjs/operators"];
|
|
32
|
+
|
|
33
|
+
// src/linker/vite.ts
|
|
34
|
+
var compilerInstance = null;
|
|
35
|
+
function getCompiler(options) {
|
|
36
|
+
if (compilerInstance) {
|
|
37
|
+
return compilerInstance;
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
let binding;
|
|
41
|
+
if (options?.bindingPath) {
|
|
42
|
+
const require2 = createRequire(import.meta.url);
|
|
43
|
+
binding = require2(options.bindingPath);
|
|
44
|
+
} else {
|
|
45
|
+
const currentFileUrl = import.meta.url;
|
|
46
|
+
const currentFilePath = fileURLToPath(currentFileUrl);
|
|
47
|
+
const currentDir = dirname(currentFilePath);
|
|
48
|
+
const require2 = createRequire(currentFileUrl);
|
|
49
|
+
const possiblePaths = [
|
|
50
|
+
join(currentDir, "..", "binding"),
|
|
51
|
+
// dist/linker/../binding
|
|
52
|
+
join(currentDir, "binding"),
|
|
53
|
+
// same directory
|
|
54
|
+
join(currentDir, "..", "..", "binding")
|
|
55
|
+
// deeper nesting
|
|
56
|
+
];
|
|
57
|
+
let loadedBinding = null;
|
|
58
|
+
let lastError = null;
|
|
59
|
+
for (const bindingPath of possiblePaths) {
|
|
60
|
+
try {
|
|
61
|
+
loadedBinding = require2(bindingPath);
|
|
62
|
+
break;
|
|
63
|
+
} catch (e) {
|
|
64
|
+
lastError = e;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (!loadedBinding) {
|
|
68
|
+
throw lastError || new Error("Could not find binding in any expected location");
|
|
69
|
+
}
|
|
70
|
+
binding = loadedBinding;
|
|
71
|
+
}
|
|
72
|
+
compilerInstance = new binding.Compiler();
|
|
73
|
+
return compilerInstance;
|
|
74
|
+
} catch (e) {
|
|
75
|
+
throw new Error(`Failed to load Angular Rust binding. Error: ${e}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function angularLinkerVitePlugin(options) {
|
|
79
|
+
const debug = options?.debug ?? false;
|
|
80
|
+
let compiler;
|
|
81
|
+
return {
|
|
82
|
+
name: "angular-linker-vite",
|
|
83
|
+
enforce: "pre",
|
|
84
|
+
config(config) {
|
|
85
|
+
const excludePackages = [
|
|
86
|
+
...ANGULAR_PACKAGES,
|
|
87
|
+
...options?.excludePackages ?? []
|
|
88
|
+
];
|
|
89
|
+
const includePackages = [
|
|
90
|
+
...NON_ANGULAR_PACKAGES,
|
|
91
|
+
...options?.includePackages ?? []
|
|
92
|
+
];
|
|
93
|
+
return {
|
|
94
|
+
optimizeDeps: {
|
|
95
|
+
exclude: excludePackages,
|
|
96
|
+
include: includePackages
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
transform(code, id) {
|
|
101
|
+
if (!compiler) {
|
|
102
|
+
compiler = getCompiler(options);
|
|
103
|
+
}
|
|
104
|
+
if (!id.includes("node_modules")) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
if (!isAngularPackage(id)) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
if (!isJsFile(id)) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
if (!needsLinking(code)) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
const cleanId = cleanModuleId(id);
|
|
117
|
+
if (debug) {
|
|
118
|
+
console.log(`[Angular Linker] Linking: ${cleanId}`);
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
const result = compiler.linkFile(cleanId, code);
|
|
122
|
+
if (result.startsWith("/* Linker Error")) {
|
|
123
|
+
console.error(`[Angular Linker Error] ${id}:
|
|
124
|
+
${result}`);
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
if (debug) {
|
|
128
|
+
console.log(`[Angular Linker] Successfully linked: ${cleanId}`);
|
|
129
|
+
}
|
|
130
|
+
return { code: result, map: null };
|
|
131
|
+
} catch (e) {
|
|
132
|
+
console.error(`[Angular Linker Failed] ${id}:`, e);
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function getAngularViteConfig() {
|
|
139
|
+
return {
|
|
140
|
+
plugins: [angularLinkerVitePlugin()],
|
|
141
|
+
optimizeDeps: {
|
|
142
|
+
exclude: ANGULAR_PACKAGES,
|
|
143
|
+
include: NON_ANGULAR_PACKAGES
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
var vite_default = angularLinkerVitePlugin;
|
|
148
|
+
export {
|
|
149
|
+
ANGULAR_PACKAGES,
|
|
150
|
+
NON_ANGULAR_PACKAGES,
|
|
151
|
+
angularLinkerVitePlugin,
|
|
152
|
+
vite_default as default,
|
|
153
|
+
getAngularViteConfig
|
|
154
|
+
};
|
|
155
|
+
//# sourceMappingURL=vite.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/linker/vite.ts","../../src/linker/types.ts"],"sourcesContent":["/**\n * Angular Linker Plugin for Vite\n *\n * This plugin handles Angular linking for both Vite's dev server (with rolldown/esbuild)\n * and production builds.\n *\n * @example\n * ```js\n * import { angularLinkerVitePlugin } from 'vite-plugin-angular-rust/vite';\n * import { defineConfig } from 'vite';\n *\n * export default defineConfig({\n * plugins: [angularLinkerVitePlugin()],\n * optimizeDeps: {\n * exclude: [\n * '@angular/core',\n * '@angular/common',\n * '@angular/platform-browser',\n * '@angular/router',\n * '@angular/forms',\n * ],\n * include: ['zone.js', 'rxjs', 'rxjs/operators'],\n * },\n * });\n * ```\n */\n\nimport type { Plugin } from \"vite\";\nimport { createRequire } from \"module\";\nimport { dirname, join } from \"path\";\nimport { fileURLToPath } from \"url\";\nimport type { CompilerBinding, LinkerOptions } from \"./types\";\nimport {\n needsLinking,\n isAngularPackage,\n isJsFile,\n cleanModuleId,\n ANGULAR_PACKAGES,\n NON_ANGULAR_PACKAGES,\n} from \"./types\";\n\nlet compilerInstance: CompilerBinding | null = null;\n\nfunction getCompiler(options?: LinkerOptions): CompilerBinding {\n if (compilerInstance) {\n return compilerInstance;\n }\n\n try {\n let binding: { Compiler: new () => CompilerBinding };\n\n if (options?.bindingPath) {\n const require = createRequire(import.meta.url);\n binding = require(options.bindingPath);\n } else {\n // Load from bundled binding directory\n const currentFileUrl = import.meta.url;\n const currentFilePath = fileURLToPath(currentFileUrl);\n const currentDir = dirname(currentFilePath);\n const require = createRequire(currentFileUrl);\n\n // Try multiple possible binding locations\n const possiblePaths = [\n join(currentDir, \"..\", \"binding\"), // dist/linker/../binding\n join(currentDir, \"binding\"), // same directory\n join(currentDir, \"..\", \"..\", \"binding\"), // deeper nesting\n ];\n\n let loadedBinding: { Compiler: new () => CompilerBinding } | null = null;\n let lastError: unknown = null;\n\n for (const bindingPath of possiblePaths) {\n try {\n loadedBinding = require(bindingPath);\n break;\n } catch (e) {\n lastError = e;\n }\n }\n\n if (!loadedBinding) {\n throw (\n lastError ||\n new Error(\"Could not find binding in any expected location\")\n );\n }\n\n binding = loadedBinding;\n }\n\n compilerInstance = new binding.Compiler();\n return compilerInstance;\n } catch (e) {\n throw new Error(`Failed to load Angular Rust binding. Error: ${e}`);\n }\n}\n\nexport interface ViteLinkerPluginOptions extends LinkerOptions {\n /**\n * Additional packages to exclude from pre-bundling\n */\n excludePackages?: string[];\n\n /**\n * Additional packages to include in pre-bundling (non-Angular packages)\n */\n includePackages?: string[];\n}\n\n/**\n * Creates a Vite plugin for Angular linker\n * Works with both rolldown-vite and standard Vite (esbuild)\n */\nexport function angularLinkerVitePlugin(\n options?: ViteLinkerPluginOptions\n): Plugin {\n const debug = options?.debug ?? false;\n let compiler: CompilerBinding;\n\n return {\n name: \"angular-linker-vite\",\n enforce: \"pre\",\n\n config(config) {\n // Merge optimizeDeps configuration\n const excludePackages = [\n ...ANGULAR_PACKAGES,\n ...(options?.excludePackages ?? []),\n ];\n const includePackages = [\n ...NON_ANGULAR_PACKAGES,\n ...(options?.includePackages ?? []),\n ];\n\n return {\n optimizeDeps: {\n exclude: excludePackages,\n include: includePackages,\n },\n };\n },\n\n transform(code: string, id: string) {\n // Lazy initialize compiler\n if (!compiler) {\n compiler = getCompiler(options);\n }\n\n // Only process node_modules\n if (!id.includes(\"node_modules\")) {\n return null;\n }\n\n // Only process Angular packages\n if (!isAngularPackage(id)) {\n return null;\n }\n\n // Only process JS files\n if (!isJsFile(id)) {\n return null;\n }\n\n // Check if file contains partial declarations\n if (!needsLinking(code)) {\n return null;\n }\n\n const cleanId = cleanModuleId(id);\n\n if (debug) {\n console.log(`[Angular Linker] Linking: ${cleanId}`);\n }\n\n try {\n const result = compiler.linkFile(cleanId, code);\n\n if (result.startsWith(\"/* Linker Error\")) {\n console.error(`[Angular Linker Error] ${id}:\\n${result}`);\n return null;\n }\n\n if (debug) {\n console.log(`[Angular Linker] Successfully linked: ${cleanId}`);\n }\n\n return { code: result, map: null };\n } catch (e) {\n console.error(`[Angular Linker Failed] ${id}:`, e);\n return null;\n }\n },\n };\n}\n\n/**\n * Get recommended Vite config for Angular with Rust linker\n */\nexport function getAngularViteConfig() {\n return {\n plugins: [angularLinkerVitePlugin()],\n optimizeDeps: {\n exclude: ANGULAR_PACKAGES,\n include: NON_ANGULAR_PACKAGES,\n },\n };\n}\n\nexport { ANGULAR_PACKAGES, NON_ANGULAR_PACKAGES };\nexport default angularLinkerVitePlugin;\n","/**\n * Angular Linker Plugin Types\n */\n\nexport interface LinkerOptions {\n /**\n * Enable debug logging\n * @default false\n */\n debug?: boolean;\n\n /**\n * Custom path to the Angular Rust binding package\n * If not specified, will try to resolve @anthropic/angular-rust-binding\n */\n bindingPath?: string;\n}\n\nexport interface LinkerResult {\n code: string;\n map: null | undefined;\n}\n\nexport interface CompilerBinding {\n linkFile(filePath: string, code: string): string;\n compile?(filePath: string, code: string): string;\n}\n\n/**\n * Check if file contains Angular partial declarations that need linking\n */\nexport function needsLinking(code: string): boolean {\n return code.includes(\"ɵɵngDeclare\");\n}\n\n/**\n * Check if file is an Angular package file\n */\nexport function isAngularPackage(id: string): boolean {\n return id.includes(\"@angular\") || id.includes(\"/@angular/\");\n}\n\n/**\n * Check if file is a JavaScript/MJS file\n */\nexport function isJsFile(id: string): boolean {\n const cleanId = id.split(\"?\")[0];\n return cleanId.endsWith(\".mjs\") || cleanId.endsWith(\".js\");\n}\n\n/**\n * Clean module ID by removing query strings\n */\nexport function cleanModuleId(id: string): string {\n return id.split(\"?\")[0];\n}\n\n/**\n * Default Angular packages to exclude from pre-bundling\n */\nexport const ANGULAR_PACKAGES = [\n \"@angular/core\",\n \"@angular/common\",\n \"@angular/platform-browser\",\n \"@angular/platform-browser-dynamic\",\n \"@angular/router\",\n \"@angular/forms\",\n \"@angular/animations\",\n \"@angular/cdk\",\n \"@angular/material\",\n];\n\n/**\n * Packages that don't need linking and should be included in pre-bundling\n */\nexport const NON_ANGULAR_PACKAGES = [\"zone.js\", \"rxjs\", \"rxjs/operators\"];\n"],"mappings":";AA4BA,SAAS,qBAAqB;AAC9B,SAAS,SAAS,YAAY;AAC9B,SAAS,qBAAqB;;;ACCvB,SAAS,aAAa,MAAuB;AAClD,SAAO,KAAK,SAAS,uBAAa;AACpC;AAKO,SAAS,iBAAiB,IAAqB;AACpD,SAAO,GAAG,SAAS,UAAU,KAAK,GAAG,SAAS,YAAY;AAC5D;AAKO,SAAS,SAAS,IAAqB;AAC5C,QAAM,UAAU,GAAG,MAAM,GAAG,EAAE,C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