@hamur/adapter-angular 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +42 -2
- package/dist/chunks/browser-index-7h7x2xz2.js +131 -0
- package/dist/chunks/browser-index-nann848q.js +205 -0
- package/dist/chunks/browser-index-qn8q5fa2.js +580 -0
- package/dist/src/adapter.js +1075 -0
- package/dist/src/error-page.js +34 -0
- package/dist/src/index.js +1729 -0
- package/dist/src/keepalive.js +0 -0
- package/dist/src/runtime/client.js +21 -0
- package/dist/src/runtime/island.js +92 -0
- package/dist/src/runtime/server.js +175 -0
- package/dist/types/src/activation.d.ts +20 -0
- package/dist/types/src/adapter.d.ts +7 -0
- package/dist/types/src/app.d.ts +6 -0
- package/dist/types/src/async-data.d.ts +21 -0
- package/dist/types/src/client-parts.d.ts +35 -0
- package/dist/types/src/compile.d.ts +99 -0
- package/dist/types/src/context.d.ts +74 -0
- package/dist/types/src/data.d.ts +65 -0
- package/dist/types/src/definition.d.ts +13 -0
- package/dist/types/src/error-page.d.ts +8 -0
- package/dist/types/src/error.d.ts +4 -0
- package/dist/types/src/form.d.ts +68 -0
- package/dist/types/src/head.d.ts +12 -0
- package/dist/types/src/hmr.d.ts +14 -0
- package/dist/types/src/hydration.d.ts +16 -0
- package/dist/types/src/index.d.ts +24 -0
- package/dist/types/src/indicators.d.ts +76 -0
- package/dist/types/src/island.d.ts +42 -0
- package/dist/types/src/keepalive.d.ts +1 -0
- package/dist/types/src/link.d.ts +68 -0
- package/dist/types/src/markdown-page.d.ts +10 -0
- package/dist/types/src/markdown.d.ts +40 -0
- package/dist/types/src/mask.d.ts +12 -0
- package/dist/types/src/navigate.d.ts +17 -0
- package/dist/types/src/outlet.d.ts +76 -0
- package/dist/types/src/request.d.ts +31 -0
- package/dist/types/src/routing.d.ts +47 -0
- package/dist/types/src/runtime/client.d.ts +29 -0
- package/dist/types/src/runtime/hydration-dev.d.ts +1 -0
- package/dist/types/src/runtime/hydration.d.ts +8 -0
- package/dist/types/src/runtime/island.d.ts +8 -0
- package/dist/types/src/runtime/server.d.ts +15 -0
- package/dist/types/src/server-island.d.ts +24 -0
- package/dist/types/src/state.d.ts +14 -0
- package/dist/types/src/svg.d.ts +15 -0
- package/dist/types/src/time.d.ts +40 -0
- package/dist/types/src/wrapper.d.ts +21 -0
- package/package.json +77 -4
- package/src/activation.ts +83 -0
- package/src/adapter.ts +107 -0
- package/src/app.ts +12 -0
- package/src/async-data.ts +105 -0
- package/src/client-parts.ts +107 -0
- package/src/compile.ts +963 -0
- package/src/context.ts +189 -0
- package/src/data.ts +389 -0
- package/src/definition.ts +144 -0
- package/src/error-page.ts +30 -0
- package/src/error.ts +15 -0
- package/src/form.ts +182 -0
- package/src/head.ts +63 -0
- package/src/hmr.ts +61 -0
- package/src/hydration.ts +120 -0
- package/src/index.ts +98 -0
- package/src/indicators.ts +220 -0
- package/src/island.ts +89 -0
- package/src/keepalive.ts +1 -0
- package/src/link.ts +214 -0
- package/src/markdown-page.ts +31 -0
- package/src/markdown.ts +140 -0
- package/src/mask.ts +36 -0
- package/src/navigate.ts +22 -0
- package/src/outlet.ts +316 -0
- package/src/request.ts +170 -0
- package/src/routing.ts +218 -0
- package/src/runtime/client.ts +140 -0
- package/src/runtime/contract.d.ts +4 -0
- package/src/runtime/hydration-dev.ts +18 -0
- package/src/runtime/hydration.ts +7 -0
- package/src/runtime/island.ts +109 -0
- package/src/runtime/server.ts +228 -0
- package/src/server-island.ts +67 -0
- package/src/state.ts +70 -0
- package/src/svg.ts +65 -0
- package/src/time.ts +115 -0
- package/src/wrapper.ts +187 -0
|
@@ -0,0 +1,1075 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/adapter-angular/src/adapter.ts
|
|
3
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
4
|
+
import { dirname as dirname2 } from "path";
|
|
5
|
+
import { ADAPTER_CONTRACT, defineAdapter } from "hamur/adapter";
|
|
6
|
+
|
|
7
|
+
// packages/adapter-angular/src/compile.ts
|
|
8
|
+
import { existsSync, readdirSync, readFileSync, realpathSync, statSync, writeFileSync } from "fs";
|
|
9
|
+
import { dirname, join, relative, sep } from "path";
|
|
10
|
+
|
|
11
|
+
// packages/adapter-angular/src/definition.ts
|
|
12
|
+
var QUOTES = new Set(['"', "'", "`"]);
|
|
13
|
+
var OPENERS = new Set(["{", "[", "("]);
|
|
14
|
+
var CLOSERS = new Set(["}", "]", ")"]);
|
|
15
|
+
function endOfString(text, start) {
|
|
16
|
+
const quote = text[start];
|
|
17
|
+
for (let i = start + 1;i < text.length; i++) {
|
|
18
|
+
if (text[i] === "\\")
|
|
19
|
+
i++;
|
|
20
|
+
else if (text[i] === quote)
|
|
21
|
+
return i + 1;
|
|
22
|
+
}
|
|
23
|
+
return text.length;
|
|
24
|
+
}
|
|
25
|
+
function skipComment(text, at) {
|
|
26
|
+
if (text[at] !== "/")
|
|
27
|
+
return;
|
|
28
|
+
if (text[at + 1] === "/") {
|
|
29
|
+
const end = text.indexOf(`
|
|
30
|
+
`, at);
|
|
31
|
+
return end === -1 ? text.length : end;
|
|
32
|
+
}
|
|
33
|
+
if (text[at + 1] === "*") {
|
|
34
|
+
const end = text.indexOf("*/", at + 2);
|
|
35
|
+
return end === -1 ? text.length : end + 2;
|
|
36
|
+
}
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
function endOfExpression(text, from) {
|
|
40
|
+
let depth = 0;
|
|
41
|
+
for (let i = from;i < text.length; i++) {
|
|
42
|
+
const char = text[i];
|
|
43
|
+
if (QUOTES.has(char))
|
|
44
|
+
i = endOfString(text, i) - 1;
|
|
45
|
+
else if (skipComment(text, i) !== undefined)
|
|
46
|
+
i = skipComment(text, i) - 1;
|
|
47
|
+
else if (OPENERS.has(char))
|
|
48
|
+
depth++;
|
|
49
|
+
else if (CLOSERS.has(char)) {
|
|
50
|
+
if (depth === 0)
|
|
51
|
+
return i;
|
|
52
|
+
depth--;
|
|
53
|
+
} else if (char === "," && depth === 0)
|
|
54
|
+
return i;
|
|
55
|
+
}
|
|
56
|
+
return text.length;
|
|
57
|
+
}
|
|
58
|
+
function endOfLiteral(text, open) {
|
|
59
|
+
let depth = 0;
|
|
60
|
+
for (let i = open;i < text.length; i++) {
|
|
61
|
+
const char = text[i];
|
|
62
|
+
if (QUOTES.has(char))
|
|
63
|
+
i = endOfString(text, i) - 1;
|
|
64
|
+
else if (skipComment(text, i) !== undefined)
|
|
65
|
+
i = skipComment(text, i) - 1;
|
|
66
|
+
else if (OPENERS.has(char))
|
|
67
|
+
depth++;
|
|
68
|
+
else if (CLOSERS.has(char) && --depth === 0)
|
|
69
|
+
return i + 1;
|
|
70
|
+
}
|
|
71
|
+
return text.length;
|
|
72
|
+
}
|
|
73
|
+
function entriesOf(object) {
|
|
74
|
+
const found = [];
|
|
75
|
+
let i = 1;
|
|
76
|
+
while (i < object.length) {
|
|
77
|
+
while (i < object.length && /[\s,]/.test(object[i]))
|
|
78
|
+
i++;
|
|
79
|
+
if (object[i] === "}" || i >= object.length)
|
|
80
|
+
break;
|
|
81
|
+
const comment = skipComment(object, i);
|
|
82
|
+
if (comment !== undefined) {
|
|
83
|
+
i = comment;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
let key;
|
|
87
|
+
if (QUOTES.has(object[i])) {
|
|
88
|
+
const end = endOfString(object, i);
|
|
89
|
+
key = object.slice(i + 1, end - 1);
|
|
90
|
+
i = end;
|
|
91
|
+
} else {
|
|
92
|
+
const match = /^[\w$]+/.exec(object.slice(i));
|
|
93
|
+
if (!match)
|
|
94
|
+
break;
|
|
95
|
+
key = match[0];
|
|
96
|
+
i += key.length;
|
|
97
|
+
}
|
|
98
|
+
while (/\s/.test(object[i] ?? ""))
|
|
99
|
+
i++;
|
|
100
|
+
if (object[i] !== ":") {
|
|
101
|
+
i = endOfExpression(object, i);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const end = endOfExpression(object, i + 1);
|
|
105
|
+
found.push([key, object.slice(i + 1, end).trim()]);
|
|
106
|
+
i = end;
|
|
107
|
+
}
|
|
108
|
+
return found;
|
|
109
|
+
}
|
|
110
|
+
var FIRST_STRING = /"((?:[^"\\]|\\.)*)"/;
|
|
111
|
+
function readDefinition(code) {
|
|
112
|
+
const call = code.indexOf("\u0275\u0275defineComponent(");
|
|
113
|
+
if (call === -1)
|
|
114
|
+
return;
|
|
115
|
+
const open = code.indexOf("{", call);
|
|
116
|
+
if (open === -1)
|
|
117
|
+
return;
|
|
118
|
+
const entries = new Map(entriesOf(code.slice(open, endOfLiteral(code, open))));
|
|
119
|
+
const selectors = entries.get("selectors");
|
|
120
|
+
if (selectors === undefined)
|
|
121
|
+
return;
|
|
122
|
+
const inputs = entries.get("inputs");
|
|
123
|
+
const names = inputs === undefined ? [] : entriesOf(inputs).map(([key, value]) => {
|
|
124
|
+
if (value.startsWith('"'))
|
|
125
|
+
return JSON.parse(value);
|
|
126
|
+
const first = FIRST_STRING.exec(value);
|
|
127
|
+
return first ? JSON.parse(`"${first[1]}"`) : key;
|
|
128
|
+
});
|
|
129
|
+
JSON.parse(selectors);
|
|
130
|
+
return { selectors, inputs: names };
|
|
131
|
+
}
|
|
132
|
+
function selectorOf(selectors) {
|
|
133
|
+
const lists = JSON.parse(selectors);
|
|
134
|
+
return lists.map(([tag, ...rest]) => {
|
|
135
|
+
let text = String(tag);
|
|
136
|
+
for (let i = 0;i < rest.length; i += 2) {
|
|
137
|
+
const name = rest[i];
|
|
138
|
+
if (name === 8) {
|
|
139
|
+
for (const name of rest.slice(i + 1))
|
|
140
|
+
text += `.${String(name)}`;
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
if (typeof name !== "string")
|
|
144
|
+
throw new Error(`a selector with :not() cannot be a wrapper: ${selectors}`);
|
|
145
|
+
const value = rest[i + 1];
|
|
146
|
+
text += value === "" ? `[${name}]` : `[${name}=${JSON.stringify(String(value))}]`;
|
|
147
|
+
}
|
|
148
|
+
return text;
|
|
149
|
+
}).join(", ");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// packages/adapter-angular/src/compile.ts
|
|
153
|
+
var COMPILE_ID = /^(?!.*[\\/]node_modules[\\/]).*\.[cm]?ts$/;
|
|
154
|
+
var LINK_ID = /[\\/]node_modules[\\/](?:.+\.m?js|(?:.+[\\/])?@hamur[\\/][^\\/]+[\\/]src[\\/].+\.ts)$/;
|
|
155
|
+
var SCRIPT = /\.[cm]?ts$/;
|
|
156
|
+
var DECORATED = /^\s*@(?:Component|Directive|Pipe|NgModule|Injectable)\s*\(/m;
|
|
157
|
+
var ENTRY = /^(?:(pages|layouts)\/.+|(app|error))\.[cm]?ts$/;
|
|
158
|
+
var NOT_AN_ENTRY = /\.load\.[cm]?ts$|\.d\.[cm]?ts$/;
|
|
159
|
+
var KINDS = { pages: "page", layouts: "layout", app: "app root", error: "error page" };
|
|
160
|
+
var MAP_COMMENT = /\n\/\/# sourceMappingURL=\S+\s*$/;
|
|
161
|
+
var STYLE_LANG = /\.(scss|sass|less|styl|stylus)$/;
|
|
162
|
+
var PREPROCESSED = {
|
|
163
|
+
scss: "sass",
|
|
164
|
+
sass: "sass",
|
|
165
|
+
less: "less",
|
|
166
|
+
styl: "stylus",
|
|
167
|
+
stylus: "stylus"
|
|
168
|
+
};
|
|
169
|
+
var real = (path) => {
|
|
170
|
+
try {
|
|
171
|
+
return realpathSync(path);
|
|
172
|
+
} catch {
|
|
173
|
+
return path;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
var angulars = new Map;
|
|
177
|
+
function loadAngular(rootDir) {
|
|
178
|
+
const dir = dirname(Bun.resolveSync("@angular/compiler-cli/package.json", rootDir));
|
|
179
|
+
let loaded = angulars.get(dir);
|
|
180
|
+
if (!loaded)
|
|
181
|
+
angulars.set(dir, loaded = importAngular(rootDir, dir));
|
|
182
|
+
return loaded;
|
|
183
|
+
}
|
|
184
|
+
async function importAngular(rootDir, dir) {
|
|
185
|
+
const cli = await import(Bun.resolveSync("@angular/compiler-cli", rootDir));
|
|
186
|
+
const linker = await import(Bun.resolveSync("@angular/compiler-cli/linker", rootDir));
|
|
187
|
+
const linkerBabel = await import(Bun.resolveSync("@angular/compiler-cli/linker/babel", rootDir));
|
|
188
|
+
const typescript = await import(Bun.resolveSync("typescript", dir));
|
|
189
|
+
const babel = await import(Bun.resolveSync("@babel/core", dir));
|
|
190
|
+
return {
|
|
191
|
+
cli,
|
|
192
|
+
ts: typescript.default,
|
|
193
|
+
needsLinking: linker.needsLinking,
|
|
194
|
+
linkerPlugin: linkerBabel.createEs2015LinkerPlugin({
|
|
195
|
+
linkerJitMode: false,
|
|
196
|
+
fileSystem: new cli.NodeJSFileSystem,
|
|
197
|
+
logger: new cli.ConsoleLogger(cli.LogLevel.warn)
|
|
198
|
+
}),
|
|
199
|
+
babel
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function scripts(dir) {
|
|
203
|
+
const found = [];
|
|
204
|
+
for (const entry of readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name < b.name ? -1 : 1)) {
|
|
205
|
+
if (entry.name === "node_modules" || entry.name.startsWith("."))
|
|
206
|
+
continue;
|
|
207
|
+
const path = join(dir, entry.name);
|
|
208
|
+
if (entry.isDirectory())
|
|
209
|
+
found.push(...scripts(path));
|
|
210
|
+
else if (SCRIPT.test(entry.name))
|
|
211
|
+
found.push(path);
|
|
212
|
+
}
|
|
213
|
+
return found;
|
|
214
|
+
}
|
|
215
|
+
function ownComponents() {
|
|
216
|
+
if (/[\\/]node_modules[\\/]/.test(import.meta.dir + "/../../src"))
|
|
217
|
+
return [];
|
|
218
|
+
return scripts(import.meta.dir + "/../../src").filter((file) => !file.endsWith(".test.ts") && DECORATED.test(readFileSync(file, "utf8")));
|
|
219
|
+
}
|
|
220
|
+
function modulePackages(rootDir) {
|
|
221
|
+
const scope = join(rootDir, "node_modules", "@hamur");
|
|
222
|
+
if (!existsSync(scope))
|
|
223
|
+
return [];
|
|
224
|
+
return readdirSync(scope).map((name) => real(join(scope, name))).filter((dir) => {
|
|
225
|
+
if (/[\\/]node_modules[\\/]/.test(dir))
|
|
226
|
+
return false;
|
|
227
|
+
try {
|
|
228
|
+
const { exports } = JSON.parse(readFileSync(join(dir, "package.json"), "utf8"));
|
|
229
|
+
return typeof exports === "object" && exports !== null && "./angular" in exports;
|
|
230
|
+
} catch {
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
function moduleComponents(rootDir) {
|
|
236
|
+
return modulePackages(rootDir).flatMap((dir) => scripts(join(dir, "src")).filter((file) => !file.endsWith(".test.ts") && DECORATED.test(readFileSync(file, "utf8"))));
|
|
237
|
+
}
|
|
238
|
+
function commonDir(a, b) {
|
|
239
|
+
let dir = a;
|
|
240
|
+
while (relative(dir, b).startsWith(".."))
|
|
241
|
+
dir = dirname(dir);
|
|
242
|
+
return dir;
|
|
243
|
+
}
|
|
244
|
+
function workspaceRoot(rootDir, base) {
|
|
245
|
+
if (!ownComponents().length)
|
|
246
|
+
return {};
|
|
247
|
+
const root = [...modulePackages(rootDir), real(dirname(import.meta.dir + "/../../src"))].reduce(commonDir, real(rootDir));
|
|
248
|
+
return { rootDir: root, ...base.rootDirs ? { rootDirs: [...base.rootDirs, root] } : {} };
|
|
249
|
+
}
|
|
250
|
+
function rootNames(rootDir) {
|
|
251
|
+
const app = join(rootDir, "app");
|
|
252
|
+
return [...existsSync(app) ? scripts(app) : [], ...ownComponents(), ...moduleComponents(rootDir)];
|
|
253
|
+
}
|
|
254
|
+
var shown = (rootDir, file) => relative(rootDir, file).split(sep).join("/");
|
|
255
|
+
function diagnosticProblem(typescript, diagnostic, rootDir) {
|
|
256
|
+
const code = diagnostic.code < 0 ? `NG${String(-diagnostic.code).slice(2)}` : `TS${diagnostic.code}`;
|
|
257
|
+
const message = typescript.flattenDiagnosticMessageText(diagnostic.messageText, `
|
|
258
|
+
`);
|
|
259
|
+
if (!diagnostic.file || diagnostic.start === undefined)
|
|
260
|
+
return { line: `error ${code}: ${message}` };
|
|
261
|
+
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
|
|
262
|
+
return {
|
|
263
|
+
file: real(diagnostic.file.fileName),
|
|
264
|
+
line: `${shown(rootDir, diagnostic.file.fileName)}(${line + 1},${character + 1}): error ${code}: ${message}`
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
function isComponent(typescript, node) {
|
|
268
|
+
return (typescript.getDecorators(node) ?? []).some(({ expression }) => {
|
|
269
|
+
if (!typescript.isCallExpression(expression))
|
|
270
|
+
return false;
|
|
271
|
+
const callee = expression.expression;
|
|
272
|
+
if (typescript.isIdentifier(callee))
|
|
273
|
+
return callee.text === "Component";
|
|
274
|
+
return typescript.isPropertyAccessExpression(callee) && callee.name.text === "Component";
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
function found(typescript, node) {
|
|
278
|
+
if (typescript.isExportAssignment(node))
|
|
279
|
+
return found(typescript, node.expression);
|
|
280
|
+
if (typescript.isVariableDeclaration(node))
|
|
281
|
+
return node.initializer ? found(typescript, node.initializer) : "undefined";
|
|
282
|
+
if (typescript.isParenthesizedExpression(node) || typescript.isAsExpression(node) || typescript.isSatisfiesExpression(node))
|
|
283
|
+
return found(typescript, node.expression);
|
|
284
|
+
if (typescript.isClassDeclaration(node) || typescript.isClassExpression(node))
|
|
285
|
+
return isComponent(typescript, node) ? undefined : "class without @Component";
|
|
286
|
+
if (typescript.isFunctionLike(node))
|
|
287
|
+
return "function";
|
|
288
|
+
if (typescript.isObjectLiteralExpression(node))
|
|
289
|
+
return "object";
|
|
290
|
+
if (typescript.isArrayLiteralExpression(node))
|
|
291
|
+
return "array";
|
|
292
|
+
if (typescript.isStringLiteralLike(node) || typescript.isTemplateExpression(node))
|
|
293
|
+
return "string";
|
|
294
|
+
if (typescript.isNumericLiteral(node))
|
|
295
|
+
return "number";
|
|
296
|
+
return "other value";
|
|
297
|
+
}
|
|
298
|
+
function defaultExportErrors(typescript, program, rootDir, roots) {
|
|
299
|
+
const checker = program.getTypeChecker();
|
|
300
|
+
const app = join(rootDir, "app");
|
|
301
|
+
const errors = [];
|
|
302
|
+
for (const file of roots) {
|
|
303
|
+
const rel = shown(app, file);
|
|
304
|
+
const entry = ENTRY.exec(rel);
|
|
305
|
+
if (!entry || NOT_AN_ENTRY.test(rel))
|
|
306
|
+
continue;
|
|
307
|
+
const source = program.getSourceFile(file);
|
|
308
|
+
const module = source && checker.getSymbolAtLocation(source);
|
|
309
|
+
const exported = module && checker.tryGetMemberInModuleExports("default", module);
|
|
310
|
+
if (!exported)
|
|
311
|
+
continue;
|
|
312
|
+
const symbol = exported.flags & typescript.SymbolFlags.Alias ? checker.getAliasedSymbol(exported) : exported;
|
|
313
|
+
const node = symbol.valueDeclaration ?? symbol.declarations?.[0];
|
|
314
|
+
const kind = node ? found(typescript, node) : "undefined";
|
|
315
|
+
if (kind !== undefined)
|
|
316
|
+
errors.push({
|
|
317
|
+
file: real(file),
|
|
318
|
+
line: `${shown(rootDir, file)}: the default export of an Angular ${KINDS[entry[1] ?? entry[2]]} must be a class with @Component (found: ${kind})`
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
return errors;
|
|
322
|
+
}
|
|
323
|
+
function asWritten(containingFile, resourceFile) {
|
|
324
|
+
const path = relative(dirname(containingFile), resourceFile).split(sep).join("/");
|
|
325
|
+
return path.startsWith("../") ? path : `./${path}`;
|
|
326
|
+
}
|
|
327
|
+
function compilerHost(ng, options, setup) {
|
|
328
|
+
const host = ng.cli.createCompilerHost({ options });
|
|
329
|
+
host.writeFile = (fileName, text, _bom, _onError, sources) => {
|
|
330
|
+
const source = sources?.[0];
|
|
331
|
+
if (!source)
|
|
332
|
+
return;
|
|
333
|
+
const key = real(source.fileName);
|
|
334
|
+
const entry = setup.files.get(key) ?? { code: "" };
|
|
335
|
+
if (fileName.endsWith(".map"))
|
|
336
|
+
entry.map = text;
|
|
337
|
+
else if (fileName.endsWith(".d.ts"))
|
|
338
|
+
entry.types = text;
|
|
339
|
+
else if (/\.[cm]?js$/.test(fileName))
|
|
340
|
+
entry.code = text.replace(MAP_COMMENT, "");
|
|
341
|
+
else
|
|
342
|
+
return;
|
|
343
|
+
setup.files.set(key, entry);
|
|
344
|
+
};
|
|
345
|
+
host.readResource = (file) => {
|
|
346
|
+
const text = host.readFile(file);
|
|
347
|
+
if (text === undefined)
|
|
348
|
+
throw new Error(`${shown(setup.rootDir, file)}: cannot be read`);
|
|
349
|
+
setup.onResource?.(file);
|
|
350
|
+
return text;
|
|
351
|
+
};
|
|
352
|
+
host.transformResource = async (data, context) => {
|
|
353
|
+
const file = context.resourceFile;
|
|
354
|
+
const lang = file ? STYLE_LANG.exec(file)?.[1] : undefined;
|
|
355
|
+
if (!file || !lang)
|
|
356
|
+
return null;
|
|
357
|
+
if (!setup.preprocessStyle)
|
|
358
|
+
throw new Error(`${shown(setup.rootDir, context.containingFile)}: styleUrl "${asWritten(context.containingFile, file)}" needs the @hamur/css module: install @hamur/css and ${PREPROCESSED[lang]} and add "@hamur/css" to modules`);
|
|
359
|
+
const css = await setup.preprocessStyle({ lang, code: data, file });
|
|
360
|
+
for (const dependency of css.dependencies)
|
|
361
|
+
setup.onResource?.(dependency, file);
|
|
362
|
+
return { content: css.code };
|
|
363
|
+
};
|
|
364
|
+
return host;
|
|
365
|
+
}
|
|
366
|
+
function problemsOf(ng, program, rootDir, check) {
|
|
367
|
+
const problems = [
|
|
368
|
+
...program.getNgOptionDiagnostics(),
|
|
369
|
+
...program.getNgStructuralDiagnostics(),
|
|
370
|
+
...program.getNgSemanticDiagnostics()
|
|
371
|
+
].filter((diagnostic) => diagnostic.category === ng.ts.DiagnosticCategory.Error).map((diagnostic) => diagnosticProblem(ng.ts, diagnostic, rootDir));
|
|
372
|
+
if (check)
|
|
373
|
+
problems.push(...check(program.getTsProgram()));
|
|
374
|
+
return problems;
|
|
375
|
+
}
|
|
376
|
+
function emitOnly(program, emits) {
|
|
377
|
+
program.emit({
|
|
378
|
+
emitCallback: ({
|
|
379
|
+
program: tsProgram,
|
|
380
|
+
targetSourceFile,
|
|
381
|
+
writeFile,
|
|
382
|
+
cancellationToken,
|
|
383
|
+
emitOnlyDtsFiles,
|
|
384
|
+
customTransformers
|
|
385
|
+
}) => targetSourceFile && !emits(real(targetSourceFile.fileName)) ? { emitSkipped: true, diagnostics: [] } : tsProgram.emit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers)
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
async function emitProgram(ng, rootDir, roots, options, preprocessStyle, emits, check) {
|
|
389
|
+
const files = new Map;
|
|
390
|
+
const program = new ng.cli.NgtscProgram(roots, options, compilerHost(ng, options, { rootDir, preprocessStyle, files }));
|
|
391
|
+
await program.loadNgStructureAsync();
|
|
392
|
+
const problems = problemsOf(ng, program, rootDir, check);
|
|
393
|
+
if (problems.length)
|
|
394
|
+
throw new Error(problems.map((problem) => problem.line).join(`
|
|
395
|
+
`));
|
|
396
|
+
const server = `${real(join(rootDir, "server"))}${sep}`;
|
|
397
|
+
emitOnly(program, (file) => emits(file) && !file.startsWith(server));
|
|
398
|
+
return files;
|
|
399
|
+
}
|
|
400
|
+
function readConfig(typescript, file) {
|
|
401
|
+
if (!existsSync(file))
|
|
402
|
+
return;
|
|
403
|
+
const read = typescript.readConfigFile(file, typescript.sys.readFile);
|
|
404
|
+
if (read.error)
|
|
405
|
+
throw new Error(`${file}: ${typescript.flattenDiagnosticMessageText(read.error.messageText, `
|
|
406
|
+
`)}`);
|
|
407
|
+
return read.config;
|
|
408
|
+
}
|
|
409
|
+
function compilerOptions(typescript, ctx) {
|
|
410
|
+
const appFile = join(ctx.buildDir, "tsconfig.app.json");
|
|
411
|
+
const app = readConfig(typescript, appFile);
|
|
412
|
+
const base = app ? typescript.parseJsonConfigFileContent(app, typescript.sys, dirname(appFile)).options : {
|
|
413
|
+
moduleResolution: typescript.ModuleResolutionKind.Bundler,
|
|
414
|
+
lib: ["lib.esnext.d.ts", "lib.dom.d.ts", "lib.dom.iterable.d.ts"],
|
|
415
|
+
types: [],
|
|
416
|
+
strict: true,
|
|
417
|
+
skipLibCheck: true,
|
|
418
|
+
allowImportingTsExtensions: true
|
|
419
|
+
};
|
|
420
|
+
const angular = readConfig(typescript, join(ctx.rootDir, "tsconfig.json"))?.angularCompilerOptions ?? {};
|
|
421
|
+
return {
|
|
422
|
+
...base,
|
|
423
|
+
...angular,
|
|
424
|
+
...workspaceRoot(ctx.rootDir, base),
|
|
425
|
+
compilationMode: "full",
|
|
426
|
+
strictTemplates: true,
|
|
427
|
+
module: typescript.ModuleKind.ESNext,
|
|
428
|
+
target: typescript.ScriptTarget.ES2022,
|
|
429
|
+
declaration: false,
|
|
430
|
+
noEmit: false,
|
|
431
|
+
emitDeclarationOnly: false,
|
|
432
|
+
noEmitOnError: false,
|
|
433
|
+
sourceMap: ctx.dev,
|
|
434
|
+
inlineSources: ctx.dev,
|
|
435
|
+
...ctx.hmr ? { _enableHmr: true } : {}
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
var programs = new Map;
|
|
439
|
+
function angularProgram(ctx) {
|
|
440
|
+
const roots = rootNames(ctx.rootDir);
|
|
441
|
+
const stamp = roots.map((file) => {
|
|
442
|
+
const stat = statSync(file);
|
|
443
|
+
return `${file}:${stat.size}:${stat.mtimeMs}`;
|
|
444
|
+
}).join(`
|
|
445
|
+
`);
|
|
446
|
+
const key = `${ctx.rootDir}\x00${ctx.buildDir}\x00${ctx.dev}`;
|
|
447
|
+
const hit = programs.get(key);
|
|
448
|
+
if (hit?.stamp === stamp)
|
|
449
|
+
return hit.files;
|
|
450
|
+
const app = `${real(ctx.rootDir)}${sep}`;
|
|
451
|
+
const own = new Set([...ownComponents(), ...moduleComponents(ctx.rootDir)].map(real));
|
|
452
|
+
const files = loadAngular(ctx.rootDir).then((ng) => emitProgram(ng, ctx.rootDir, roots, compilerOptions(ng.ts, ctx), ctx.preprocessStyle, (path) => path.startsWith(app) || own.has(path), (program) => defaultExportErrors(ng.ts, program, ctx.rootDir, roots)));
|
|
453
|
+
programs.set(key, { stamp, files });
|
|
454
|
+
return files;
|
|
455
|
+
}
|
|
456
|
+
var stampOf = (file) => {
|
|
457
|
+
const stat = statSync(file, { throwIfNoEntry: false });
|
|
458
|
+
return stat ? `${stat.mtimeMs}:${stat.size}` : "missing";
|
|
459
|
+
};
|
|
460
|
+
var devPrograms = new Map;
|
|
461
|
+
var devBases = new Map;
|
|
462
|
+
function angularDevProgram(ctx) {
|
|
463
|
+
const key = `${ctx.rootDir}\x00${ctx.buildDir}`;
|
|
464
|
+
let program = devPrograms.get(key);
|
|
465
|
+
if (!program)
|
|
466
|
+
devPrograms.set(key, program = new AngularDevProgram(ctx));
|
|
467
|
+
return program;
|
|
468
|
+
}
|
|
469
|
+
var devProgramOf = (rootDir, buildDir) => devPrograms.get(`${rootDir}\x00${buildDir}`);
|
|
470
|
+
function releaseDevProgram(rootDir, buildDir) {
|
|
471
|
+
devPrograms.delete(`${rootDir}\x00${buildDir}`);
|
|
472
|
+
devBases.delete(`${rootDir}\x00${buildDir}`);
|
|
473
|
+
}
|
|
474
|
+
function setDevBaseURL(rootDir, buildDir, baseURL) {
|
|
475
|
+
devBases.set(`${rootDir}\x00${buildDir}`, baseURL);
|
|
476
|
+
}
|
|
477
|
+
var HMR_URL = /(\u0275\u0275getReplaceMetadataURL\([^()]*?,\s*)import\.meta\.url\)/g;
|
|
478
|
+
var HMR_LISTEN = /import\.meta\.hot\s*&&\s*import\.meta\.hot\.on\(\s*(["'])angular:component-update\1/g;
|
|
479
|
+
var HMR_FIRST_LOAD = /\b([A-Za-z_$][\w$]*_HmrLoad)\(Date\.now\(\)\)/g;
|
|
480
|
+
var HMR_FOOTER = `
|
|
481
|
+
import { onDevEvent as \u0275hamurOnDevEvent } from "hamur/app";
|
|
482
|
+
function \u0275hamurNgOn(event, handler) { queueMicrotask(() => \u0275hamurOnDevEvent(event, handler)); }
|
|
483
|
+
`;
|
|
484
|
+
function rewriteHmrInitializer(code, target, baseURL) {
|
|
485
|
+
if (target === "server")
|
|
486
|
+
return code.replace(HMR_FIRST_LOAD, "void 0");
|
|
487
|
+
const listened = code.replace(HMR_LISTEN, `\u0275hamurNgOn("angular:component-update"`);
|
|
488
|
+
if (listened === code)
|
|
489
|
+
return code;
|
|
490
|
+
const base = `new URL(${JSON.stringify(baseURL)}, location.href).href`;
|
|
491
|
+
return `${listened.replace(HMR_URL, (_call, head) => `${head}${base})`)}${HMR_FOOTER}`;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
class AngularDevProgram {
|
|
495
|
+
emitted = [];
|
|
496
|
+
ctx;
|
|
497
|
+
root;
|
|
498
|
+
app;
|
|
499
|
+
server;
|
|
500
|
+
own;
|
|
501
|
+
files = new Map;
|
|
502
|
+
texts = new Map;
|
|
503
|
+
resources = new Map;
|
|
504
|
+
importers = new Map;
|
|
505
|
+
sourceFiles = new Map;
|
|
506
|
+
resourcesOf = new Map;
|
|
507
|
+
broken = new Set;
|
|
508
|
+
options;
|
|
509
|
+
program;
|
|
510
|
+
problems = [];
|
|
511
|
+
failure;
|
|
512
|
+
built = false;
|
|
513
|
+
queue = Promise.resolve();
|
|
514
|
+
constructor(ctx) {
|
|
515
|
+
this.ctx = ctx;
|
|
516
|
+
this.root = `${real(ctx.rootDir)}${sep}`;
|
|
517
|
+
this.app = `${real(join(ctx.rootDir, "app"))}${sep}`;
|
|
518
|
+
this.server = `${real(join(ctx.rootDir, "server"))}${sep}`;
|
|
519
|
+
this.own = new Set([...ownComponents(), ...moduleComponents(ctx.rootDir)].map(real));
|
|
520
|
+
}
|
|
521
|
+
emits(path) {
|
|
522
|
+
return path.startsWith(this.root) && !path.startsWith(this.server) || this.own.has(path);
|
|
523
|
+
}
|
|
524
|
+
async load(path) {
|
|
525
|
+
if (!this.emits(path))
|
|
526
|
+
return;
|
|
527
|
+
const text = existsSync(path) ? readFileSync(path, "utf8") : undefined;
|
|
528
|
+
const next = this.queue.then(async () => {
|
|
529
|
+
const modified = this.modifiedResources();
|
|
530
|
+
if (this.built && modified.length === 0 && !this.stale(path, text))
|
|
531
|
+
return;
|
|
532
|
+
await this.build(modified);
|
|
533
|
+
});
|
|
534
|
+
this.queue = next.catch(() => {});
|
|
535
|
+
await next;
|
|
536
|
+
if (this.failure)
|
|
537
|
+
throw this.failure;
|
|
538
|
+
if (!this.texts.has(path))
|
|
539
|
+
return;
|
|
540
|
+
const problems = this.problems.filter((problem) => problem.file === undefined || problem.file === path || this.reads(path, problem.file));
|
|
541
|
+
if (problems.length)
|
|
542
|
+
throw new Error(problems.map((problem) => problem.line).join(`
|
|
543
|
+
`));
|
|
544
|
+
return this.files.get(path);
|
|
545
|
+
}
|
|
546
|
+
stale(path, text) {
|
|
547
|
+
const compiled = this.texts.get(path);
|
|
548
|
+
if (compiled !== undefined)
|
|
549
|
+
return compiled !== text;
|
|
550
|
+
return text !== undefined && path.startsWith(this.app) && SCRIPT.test(path) && !/\.d\.[cm]?ts$/.test(path);
|
|
551
|
+
}
|
|
552
|
+
version(path) {
|
|
553
|
+
const files = this.resourcesOf.get(path);
|
|
554
|
+
if (!files)
|
|
555
|
+
return "";
|
|
556
|
+
return [...files, ...this.styleImportsOf(files)].sort().map((file) => `${file}:${stampOf(file)}`).join(`
|
|
557
|
+
`);
|
|
558
|
+
}
|
|
559
|
+
dependentsOf(file) {
|
|
560
|
+
const path = real(file);
|
|
561
|
+
return [...this.resourcesOf.keys()].filter((component) => this.reads(component, path));
|
|
562
|
+
}
|
|
563
|
+
async hotUpdate(file) {
|
|
564
|
+
const path = real(file);
|
|
565
|
+
const resources = [...this.resources.keys()].filter((resource) => real(resource) === path);
|
|
566
|
+
if (!resources.length)
|
|
567
|
+
return { updates: [], reload: [] };
|
|
568
|
+
const next = this.queue.then(async () => {
|
|
569
|
+
const modified = this.modifiedResources();
|
|
570
|
+
if (modified.length)
|
|
571
|
+
await this.build(modified);
|
|
572
|
+
});
|
|
573
|
+
this.queue = next.catch(() => {});
|
|
574
|
+
await next;
|
|
575
|
+
const components = this.dependentsOf(path);
|
|
576
|
+
const program = this.program;
|
|
577
|
+
const failing = this.failure || !program ? components : components.filter((component) => this.problems.some((problem) => problem.file === component || !!problem.file && this.reads(component, problem.file)));
|
|
578
|
+
if (failing.length || !program) {
|
|
579
|
+
for (const component of failing)
|
|
580
|
+
this.broken.add(component);
|
|
581
|
+
return { updates: [], reload: failing };
|
|
582
|
+
}
|
|
583
|
+
const reload = components.filter((component) => this.broken.delete(component));
|
|
584
|
+
const ng = await loadAngular(this.ctx.rootDir);
|
|
585
|
+
const styles = resources.flatMap((resource) => [...this.importers.get(resource) ?? []]);
|
|
586
|
+
const nodes = new Set;
|
|
587
|
+
for (const resource of [...resources, ...styles]) {
|
|
588
|
+
for (const node of program.compiler.getComponentsWithTemplateFile(resource))
|
|
589
|
+
nodes.add(node);
|
|
590
|
+
for (const node of program.compiler.getComponentsWithStyleFile(resource))
|
|
591
|
+
nodes.add(node);
|
|
592
|
+
}
|
|
593
|
+
const updates = [];
|
|
594
|
+
for (const node of nodes) {
|
|
595
|
+
if (!ng.ts.isClassDeclaration(node) || !node.name)
|
|
596
|
+
continue;
|
|
597
|
+
const code = program.compiler.emitHmrUpdateModule(node);
|
|
598
|
+
const name = node.name.text.replace(/[$]/g, "\\$&");
|
|
599
|
+
const emitted = this.files.get(real(node.getSourceFile().fileName))?.code ?? "";
|
|
600
|
+
const id = new RegExp(`"([^"]*%40${name})"`).exec(emitted)?.[1];
|
|
601
|
+
if (code !== null && id)
|
|
602
|
+
updates.push({ id, code });
|
|
603
|
+
}
|
|
604
|
+
return { updates, reload };
|
|
605
|
+
}
|
|
606
|
+
reads(component, file) {
|
|
607
|
+
const files = this.resourcesOf.get(component);
|
|
608
|
+
return !!files && (files.has(file) || this.styleImportsOf(files).includes(file));
|
|
609
|
+
}
|
|
610
|
+
styleImportsOf(files) {
|
|
611
|
+
const found = [];
|
|
612
|
+
for (const [dependency, importers] of this.importers)
|
|
613
|
+
if ([...importers].some((importer) => files.has(real(importer))))
|
|
614
|
+
found.push(real(dependency));
|
|
615
|
+
return found;
|
|
616
|
+
}
|
|
617
|
+
modifiedResources() {
|
|
618
|
+
const modified = new Set;
|
|
619
|
+
for (const [file, stamp] of this.resources) {
|
|
620
|
+
if (stampOf(file) === stamp)
|
|
621
|
+
continue;
|
|
622
|
+
modified.add(file);
|
|
623
|
+
for (const importer of this.importers.get(file) ?? [])
|
|
624
|
+
modified.add(importer);
|
|
625
|
+
}
|
|
626
|
+
return [...modified];
|
|
627
|
+
}
|
|
628
|
+
async build(modified) {
|
|
629
|
+
const ng = await loadAngular(this.ctx.rootDir);
|
|
630
|
+
const options = this.options ??= compilerOptions(ng.ts, this.ctx);
|
|
631
|
+
const roots = rootNames(this.ctx.rootDir);
|
|
632
|
+
const written = new Map;
|
|
633
|
+
const host = compilerHost(ng, options, {
|
|
634
|
+
rootDir: this.ctx.rootDir,
|
|
635
|
+
preprocessStyle: this.ctx.preprocessStyle,
|
|
636
|
+
files: written,
|
|
637
|
+
onResource: (file, from) => {
|
|
638
|
+
this.resources.set(file, stampOf(file));
|
|
639
|
+
if (from) {
|
|
640
|
+
const importers = this.importers.get(file) ?? new Set;
|
|
641
|
+
this.importers.set(file, importers.add(from));
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
});
|
|
645
|
+
host.getModifiedResourceFiles = () => new Set(modified);
|
|
646
|
+
const read = host.getSourceFile.bind(host);
|
|
647
|
+
host.getSourceFile = (fileName, language, onError, fresh) => {
|
|
648
|
+
const stamp = /[\\/]node_modules[\\/]/.test(fileName) ? "" : stampOf(fileName);
|
|
649
|
+
const hit = this.sourceFiles.get(fileName);
|
|
650
|
+
if (hit && hit.stamp === stamp && !fresh)
|
|
651
|
+
return hit.file;
|
|
652
|
+
const file = read(fileName, language, onError, fresh);
|
|
653
|
+
if (file)
|
|
654
|
+
this.sourceFiles.set(fileName, { stamp, file });
|
|
655
|
+
return file;
|
|
656
|
+
};
|
|
657
|
+
for (const file of modified)
|
|
658
|
+
this.resources.set(file, stampOf(file));
|
|
659
|
+
const program = new ng.cli.NgtscProgram(roots, options, host, this.program);
|
|
660
|
+
this.built = true;
|
|
661
|
+
this.record(program);
|
|
662
|
+
try {
|
|
663
|
+
await program.loadNgStructureAsync();
|
|
664
|
+
} catch (error) {
|
|
665
|
+
this.failure = error instanceof Error ? error : new Error(String(error));
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
this.failure = undefined;
|
|
669
|
+
this.resourcesOf.clear();
|
|
670
|
+
for (const source of program.getTsProgram().getSourceFiles()) {
|
|
671
|
+
if (source.isDeclarationFile)
|
|
672
|
+
continue;
|
|
673
|
+
const resources = program.compiler.getResourceDependencies(source);
|
|
674
|
+
if (resources.length)
|
|
675
|
+
this.resourcesOf.set(real(source.fileName), new Set(resources.map(real)));
|
|
676
|
+
}
|
|
677
|
+
this.problems = problemsOf(ng, program, this.ctx.rootDir, (tsProgram) => defaultExportErrors(ng.ts, tsProgram, this.ctx.rootDir, roots));
|
|
678
|
+
emitOnly(program, (path) => this.emits(path));
|
|
679
|
+
this.program = program;
|
|
680
|
+
this.emitted = [...written.keys()].filter((file) => this.texts.has(file));
|
|
681
|
+
for (const file of this.emitted)
|
|
682
|
+
this.files.set(file, written.get(file));
|
|
683
|
+
}
|
|
684
|
+
record(program) {
|
|
685
|
+
this.texts.clear();
|
|
686
|
+
for (const source of program.getTsProgram().getSourceFiles()) {
|
|
687
|
+
if (source.isDeclarationFile)
|
|
688
|
+
continue;
|
|
689
|
+
const path = real(source.fileName);
|
|
690
|
+
if (path.startsWith(this.root) || this.own.has(path))
|
|
691
|
+
this.texts.set(path, source.text);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
var GENERATED = ["svg", "markdown", "client-components", "server-components"];
|
|
696
|
+
function generatedKind(ctx, id) {
|
|
697
|
+
const path = real(id);
|
|
698
|
+
return GENERATED.find((dir) => path.startsWith(`${real(join(ctx.buildDir, dir))}${sep}`));
|
|
699
|
+
}
|
|
700
|
+
var generatedEmits = new Map;
|
|
701
|
+
function compileGenerated(ctx, id, code) {
|
|
702
|
+
const path = real(id);
|
|
703
|
+
const hash = `${ctx.dev}\x00${Bun.hash(code)}`;
|
|
704
|
+
const hit = generatedEmits.get(path);
|
|
705
|
+
if (hit?.hash === hash)
|
|
706
|
+
return hit.emit;
|
|
707
|
+
const emit = loadAngular(ctx.rootDir).then(async (ng) => {
|
|
708
|
+
const files = await emitProgram(ng, ctx.rootDir, [path], { ...compilerOptions(ng.ts, ctx), _enableHmr: false }, ctx.preprocessStyle, (file) => file === path);
|
|
709
|
+
const emitted = files.get(path);
|
|
710
|
+
if (!emitted)
|
|
711
|
+
throw new Error(`${shown(ctx.rootDir, id)}: the Angular program emitted nothing for the generated module`);
|
|
712
|
+
return emitted;
|
|
713
|
+
});
|
|
714
|
+
generatedEmits.set(path, { hash, emit });
|
|
715
|
+
emit.catch(() => {
|
|
716
|
+
if (generatedEmits.get(path)?.emit === emit)
|
|
717
|
+
generatedEmits.delete(path);
|
|
718
|
+
});
|
|
719
|
+
return emit;
|
|
720
|
+
}
|
|
721
|
+
var WRAPPER = /^\/\* hamur-angular-component (\{.*\}) \*\//;
|
|
722
|
+
var pascal = (segment) => segment.split(/[^A-Za-z0-9]+/).filter(Boolean).map((part) => part[0].toUpperCase() + part.slice(1)).join("");
|
|
723
|
+
function componentName(path) {
|
|
724
|
+
const segments = path.split("/");
|
|
725
|
+
if (segments.length > 1 && segments[segments.length - 1] === "index")
|
|
726
|
+
segments.pop();
|
|
727
|
+
return segments.map(pascal).join("");
|
|
728
|
+
}
|
|
729
|
+
function componentFile(rootDir, suffix, name) {
|
|
730
|
+
const dir = join(rootDir, "app", "components");
|
|
731
|
+
if (!existsSync(dir))
|
|
732
|
+
return;
|
|
733
|
+
const end = new RegExp(`\\.${suffix}\\.[cm]?ts$`);
|
|
734
|
+
return scripts(dir).find((file) => {
|
|
735
|
+
const rel = relative(dir, file).split(sep).join("/");
|
|
736
|
+
return end.test(rel) && componentName(rel.replace(end, "")) === name;
|
|
737
|
+
});
|
|
738
|
+
}
|
|
739
|
+
var componentFiles = (rootDir) => {
|
|
740
|
+
const dir = join(rootDir, "app", "components");
|
|
741
|
+
return existsSync(dir) ? scripts(dir).filter((file) => /\.(?:client|server)\.[cm]?ts$/.test(file)) : [];
|
|
742
|
+
};
|
|
743
|
+
var COMPONENT = "@Component";
|
|
744
|
+
async function wrapperSource(ctx, spec) {
|
|
745
|
+
const file = spec.file ?? componentFile(ctx.rootDir, spec.kind, spec.name);
|
|
746
|
+
if (!file)
|
|
747
|
+
throw new Error(`${spec.name}.${spec.kind}: there is no app/components/**/${spec.name}.${spec.kind}.ts file`);
|
|
748
|
+
const emitted = ctx.dev ? await angularDevProgram(ctx).load(real(file)) : (await angularProgram(ctx)).get(real(file));
|
|
749
|
+
const definition = emitted && readDefinition(emitted.code);
|
|
750
|
+
if (!definition)
|
|
751
|
+
throw new Error(`${shown(ctx.rootDir, file)}: a .${spec.kind} component must default-export a class with @Component`);
|
|
752
|
+
const q = JSON.stringify;
|
|
753
|
+
const fields = definition.inputs.map((_, index) => `i${index}`);
|
|
754
|
+
const call = spec.kind === "client" ? `clientComponent(${spec.target === "client" ? `() => import(${q(file)})` : ""})` : `serverComponent(${[q(spec.name), ...spec.target === "server" ? ["component"] : []].join(", ")})`;
|
|
755
|
+
return `import { Component, type OnChanges, type OnInit, type SimpleChanges } from "@angular/core";
|
|
756
|
+
import { ${spec.kind === "client" ? "clientComponent" : "serverComponent"} } from ${q(`@hamur/adapter-angular/runtime/${spec.target}`)};
|
|
757
|
+
${spec.kind === "server" && spec.target === "server" ? `import component from ${q(file)};
|
|
758
|
+
` : ""}
|
|
759
|
+
const names: Record<string, string> = ${q(Object.fromEntries(fields.map((field, index) => [field, definition.inputs[index]])))};
|
|
760
|
+
|
|
761
|
+
${COMPONENT}({
|
|
762
|
+
selector: ${q(selectorOf(definition.selectors))},
|
|
763
|
+
template: "",
|
|
764
|
+
inputs: ${q(fields.map((field, index) => `${field}: ${definition.inputs[index]}`))},
|
|
765
|
+
})
|
|
766
|
+
export default class Wrapper implements OnChanges, OnInit {
|
|
767
|
+
${fields.map((field) => ` ${field}: unknown = undefined;`).join(`
|
|
768
|
+
`)}
|
|
769
|
+
private readonly controller = ${call};
|
|
770
|
+
|
|
771
|
+
ngOnChanges(changes: SimpleChanges): void {
|
|
772
|
+
for (const [key, change] of Object.entries(changes)) this.controller.set(names[key]!, change.currentValue);
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
ngOnInit(): void {
|
|
776
|
+
this.controller.init();
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
`;
|
|
780
|
+
}
|
|
781
|
+
function compileStep(ctx) {
|
|
782
|
+
let files;
|
|
783
|
+
const dev = ctx.dev ? angularDevProgram(ctx) : undefined;
|
|
784
|
+
const key = `${ctx.rootDir}\x00${ctx.buildDir}`;
|
|
785
|
+
return {
|
|
786
|
+
name: "hamur-angular-compile",
|
|
787
|
+
filter: { id: COMPILE_ID },
|
|
788
|
+
...dev ? {
|
|
789
|
+
version: (id) => [
|
|
790
|
+
dev.version(real(id)),
|
|
791
|
+
.../^(?:client|server)-components$/.test(generatedKind(ctx, id) ?? "") ? componentFiles(ctx.rootDir).map((file) => `${file}:${stampOf(file)}`) : []
|
|
792
|
+
].join(`
|
|
793
|
+
`)
|
|
794
|
+
} : {},
|
|
795
|
+
async transform(source, { id }) {
|
|
796
|
+
const kind = generatedKind(ctx, id);
|
|
797
|
+
if (kind === "client-components" || kind === "server-components") {
|
|
798
|
+
const marker = WRAPPER.exec(source);
|
|
799
|
+
if (!marker)
|
|
800
|
+
return;
|
|
801
|
+
const wrapper = id.replace(/\.ts$/, ".wrapper.ts");
|
|
802
|
+
const text = await wrapperSource(ctx, JSON.parse(marker[1]));
|
|
803
|
+
if (!existsSync(wrapper) || readFileSync(wrapper, "utf8") !== text)
|
|
804
|
+
writeFileSync(wrapper, text);
|
|
805
|
+
const emitted = await compileGenerated(ctx, wrapper, text);
|
|
806
|
+
return { code: emitted.code, ...emitted.map ? { map: emitted.map } : {}, loader: "js" };
|
|
807
|
+
}
|
|
808
|
+
if (kind) {
|
|
809
|
+
const emitted = await compileGenerated(ctx, id, source);
|
|
810
|
+
return { code: emitted.code, ...emitted.map ? { map: emitted.map } : {}, loader: "js" };
|
|
811
|
+
}
|
|
812
|
+
let emitted;
|
|
813
|
+
if (dev)
|
|
814
|
+
emitted = await dev.load(real(id));
|
|
815
|
+
else {
|
|
816
|
+
files ??= angularProgram(ctx);
|
|
817
|
+
emitted = (await files).get(real(id));
|
|
818
|
+
}
|
|
819
|
+
if (!emitted)
|
|
820
|
+
return;
|
|
821
|
+
const code = ctx.hmr ? rewriteHmrInitializer(emitted.code, ctx.target, devBases.get(key) ?? "/") : emitted.code;
|
|
822
|
+
return { code, ...emitted.map ? { map: emitted.map } : {}, loader: "js" };
|
|
823
|
+
}
|
|
824
|
+
};
|
|
825
|
+
}
|
|
826
|
+
var CLIENT_RUNTIME = /[\\/]adapter-angular[\\/]src[\\/]runtime[\\/]client\.ts$/;
|
|
827
|
+
function hydrationStep() {
|
|
828
|
+
return {
|
|
829
|
+
name: "hamur-angular-hydration",
|
|
830
|
+
filter: { id: CLIENT_RUNTIME },
|
|
831
|
+
transform: (code) => `import "./hydration-dev.ts"; ${code}`
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
var linked = new Map;
|
|
835
|
+
function linkStep(ctx) {
|
|
836
|
+
return {
|
|
837
|
+
name: "hamur-angular-link",
|
|
838
|
+
filter: { id: LINK_ID },
|
|
839
|
+
async transform(code, { id }) {
|
|
840
|
+
const ng = await loadAngular(ctx.rootDir);
|
|
841
|
+
if (!ng.needsLinking(id, code))
|
|
842
|
+
return;
|
|
843
|
+
const hit = linked.get(id);
|
|
844
|
+
if (hit?.size === code.length)
|
|
845
|
+
return hit.code;
|
|
846
|
+
const result = await ng.babel.transformAsync(code, {
|
|
847
|
+
filename: id,
|
|
848
|
+
plugins: [ng.linkerPlugin],
|
|
849
|
+
configFile: false,
|
|
850
|
+
babelrc: false,
|
|
851
|
+
compact: false,
|
|
852
|
+
sourceMaps: false
|
|
853
|
+
});
|
|
854
|
+
const out = result?.code ?? code;
|
|
855
|
+
linked.set(id, { size: code.length, code: out });
|
|
856
|
+
return out;
|
|
857
|
+
}
|
|
858
|
+
};
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
// packages/adapter-angular/src/hmr.ts
|
|
862
|
+
import { addDevServerHandler, useDevChannel, useHamur } from "hamur/kit";
|
|
863
|
+
var stores = new WeakMap;
|
|
864
|
+
var NO_STORE = { "content-type": "text/javascript", "cache-control": "no-store" };
|
|
865
|
+
var angularHooks = {
|
|
866
|
+
"modules:done": () => {
|
|
867
|
+
const hamur = useHamur();
|
|
868
|
+
const { dev, devServer, rootDir, buildDir, app } = hamur.options;
|
|
869
|
+
if (!dev || !devServer.hmr)
|
|
870
|
+
return;
|
|
871
|
+
setDevBaseURL(rootDir, buildDir, app.baseURL);
|
|
872
|
+
const updates = new Map;
|
|
873
|
+
stores.set(hamur, updates);
|
|
874
|
+
addDevServerHandler({
|
|
875
|
+
route: "/@ng/component",
|
|
876
|
+
handler: (request) => {
|
|
877
|
+
if (request.method !== "GET")
|
|
878
|
+
return;
|
|
879
|
+
const id = new URL(request.url).searchParams.get("c");
|
|
880
|
+
if (id === null)
|
|
881
|
+
return;
|
|
882
|
+
return new Response(updates.get(id) ?? "", { headers: NO_STORE });
|
|
883
|
+
}
|
|
884
|
+
});
|
|
885
|
+
},
|
|
886
|
+
"dev:change": async (change) => {
|
|
887
|
+
const hamur = useHamur();
|
|
888
|
+
const { dev, devServer, rootDir, buildDir } = hamur.options;
|
|
889
|
+
const program = dev ? devProgramOf(rootDir, buildDir) : undefined;
|
|
890
|
+
if (!program)
|
|
891
|
+
return;
|
|
892
|
+
if (!devServer.hmr) {
|
|
893
|
+
change.dependents.push(...program.dependentsOf(change.file));
|
|
894
|
+
return;
|
|
895
|
+
}
|
|
896
|
+
const { updates, reload } = await program.hotUpdate(change.file);
|
|
897
|
+
change.dependents.push(...reload);
|
|
898
|
+
const store = stores.get(hamur);
|
|
899
|
+
if (!updates.length || !store)
|
|
900
|
+
return;
|
|
901
|
+
const timestamp = Date.now();
|
|
902
|
+
for (const { id, code } of updates)
|
|
903
|
+
store.set(decodeURIComponent(id), code);
|
|
904
|
+
const channel = useDevChannel();
|
|
905
|
+
for (const { id } of updates)
|
|
906
|
+
channel.send("angular:component-update", { id, timestamp });
|
|
907
|
+
},
|
|
908
|
+
close: () => {
|
|
909
|
+
const { rootDir, buildDir } = useHamur().options;
|
|
910
|
+
releaseDevProgram(rootDir, buildDir);
|
|
911
|
+
}
|
|
912
|
+
};
|
|
913
|
+
|
|
914
|
+
// packages/adapter-angular/src/markdown-page.ts
|
|
915
|
+
import { markdownPageCode } from "hamur/adapter/build";
|
|
916
|
+
var COMPONENT2 = "@Component";
|
|
917
|
+
function module(source) {
|
|
918
|
+
const { imports, value, props, head } = markdownPageCode(source);
|
|
919
|
+
const names = Object.keys(source.components);
|
|
920
|
+
const template = `<hamur-markdown [value]="value"${source.html ? ` [raw]="raw"` : ""}${names.length ? ` [components]="components"` : ""} />`;
|
|
921
|
+
const contents = `import { Component } from "@angular/core";
|
|
922
|
+
import { MarkdownRenderer, injectHead } from "@hamur/adapter-angular";
|
|
923
|
+
${imports}
|
|
924
|
+
const value = ${value};
|
|
925
|
+
const props: Record<string, unknown> = ${props};
|
|
926
|
+
|
|
927
|
+
${COMPONENT2}({ imports: [MarkdownRenderer], template: ${JSON.stringify(template)} })
|
|
928
|
+
export default class MarkdownPage {
|
|
929
|
+
protected readonly value = value as never;
|
|
930
|
+
protected readonly raw = props["raw"] as never;
|
|
931
|
+
protected readonly components = props["components"] as never;
|
|
932
|
+
|
|
933
|
+
constructor() {
|
|
934
|
+
${head ? ` injectHead(${head});
|
|
935
|
+
` : ""} }
|
|
936
|
+
}
|
|
937
|
+
`;
|
|
938
|
+
return { contents, extension: ".ts" };
|
|
939
|
+
}
|
|
940
|
+
var markdownComponent = { module };
|
|
941
|
+
|
|
942
|
+
// packages/adapter-angular/src/svg.ts
|
|
943
|
+
import { svgCode } from "hamur/adapter";
|
|
944
|
+
var TEXT = {
|
|
945
|
+
"&": "&",
|
|
946
|
+
"<": "<",
|
|
947
|
+
">": ">",
|
|
948
|
+
"@": "@",
|
|
949
|
+
"{": '{{ "{" }}',
|
|
950
|
+
"}": '{{ "}" }}'
|
|
951
|
+
};
|
|
952
|
+
var ATTRIBUTE = { "&": "&", '"': """, "<": "<", ">": ">" };
|
|
953
|
+
var text = (value) => value.replace(/[&<>@{}]/g, (char) => TEXT[char]);
|
|
954
|
+
var escaped = (value) => value.replace(/[&"<>]/g, (char) => ATTRIBUTE[char]);
|
|
955
|
+
var COMPONENT3 = "@Component";
|
|
956
|
+
function attribute(code, name, value) {
|
|
957
|
+
if (typeof value === "string" && !/[{}]/.test(value))
|
|
958
|
+
return ` ${name}="${escaped(value)}"`;
|
|
959
|
+
return ` [attr.${name}]="${escaped(code.value(value))}"`;
|
|
960
|
+
}
|
|
961
|
+
function node(code, child) {
|
|
962
|
+
if (typeof child === "string")
|
|
963
|
+
return text(child);
|
|
964
|
+
if (Array.isArray(child))
|
|
965
|
+
return `{{ ${escaped(code.value(child))} }}`;
|
|
966
|
+
const attrs = Object.entries(code.attrs(child)).map(([name, value]) => attribute(code, name, value)).join("");
|
|
967
|
+
return `<${child.tag}${attrs}>${child.children.map((inner) => node(code, inner)).join("")}</${child.tag}>`;
|
|
968
|
+
}
|
|
969
|
+
function module2(svg) {
|
|
970
|
+
const code = svgCode(svg, { title: "title()", props: "{}" });
|
|
971
|
+
const root = Object.entries(svg.attrs).map(([name, value]) => attribute(code, name, value)).join("");
|
|
972
|
+
const hidden = code.hidden ? ` [attr.aria-hidden]="title() == null ? 'true' : null"` : "";
|
|
973
|
+
const children = svg.children.map((child) => node(code, child)).join("");
|
|
974
|
+
const template = `<svg${root}${hidden}>@if (title() != null) {<title>{{ title() }}</title>}${children}</svg>`;
|
|
975
|
+
const contents = `import { Component, NO_ERRORS_SCHEMA, input } from "@angular/core";
|
|
976
|
+
import { injectId } from "@hamur/adapter-angular";
|
|
977
|
+
|
|
978
|
+
${COMPONENT3}({
|
|
979
|
+
schemas: [NO_ERRORS_SCHEMA],
|
|
980
|
+
preserveWhitespaces: true,
|
|
981
|
+
template: ${JSON.stringify(template)},
|
|
982
|
+
})
|
|
983
|
+
export default class SvgComponent {
|
|
984
|
+
readonly title = input<string>();
|
|
985
|
+
protected readonly uid = injectId().replace(/[^\\w-]/g, "_");
|
|
986
|
+
}
|
|
987
|
+
`;
|
|
988
|
+
return { contents, extension: ".ts" };
|
|
989
|
+
}
|
|
990
|
+
var svgComponent = { types: `import("@angular/core").Type<{ title?: string }>`, module: module2 };
|
|
991
|
+
|
|
992
|
+
// packages/adapter-angular/src/adapter.ts
|
|
993
|
+
var ANGULAR = [
|
|
994
|
+
"@angular/core",
|
|
995
|
+
"@angular/common",
|
|
996
|
+
"@angular/compiler",
|
|
997
|
+
"@angular/platform-browser",
|
|
998
|
+
"@angular/platform-server",
|
|
999
|
+
"@angular/compiler-cli"
|
|
1000
|
+
];
|
|
1001
|
+
var manifestOf = (name, from) => {
|
|
1002
|
+
const file = Bun.resolveSync(`${name}/package.json`, from);
|
|
1003
|
+
return { file, manifest: JSON.parse(readFileSync2(file, "utf8")) };
|
|
1004
|
+
};
|
|
1005
|
+
function assertAngular(rootDir) {
|
|
1006
|
+
const core = manifestOf("@angular/core", rootDir).manifest.version;
|
|
1007
|
+
if (core.split(".")[0] !== "22")
|
|
1008
|
+
throw new Error(`@hamur/adapter-angular supports Angular 22: @angular/core ${core} is installed in ${rootDir}. Install it with: bun add ${ANGULAR.map((name) => `${name}@^22.3.0`).join(" ")}`);
|
|
1009
|
+
for (const name of ANGULAR.slice(1)) {
|
|
1010
|
+
const { version } = manifestOf(name, rootDir).manifest;
|
|
1011
|
+
if (version !== core)
|
|
1012
|
+
throw new Error(`@hamur/adapter-angular needs every @angular package at one version; found @angular/core ${core} and ${name} ${version} in ${rootDir}`);
|
|
1013
|
+
}
|
|
1014
|
+
const cli = manifestOf("@angular/compiler-cli", rootDir);
|
|
1015
|
+
const range = cli.manifest.peerDependencies?.typescript ?? ">=6.0 <6.1";
|
|
1016
|
+
let typescript;
|
|
1017
|
+
try {
|
|
1018
|
+
typescript = manifestOf("typescript", dirname2(cli.file)).manifest.version;
|
|
1019
|
+
} catch {
|
|
1020
|
+
typescript = manifestOf("typescript", rootDir).manifest.version;
|
|
1021
|
+
}
|
|
1022
|
+
if (!Bun.semver.satisfies(typescript, range))
|
|
1023
|
+
throw new Error(`@hamur/adapter-angular needs typescript 6.0 for the Angular compiler (its range is "${range}"); typescript ${typescript} is installed in ${rootDir}. Install it with: bun add -d typescript@~6.0.3`);
|
|
1024
|
+
}
|
|
1025
|
+
function angularDefines(target) {
|
|
1026
|
+
return {
|
|
1027
|
+
name: "hamur-angular-define",
|
|
1028
|
+
setup(build) {
|
|
1029
|
+
build.config.define = {
|
|
1030
|
+
ngDevMode: "false",
|
|
1031
|
+
ngJitMode: "false",
|
|
1032
|
+
ngI18nClosureMode: "false",
|
|
1033
|
+
ngServerMode: target === "server" ? "true" : "false",
|
|
1034
|
+
...build.config.define
|
|
1035
|
+
};
|
|
1036
|
+
}
|
|
1037
|
+
};
|
|
1038
|
+
}
|
|
1039
|
+
var wrapper = (kind) => (component) => ({
|
|
1040
|
+
contents: `/* hamur-angular-component ${JSON.stringify({ kind, name: component.name, target: component.target, ...component.file === undefined ? {} : { file: component.file } })} */
|
|
1041
|
+
export default undefined;
|
|
1042
|
+
`,
|
|
1043
|
+
extension: ".ts"
|
|
1044
|
+
});
|
|
1045
|
+
var adapter_default = defineAdapter({
|
|
1046
|
+
name: "angular",
|
|
1047
|
+
contract: ADAPTER_CONTRACT,
|
|
1048
|
+
extensions: [".ts"],
|
|
1049
|
+
clientEntry: "@hamur/adapter-angular/runtime/client",
|
|
1050
|
+
serverEntry: "@hamur/adapter-angular/runtime/server",
|
|
1051
|
+
errorPage: "@hamur/adapter-angular/error-page",
|
|
1052
|
+
islandEntry: "@hamur/adapter-angular/runtime/island",
|
|
1053
|
+
plugins: (ctx) => {
|
|
1054
|
+
assertAngular(ctx.rootDir);
|
|
1055
|
+
return ctx.dev ? [] : [angularDefines(ctx.target)];
|
|
1056
|
+
},
|
|
1057
|
+
transforms: (ctx) => [
|
|
1058
|
+
compileStep(ctx),
|
|
1059
|
+
linkStep(ctx),
|
|
1060
|
+
...ctx.dev && ctx.target === "client" ? [hydrationStep()] : []
|
|
1061
|
+
],
|
|
1062
|
+
hooks: angularHooks,
|
|
1063
|
+
peers: [...ANGULAR, "rxjs", "typescript"],
|
|
1064
|
+
stream: false,
|
|
1065
|
+
trustedTypesPolicies: ["angular", "angular#unsafe-bypass"],
|
|
1066
|
+
svgComponent,
|
|
1067
|
+
markdownComponent,
|
|
1068
|
+
clientComponent: { module: wrapper("client") },
|
|
1069
|
+
serverComponent: { module: wrapper("server") }
|
|
1070
|
+
});
|
|
1071
|
+
export {
|
|
1072
|
+
angularDefines,
|
|
1073
|
+
assertAngular,
|
|
1074
|
+
adapter_default as default
|
|
1075
|
+
};
|