@ng-desktop/builders 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +13 -2
- package/bin/ng-desktop.mjs +200 -0
- package/builders.json +15 -0
- package/fesm2022/application-builder.d.mts +62 -0
- package/fesm2022/application-builder.mjs +58 -0
- package/fesm2022/ast-metadata-extractor.d.mts +40 -0
- package/fesm2022/ast-metadata-extractor.mjs +460 -0
- package/fesm2022/build-bundle.d.mts +45 -0
- package/fesm2022/build-bundle.mjs +462 -0
- package/fesm2022/builder.d.ts +66 -0
- package/fesm2022/builder.js +123 -0
- package/fesm2022/dev-server-builder.d.mts +26 -0
- package/fesm2022/dev-server-builder.mjs +24 -0
- package/fesm2022/dev-server.mjs +47 -0
- package/fesm2022/hmr-client.d.mts +24 -0
- package/fesm2022/hmr-client.mjs +84 -0
- package/fesm2022/icon-compiler.d.mts +6 -0
- package/fesm2022/icon-compiler.mjs +148 -0
- package/fesm2022/index.d.ts +13 -0
- package/fesm2022/index.js +13 -0
- package/fesm2022/permission-analyzer.d.mts +45 -0
- package/fesm2022/permission-analyzer.mjs +333 -0
- package/fesm2022/resolve-engine-binary.d.mts +35 -0
- package/fesm2022/resolve-engine-binary.mjs +130 -0
- package/fesm2022/server-compiler.d.mts +29 -0
- package/fesm2022/server-compiler.mjs +236 -0
- package/fesm2022/style-compiler.d.mts +26 -0
- package/fesm2022/style-compiler.mjs +258 -0
- package/fesm2022/template-compiler.d.mts +53 -0
- package/fesm2022/template-compiler.mjs +892 -0
- package/fesm2022/workspace-resolver.d.mts +24 -0
- package/fesm2022/workspace-resolver.mjs +199 -0
- package/package.json +79 -4
- package/schema/angular-native-schema.json +52 -0
- package/schema/native-builder-schema.json +97 -0
- package/schematics/collection.json +10 -0
- package/schematics/ng-add/index.d.mts +20 -0
- package/schematics/ng-add/index.mjs +56 -0
- package/schematics/ng-add/schema.json +24 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, mkdirSync, readdirSync, statSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { dirname, resolve } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
|
|
7
|
+
function getModuleDir() {
|
|
8
|
+
if (typeof import.meta.url === 'string' && import.meta.url.startsWith('file:')) {
|
|
9
|
+
return dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
}
|
|
11
|
+
return resolve(process.cwd(), 'packages/builders-native/src');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const HTTP_METHOD_SUFFIX = /\.(get|post|put|patch|delete|head|options)$/i;
|
|
15
|
+
|
|
16
|
+
function segmentWeight(seg) {
|
|
17
|
+
if (seg.startsWith('*')) return 1;
|
|
18
|
+
if (seg.startsWith(':')) return 2;
|
|
19
|
+
return 3;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function routeTier(segs) {
|
|
23
|
+
if (segs.some((seg) => seg.startsWith('*'))) return 1;
|
|
24
|
+
if (segs.some((seg) => seg.startsWith(':'))) return 2;
|
|
25
|
+
return 3;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function compareDiscoveredRoutes(a, b) {
|
|
29
|
+
const segsA = a.pattern.split('/').filter(Boolean);
|
|
30
|
+
const segsB = b.pattern.split('/').filter(Boolean);
|
|
31
|
+
const tierDiff = routeTier(segsB) - routeTier(segsA);
|
|
32
|
+
if (tierDiff !== 0) return tierDiff;
|
|
33
|
+
const maxLen = Math.max(segsA.length, segsB.length);
|
|
34
|
+
for (let i = 0; i < maxLen; i += 1) {
|
|
35
|
+
const wA = segsA[i] ? segmentWeight(segsA[i]) : 0;
|
|
36
|
+
const wB = segsB[i] ? segmentWeight(segsB[i]) : 0;
|
|
37
|
+
if (wA !== wB) return wB - wA;
|
|
38
|
+
}
|
|
39
|
+
const exactA = a.method !== '*';
|
|
40
|
+
const exactB = b.method !== '*';
|
|
41
|
+
if (exactA !== exactB) return exactA ? -1 : 1;
|
|
42
|
+
return a.pattern.localeCompare(b.pattern) || a.method.localeCompare(b.method);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function mapFileToRoute(absFile, routesRootAbs) {
|
|
46
|
+
const rel = absFile.slice(routesRootAbs.length).replaceAll('\\', '/').replace(/^\/+/, '');
|
|
47
|
+
const withoutExt = rel.replace(/\.ts$/i, '');
|
|
48
|
+
|
|
49
|
+
let method = '*';
|
|
50
|
+
let routeStem = withoutExt;
|
|
51
|
+
const methodMatch = withoutExt.match(HTTP_METHOD_SUFFIX);
|
|
52
|
+
if (methodMatch) {
|
|
53
|
+
method = methodMatch[1].toUpperCase();
|
|
54
|
+
routeStem = withoutExt.slice(0, -methodMatch[0].length);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (routeStem === 'index') {
|
|
58
|
+
routeStem = '';
|
|
59
|
+
} else if (routeStem.endsWith('/index')) {
|
|
60
|
+
routeStem = routeStem.slice(0, -'/index'.length);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const mappedSegments = routeStem
|
|
64
|
+
.split('/')
|
|
65
|
+
.filter(Boolean)
|
|
66
|
+
.map((seg) => {
|
|
67
|
+
const catchAll = seg.match(/^\[\.\.\.([^\]]+)\]$/);
|
|
68
|
+
if (catchAll) return `*${catchAll[1]}`;
|
|
69
|
+
const dynamicParam = seg.match(/^\[([^\]]+)\]$/);
|
|
70
|
+
if (dynamicParam) return `:${dynamicParam[1]}`;
|
|
71
|
+
return seg;
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
method,
|
|
76
|
+
pattern: `/${mappedSegments.join('/')}`,
|
|
77
|
+
filePath: absFile,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function collectRouteFiles(dirPath, out = []) {
|
|
82
|
+
if (!existsSync(dirPath)) return out;
|
|
83
|
+
for (const entry of readdirSync(dirPath)) {
|
|
84
|
+
const full = resolve(dirPath, entry);
|
|
85
|
+
const stat = statSync(full);
|
|
86
|
+
if (stat.isDirectory()) {
|
|
87
|
+
collectRouteFiles(full, out);
|
|
88
|
+
} else if (
|
|
89
|
+
entry.endsWith('.ts') &&
|
|
90
|
+
!entry.endsWith('.spec.ts') &&
|
|
91
|
+
!entry.endsWith('.test.ts') &&
|
|
92
|
+
!entry.endsWith('.d.ts')
|
|
93
|
+
) {
|
|
94
|
+
out.push(full);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Discovers AnalogJS-style file-based routes under `<sourceRoot>/server/routes/`.
|
|
102
|
+
*/
|
|
103
|
+
export function discoverServerRoutes(sourceRootAbs) {
|
|
104
|
+
const routesRootAbs = resolve(sourceRootAbs, 'server/routes');
|
|
105
|
+
if (!existsSync(routesRootAbs)) {
|
|
106
|
+
return [];
|
|
107
|
+
}
|
|
108
|
+
const files = collectRouteFiles(routesRootAbs);
|
|
109
|
+
return files.map((file) => mapFileToRoute(file, routesRootAbs)).sort(compareDiscoveredRoutes);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function findPnpmEsbuild(baseDir) {
|
|
113
|
+
const pnpmDir = resolve(baseDir, 'node_modules/.pnpm');
|
|
114
|
+
if (!existsSync(pnpmDir)) return null;
|
|
115
|
+
const esbuildPkg = readdirSync(pnpmDir).find((name) => name.startsWith('esbuild@'));
|
|
116
|
+
if (!esbuildPkg) return null;
|
|
117
|
+
const candidate = resolve(pnpmDir, esbuildPkg, 'node_modules/esbuild');
|
|
118
|
+
return existsSync(candidate) ? candidate : null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function resolveEsbuildPath(workspaceRoot) {
|
|
122
|
+
const moduleDir = getModuleDir();
|
|
123
|
+
const roots = [workspaceRoot, resolve(moduleDir, '../../..'), process.cwd()];
|
|
124
|
+
for (const root of roots) {
|
|
125
|
+
try {
|
|
126
|
+
const req = createRequire(resolve(root, 'package.json'));
|
|
127
|
+
return req.resolve('esbuild');
|
|
128
|
+
} catch {
|
|
129
|
+
const pnpmPath = findPnpmEsbuild(root);
|
|
130
|
+
if (pnpmPath) {
|
|
131
|
+
return pnpmPath;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
throw new Error("Cannot locate 'esbuild' to compile server.bundle.js");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function runEsbuildBuild({
|
|
139
|
+
workspaceRoot,
|
|
140
|
+
resolveDir = workspaceRoot,
|
|
141
|
+
entrySource,
|
|
142
|
+
loader = 'ts',
|
|
143
|
+
alias,
|
|
144
|
+
define,
|
|
145
|
+
minifySyntax = false,
|
|
146
|
+
banner,
|
|
147
|
+
}) {
|
|
148
|
+
const esbuildPath = resolveEsbuildPath(workspaceRoot);
|
|
149
|
+
const buildOptions = {
|
|
150
|
+
stdin: {
|
|
151
|
+
contents: entrySource,
|
|
152
|
+
resolveDir,
|
|
153
|
+
loader,
|
|
154
|
+
},
|
|
155
|
+
bundle: true,
|
|
156
|
+
format: 'iife',
|
|
157
|
+
platform: 'browser',
|
|
158
|
+
target: 'es2022',
|
|
159
|
+
write: false,
|
|
160
|
+
mainFields: ['fesm2022', 'module', 'main'],
|
|
161
|
+
...(alias ? { alias } : {}),
|
|
162
|
+
...(define ? { define } : {}),
|
|
163
|
+
...(minifySyntax ? { minifySyntax: true } : {}),
|
|
164
|
+
...(banner ? { banner } : {}),
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
if (typeof window === 'undefined') {
|
|
168
|
+
const req = createRequire(resolve(workspaceRoot, 'package.json'));
|
|
169
|
+
const esbuild = req(esbuildPath);
|
|
170
|
+
const built = esbuild.buildSync(buildOptions);
|
|
171
|
+
return built.outputFiles[0].text;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Vitest jsdom realms override Uint8Array/TextEncoder prototypes; isolate esbuild in Node.
|
|
175
|
+
const runner = [
|
|
176
|
+
"const fs = require('node:fs');",
|
|
177
|
+
'const esbuild = require(process.argv[1]);',
|
|
178
|
+
"const buildOptions = JSON.parse(fs.readFileSync(0, 'utf8'));",
|
|
179
|
+
'const built = esbuild.buildSync(buildOptions);',
|
|
180
|
+
'process.stdout.write(built.outputFiles[0].text);',
|
|
181
|
+
].join('\n');
|
|
182
|
+
|
|
183
|
+
return execFileSync(process.execPath, ['-e', runner, esbuildPath], {
|
|
184
|
+
input: JSON.stringify(buildOptions),
|
|
185
|
+
encoding: 'utf8',
|
|
186
|
+
maxBuffer: 32 * 1024 * 1024,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Bundles discovered `src/server/routes/**` handlers into `<outDir>/server.bundle.js`.
|
|
192
|
+
*/
|
|
193
|
+
export function compileServerBundle({ workspaceRoot, sourceRootAbs, outDir, projectName }) {
|
|
194
|
+
const routes = discoverServerRoutes(sourceRootAbs);
|
|
195
|
+
if (routes.length === 0) {
|
|
196
|
+
return undefined;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
mkdirSync(outDir, { recursive: true });
|
|
200
|
+
|
|
201
|
+
const imports = [];
|
|
202
|
+
const entries = [];
|
|
203
|
+
routes.forEach((route, idx) => {
|
|
204
|
+
const varName = `routeHandler${idx}`;
|
|
205
|
+
imports.push(`import ${varName} from ${JSON.stringify(route.filePath)};`);
|
|
206
|
+
entries.push(
|
|
207
|
+
` { method: ${JSON.stringify(route.method)}, pattern: ${JSON.stringify(route.pattern)}, handler: ${varName} },`,
|
|
208
|
+
);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const entrySource = [
|
|
212
|
+
`import { registerNativeServerApp } from '@ng-desktop/server';`,
|
|
213
|
+
...imports,
|
|
214
|
+
`registerNativeServerApp([`,
|
|
215
|
+
...entries,
|
|
216
|
+
`]);`,
|
|
217
|
+
].join('\n');
|
|
218
|
+
|
|
219
|
+
const localServerEntry = resolve(workspaceRoot, 'packages/server-native/src/index.ts');
|
|
220
|
+
const repoServerEntry = resolve(getModuleDir(), '../..', 'server-native/src/index.ts');
|
|
221
|
+
const cwdServerEntry = resolve(process.cwd(), 'packages/server-native/src/index.ts');
|
|
222
|
+
const alias = {};
|
|
223
|
+
if (existsSync(localServerEntry)) {
|
|
224
|
+
alias['@ng-desktop/server'] = localServerEntry;
|
|
225
|
+
} else if (existsSync(repoServerEntry)) {
|
|
226
|
+
alias['@ng-desktop/server'] = repoServerEntry;
|
|
227
|
+
} else if (existsSync(cwdServerEntry)) {
|
|
228
|
+
alias['@ng-desktop/server'] = cwdServerEntry;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const outputText = runEsbuildBuild({ workspaceRoot, entrySource, alias });
|
|
232
|
+
const preamble = `globalThis.__NG_NATIVE_APP_ID__ = ${JSON.stringify(projectName)};\n`;
|
|
233
|
+
const serverBundleFile = resolve(outDir, 'server.bundle.js');
|
|
234
|
+
writeFileSync(serverBundleFile, preamble + outputText, 'utf8');
|
|
235
|
+
return serverBundleFile;
|
|
236
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface StyleRuleIr {
|
|
2
|
+
className: string;
|
|
3
|
+
background: string;
|
|
4
|
+
color: string;
|
|
5
|
+
padding: string;
|
|
6
|
+
spacing: string;
|
|
7
|
+
borderRadius: number | null;
|
|
8
|
+
fontSize: number | null;
|
|
9
|
+
fontWeight: number | null;
|
|
10
|
+
minWidth: number | null;
|
|
11
|
+
minHeight: number | null;
|
|
12
|
+
orientation: 'horizontal' | 'vertical' | null;
|
|
13
|
+
hexpand: boolean | null;
|
|
14
|
+
vexpand: boolean | null;
|
|
15
|
+
halign: string | null;
|
|
16
|
+
valign: string | null;
|
|
17
|
+
minChildren: number | null;
|
|
18
|
+
maxChildren: number | null;
|
|
19
|
+
homogeneous: boolean | null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function parseStylesCss(cssSource: string, from?: string): StyleRuleIr[];
|
|
23
|
+
|
|
24
|
+
export function formatStyleRuleExtensions(rule: StyleRuleIr): string;
|
|
25
|
+
|
|
26
|
+
export function emitStyleInstruction(rule: StyleRuleIr): string;
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import postcss from 'postcss';
|
|
2
|
+
|
|
3
|
+
const SIMPLE_CLASS_SELECTOR = /^\.([a-zA-Z0-9_-]+)$/;
|
|
4
|
+
|
|
5
|
+
function toPixelInt(raw) {
|
|
6
|
+
const parsed = Number.parseInt(String(raw).trim().replace(/px$/, ''), 10);
|
|
7
|
+
return Number.isFinite(parsed) && parsed >= 0 ? parsed : null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function mapAlignKeyword(raw) {
|
|
11
|
+
switch (String(raw).trim().toLowerCase()) {
|
|
12
|
+
case 'center':
|
|
13
|
+
return 'center';
|
|
14
|
+
case 'flex-start':
|
|
15
|
+
case 'start':
|
|
16
|
+
return 'start';
|
|
17
|
+
case 'flex-end':
|
|
18
|
+
case 'end':
|
|
19
|
+
return 'end';
|
|
20
|
+
case 'stretch':
|
|
21
|
+
case 'fill':
|
|
22
|
+
return 'fill';
|
|
23
|
+
case 'baseline':
|
|
24
|
+
return 'baseline';
|
|
25
|
+
default:
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function createEmptyRule(className) {
|
|
31
|
+
return {
|
|
32
|
+
className,
|
|
33
|
+
background: 'none',
|
|
34
|
+
color: 'none',
|
|
35
|
+
padding: '0',
|
|
36
|
+
spacing: '0',
|
|
37
|
+
borderRadius: null,
|
|
38
|
+
fontSize: null,
|
|
39
|
+
fontWeight: null,
|
|
40
|
+
minWidth: null,
|
|
41
|
+
minHeight: null,
|
|
42
|
+
orientation: null,
|
|
43
|
+
hexpand: null,
|
|
44
|
+
vexpand: null,
|
|
45
|
+
halign: null,
|
|
46
|
+
valign: null,
|
|
47
|
+
minChildren: null,
|
|
48
|
+
maxChildren: null,
|
|
49
|
+
homogeneous: null,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function applyGridTemplateColumns(rule, value) {
|
|
54
|
+
const match = String(value)
|
|
55
|
+
.trim()
|
|
56
|
+
.match(/repeat\(\s*(auto-fit|auto-fill|\d+)\s*,\s*(?:minmax\(\s*(\d+)px\s*,[^)]+\)|[^)]+)\)/i);
|
|
57
|
+
if (!match) return;
|
|
58
|
+
const track = match[1].toLowerCase();
|
|
59
|
+
if (track === 'auto-fit' || track === 'auto-fill') {
|
|
60
|
+
rule.minChildren = 1;
|
|
61
|
+
rule.maxChildren = 12;
|
|
62
|
+
} else {
|
|
63
|
+
const cols = Number.parseInt(track, 10);
|
|
64
|
+
if (!Number.isFinite(cols) || cols < 1) return;
|
|
65
|
+
rule.minChildren = cols;
|
|
66
|
+
rule.maxChildren = cols;
|
|
67
|
+
}
|
|
68
|
+
rule.homogeneous = true;
|
|
69
|
+
rule.hexpand = true;
|
|
70
|
+
if (match[2]) {
|
|
71
|
+
rule.minWidth ??= Number.parseInt(match[2], 10);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function applyLayoutDeclaration(rule, prop, value) {
|
|
76
|
+
const lower = String(value).trim().toLowerCase();
|
|
77
|
+
switch (prop) {
|
|
78
|
+
case 'flex-direction':
|
|
79
|
+
if (lower.startsWith('row')) rule.orientation = 'horizontal';
|
|
80
|
+
else if (lower.startsWith('column')) rule.orientation = 'vertical';
|
|
81
|
+
break;
|
|
82
|
+
case 'flex-wrap':
|
|
83
|
+
if (lower.startsWith('wrap')) {
|
|
84
|
+
rule.minChildren ??= 1;
|
|
85
|
+
rule.maxChildren ??= 12;
|
|
86
|
+
rule.homogeneous = true;
|
|
87
|
+
rule.hexpand = true;
|
|
88
|
+
}
|
|
89
|
+
break;
|
|
90
|
+
case 'flex':
|
|
91
|
+
case 'flex-grow': {
|
|
92
|
+
const grow = Number.parseFloat(lower);
|
|
93
|
+
if (Number.isFinite(grow) && grow >= 1) rule.hexpand = true;
|
|
94
|
+
if (prop === 'flex') {
|
|
95
|
+
const basisMatch = lower.match(/(\d+)px/);
|
|
96
|
+
if (basisMatch) rule.minWidth ??= Number.parseInt(basisMatch[1], 10);
|
|
97
|
+
}
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
case 'width':
|
|
101
|
+
if (lower === '100%') rule.hexpand = true;
|
|
102
|
+
else {
|
|
103
|
+
const px = toPixelInt(value);
|
|
104
|
+
if (px !== null) rule.minWidth ??= px;
|
|
105
|
+
}
|
|
106
|
+
break;
|
|
107
|
+
case 'height':
|
|
108
|
+
if (lower === '100%') rule.vexpand = true;
|
|
109
|
+
else {
|
|
110
|
+
const px = toPixelInt(value);
|
|
111
|
+
if (px !== null) rule.minHeight ??= px;
|
|
112
|
+
}
|
|
113
|
+
break;
|
|
114
|
+
case 'grid-template-columns':
|
|
115
|
+
applyGridTemplateColumns(rule, value);
|
|
116
|
+
break;
|
|
117
|
+
case 'justify-self': {
|
|
118
|
+
const aligned = mapAlignKeyword(value);
|
|
119
|
+
if (aligned) rule.halign = aligned;
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
case 'align-self': {
|
|
123
|
+
const aligned = mapAlignKeyword(value);
|
|
124
|
+
if (aligned) rule.valign = aligned;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
case 'justify-content': {
|
|
128
|
+
const aligned = mapAlignKeyword(value);
|
|
129
|
+
if (!rule.hexpand && aligned) rule.halign ??= aligned;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
case 'align-items': {
|
|
133
|
+
const aligned = mapAlignKeyword(value);
|
|
134
|
+
if (!rule.vexpand && aligned) rule.valign ??= aligned;
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
default:
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function applyDeclaration(rule, prop, value) {
|
|
143
|
+
switch (prop) {
|
|
144
|
+
case 'background':
|
|
145
|
+
case 'background-color':
|
|
146
|
+
rule.background = value;
|
|
147
|
+
break;
|
|
148
|
+
case 'color':
|
|
149
|
+
rule.color = value;
|
|
150
|
+
break;
|
|
151
|
+
case 'padding': {
|
|
152
|
+
const px = toPixelInt(value);
|
|
153
|
+
if (px !== null) rule.padding = String(px);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
case 'gap':
|
|
157
|
+
case 'spacing': {
|
|
158
|
+
const px = toPixelInt(value);
|
|
159
|
+
if (px !== null) rule.spacing = String(px);
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
case 'row-gap':
|
|
163
|
+
case 'column-gap': {
|
|
164
|
+
if (rule.spacing === '0') {
|
|
165
|
+
const px = toPixelInt(value);
|
|
166
|
+
if (px !== null) rule.spacing = String(px);
|
|
167
|
+
}
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
case 'border-radius':
|
|
171
|
+
rule.borderRadius = toPixelInt(value);
|
|
172
|
+
break;
|
|
173
|
+
case 'font-size':
|
|
174
|
+
rule.fontSize = toPixelInt(value);
|
|
175
|
+
break;
|
|
176
|
+
case 'font-weight': {
|
|
177
|
+
const keyword = { normal: 400, bold: 700 }[value];
|
|
178
|
+
rule.fontWeight = keyword ?? toPixelInt(value);
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
case 'min-width':
|
|
182
|
+
rule.minWidth = toPixelInt(value);
|
|
183
|
+
break;
|
|
184
|
+
case 'min-height':
|
|
185
|
+
rule.minHeight = toPixelInt(value);
|
|
186
|
+
break;
|
|
187
|
+
default:
|
|
188
|
+
applyLayoutDeclaration(rule, prop, value);
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Parses component CSS with the `postcss` AST into the native Style IR.
|
|
195
|
+
* Only simple class selectors (`.name`, including selector lists) are mapped;
|
|
196
|
+
* pseudo-classes, combinators, and at-rules are skipped because the native
|
|
197
|
+
* backends have no equivalent for them yet.
|
|
198
|
+
*/
|
|
199
|
+
export function parseStylesCss(cssSource, from = 'app.css') {
|
|
200
|
+
const root = postcss.parse(cssSource, { from });
|
|
201
|
+
const rulesByClass = new Map();
|
|
202
|
+
|
|
203
|
+
root.walkRules((cssRule) => {
|
|
204
|
+
if (cssRule.parent?.type === 'atrule') return;
|
|
205
|
+
|
|
206
|
+
for (const selector of cssRule.selectors) {
|
|
207
|
+
const match = selector.trim().match(SIMPLE_CLASS_SELECTOR);
|
|
208
|
+
if (!match) continue;
|
|
209
|
+
|
|
210
|
+
const className = match[1];
|
|
211
|
+
const rule = rulesByClass.get(className) ?? createEmptyRule(className);
|
|
212
|
+
cssRule.walkDecls((decl) => applyDeclaration(rule, decl.prop, decl.value.trim()));
|
|
213
|
+
rulesByClass.set(className, rule);
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
return [...rulesByClass.values()];
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const STYLE_EXTENSION_FIELDS = [
|
|
221
|
+
['borderRadius', 'radius'],
|
|
222
|
+
['fontSize', 'font-size'],
|
|
223
|
+
['fontWeight', 'font-weight'],
|
|
224
|
+
['minWidth', 'min-width'],
|
|
225
|
+
['minHeight', 'min-height'],
|
|
226
|
+
['orientation', 'orientation'],
|
|
227
|
+
['hexpand', 'hexpand'],
|
|
228
|
+
['vexpand', 'vexpand'],
|
|
229
|
+
['halign', 'halign'],
|
|
230
|
+
['valign', 'valign'],
|
|
231
|
+
['minChildren', 'min-children'],
|
|
232
|
+
['maxChildren', 'max-children'],
|
|
233
|
+
['homogeneous', 'homogeneous'],
|
|
234
|
+
];
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Formats optional Style IR typography/layout extensions as space-separated `key=value` tokens.
|
|
238
|
+
*/
|
|
239
|
+
export function formatStyleRuleExtensions(rule) {
|
|
240
|
+
const extensions = [];
|
|
241
|
+
for (const [prop, key] of STYLE_EXTENSION_FIELDS) {
|
|
242
|
+
const value = rule?.[prop];
|
|
243
|
+
if (value !== null && value !== undefined) {
|
|
244
|
+
extensions.push(`${key}=${value}`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return extensions.join(' ');
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Serializes a Style IR rule into a `.ngb` `style` instruction, appending optional
|
|
252
|
+
* `key=value` extensions only when declared (keeps legacy bundles byte-identical).
|
|
253
|
+
*/
|
|
254
|
+
export function emitStyleInstruction(rule) {
|
|
255
|
+
const base = `style ${rule.className} ${rule.background} ${rule.color} ${rule.padding} ${rule.spacing}`;
|
|
256
|
+
const extras = formatStyleRuleExtensions(rule);
|
|
257
|
+
return extras.length > 0 ? `${base} ${extras}` : base;
|
|
258
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export interface ReactiveBindingIr {
|
|
2
|
+
signalName: string;
|
|
3
|
+
template: string;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface ReactiveAttrBindingIr {
|
|
7
|
+
attrKey: string;
|
|
8
|
+
signalName: string;
|
|
9
|
+
template: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface VisibilityBindingIr {
|
|
13
|
+
signalName: string;
|
|
14
|
+
op: string;
|
|
15
|
+
rhs: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface TemplateAstNode {
|
|
19
|
+
id: string;
|
|
20
|
+
tag: string;
|
|
21
|
+
isComment?: boolean;
|
|
22
|
+
text: string | null;
|
|
23
|
+
title: string | null;
|
|
24
|
+
classes: string[];
|
|
25
|
+
attributes: Record<string, string>;
|
|
26
|
+
reactiveBinding: ReactiveBindingIr | null;
|
|
27
|
+
reactiveAttrs?: ReactiveAttrBindingIr[];
|
|
28
|
+
visibilityBinding?: VisibilityBindingIr | null;
|
|
29
|
+
events: Record<string, string>;
|
|
30
|
+
children: TemplateAstNode[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ComponentDefinitionIr {
|
|
34
|
+
templateHtml: string;
|
|
35
|
+
templateUrl?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface TemplateIdState {
|
|
39
|
+
next: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function applyPipeTransform(value: unknown, pipeName: string, args?: unknown[]): unknown;
|
|
43
|
+
|
|
44
|
+
export function parseTemplateHtml(
|
|
45
|
+
html: string,
|
|
46
|
+
state: Record<string, unknown>,
|
|
47
|
+
numericSignals: Record<string, number>,
|
|
48
|
+
templateUrl?: string,
|
|
49
|
+
componentRegistry?: Record<string, ComponentDefinitionIr>,
|
|
50
|
+
projectedChildren?: TemplateAstNode[] | null,
|
|
51
|
+
idState?: TemplateIdState,
|
|
52
|
+
signalAliases?: Record<string, string>,
|
|
53
|
+
): TemplateAstNode[];
|