@ng-desktop/builders 0.3.0 → 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/bin/ng-desktop.mjs +9 -0
- package/builders.json +7 -2
- package/fesm2022/application-builder.d.mts +62 -0
- package/fesm2022/application-builder.mjs +58 -0
- package/fesm2022/ast-metadata-extractor.mjs +51 -11
- package/fesm2022/build-bundle.d.mts +9 -0
- package/fesm2022/build-bundle.mjs +172 -25
- package/fesm2022/builder.d.ts +0 -1
- package/fesm2022/builder.js +0 -1
- package/fesm2022/dev-server-builder.d.mts +26 -0
- package/fesm2022/dev-server-builder.mjs +24 -0
- package/fesm2022/hmr-client.d.mts +5 -0
- package/fesm2022/hmr-client.mjs +7 -0
- package/fesm2022/index.d.ts +6 -4
- package/fesm2022/index.js +5 -3
- package/fesm2022/permission-analyzer.mjs +20 -0
- package/fesm2022/server-compiler.d.mts +29 -0
- package/fesm2022/server-compiler.mjs +236 -0
- package/fesm2022/style-compiler.d.mts +12 -0
- package/fesm2022/style-compiler.mjs +166 -5
- package/fesm2022/template-compiler.mjs +83 -1
- package/fesm2022/workspace-resolver.mjs +1 -1
- package/package.json +8 -7
- package/schema/native-builder-schema.json +18 -0
- package/schematics/ng-add/index.mjs +2 -2
- package/fesm2022/builder.d.ts.map +0 -1
- package/fesm2022/builder.js.map +0 -1
- package/fesm2022/index.d.ts.map +0 -1
- package/fesm2022/index.js.map +0 -1
package/fesm2022/index.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
export { extractComponentMetadata, type ComponentDecoratorMeta, type ExtractedComponentMetadata, type MethodMutationMeta, } from './ast-metadata-extractor.mjs';
|
|
2
|
-
export {
|
|
2
|
+
export { ARCHITECT_BUILDER_SYMBOL, ARCHITECT_VERSION_SYMBOL, applicationBuilder, createArchitectBuilder, runNativeApplicationBuilder, type ArchitectBuilderContextLike, type ArchitectBuilderObject, type NativeApplicationBuilderOptions, type NativeApplicationBuilderOutput, } from './application-builder.mjs';
|
|
3
|
+
export { devServerBuilder, runNativeDevServerBuilder, type NativeDevServerBuilderOptions, type NativeDevServerBuilderOutput, } from './dev-server-builder.mjs';
|
|
4
|
+
export { compileBundle, emitNgbBundle, watchBundle, type CompileBundleOptions, type CompileBundleResult, type HmrDeltaSummary, } from './build-bundle.mjs';
|
|
3
5
|
export { analyzeProjectPermissions, analyzeSourcePermissions, type NativeCapabilitiesObject, type PermissionAnalysisManifest, type PermissionEvidence, type PermissionPolicyMode, type PermissionsPolicyConfig, } from './permission-analyzer.mjs';
|
|
4
6
|
export { resolveWorkspaceProject, type ResolvedWorkspaceProject, type ResolveWorkspaceOptions, type WorkspaceKind, } from './workspace-resolver.mjs';
|
|
5
7
|
export { ENGINE_BINARY_PACKAGES, resolveEngineBinaryPath, type EngineBinaryPackageMeta, type EngineBinaryResolution, type EngineBinarySource, type ResolveEngineBinaryOptions, } from './resolve-engine-binary.mjs';
|
|
6
8
|
export { createNativeTargetConfig, executeNativeBuilder, resolveNativeBuilderConfig, type LinuxPackageFormat, type NativeArchKind, type NativeBuilderOptions, type NativeBuilderResult, type NativeCpuProfile, type NativeJsEngineKind, type NativeOsKind, type NativePackageFormat, type NativePermissionsMode, type NativePermissionsOptions, type NativePlatformTarget, type ResolvedNativeBuilderConfig, } from './builder';
|
|
7
|
-
export { pingHmr, pushHmrEval, pushHmrReload, sendHmrMessage, type HmrClientResult, } from './hmr-client.mjs';
|
|
9
|
+
export { pingHmr, pushHmrEval, pushHmrReload, pushHmrServerReload, sendHmrMessage, type HmrClientResult, } from './hmr-client.mjs';
|
|
8
10
|
export { buildMultiResolutionIcons } from './icon-compiler.mjs';
|
|
9
|
-
export { emitStyleInstruction, parseStylesCss, type StyleRuleIr } from './style-compiler.mjs';
|
|
11
|
+
export { emitStyleInstruction, formatStyleRuleExtensions, parseStylesCss, type StyleRuleIr, } from './style-compiler.mjs';
|
|
10
12
|
export { applyPipeTransform, parseTemplateHtml, type ComponentDefinitionIr, type ReactiveBindingIr, type TemplateAstNode, type TemplateIdState, } from './template-compiler.mjs';
|
|
11
|
-
|
|
13
|
+
export { compileServerBundle, discoverServerRoutes, type CompileServerBundleOptions, type DiscoveredServerRoute, } from './server-compiler.mjs';
|
package/fesm2022/index.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
export { extractComponentMetadata, } from './ast-metadata-extractor.mjs';
|
|
2
|
+
export { ARCHITECT_BUILDER_SYMBOL, ARCHITECT_VERSION_SYMBOL, applicationBuilder, createArchitectBuilder, runNativeApplicationBuilder, } from './application-builder.mjs';
|
|
3
|
+
export { devServerBuilder, runNativeDevServerBuilder, } from './dev-server-builder.mjs';
|
|
2
4
|
export { compileBundle, emitNgbBundle, watchBundle, } from './build-bundle.mjs';
|
|
3
5
|
export { analyzeProjectPermissions, analyzeSourcePermissions, } from './permission-analyzer.mjs';
|
|
4
6
|
export { resolveWorkspaceProject, } from './workspace-resolver.mjs';
|
|
5
7
|
export { ENGINE_BINARY_PACKAGES, resolveEngineBinaryPath, } from './resolve-engine-binary.mjs';
|
|
6
8
|
export { createNativeTargetConfig, executeNativeBuilder, resolveNativeBuilderConfig, } from './builder.js';
|
|
7
|
-
export { pingHmr, pushHmrEval, pushHmrReload, sendHmrMessage, } from './hmr-client.mjs';
|
|
9
|
+
export { pingHmr, pushHmrEval, pushHmrReload, pushHmrServerReload, sendHmrMessage, } from './hmr-client.mjs';
|
|
8
10
|
export { buildMultiResolutionIcons } from './icon-compiler.mjs';
|
|
9
|
-
export { emitStyleInstruction, parseStylesCss } from './style-compiler.mjs';
|
|
11
|
+
export { emitStyleInstruction, formatStyleRuleExtensions, parseStylesCss, } from './style-compiler.mjs';
|
|
10
12
|
export { applyPipeTransform, parseTemplateHtml, } from './template-compiler.mjs';
|
|
11
|
-
|
|
13
|
+
export { compileServerBundle, discoverServerRoutes, } from './server-compiler.mjs';
|
|
@@ -139,6 +139,13 @@ const CALL_NAME_CAPS = new Map([
|
|
|
139
139
|
['computeHash', 'compute-threads'],
|
|
140
140
|
]);
|
|
141
141
|
|
|
142
|
+
const SERVER_CONTEXT_CAPS = new Map([
|
|
143
|
+
['db', 'sqlite'],
|
|
144
|
+
['fs', 'filesystem:xdg'],
|
|
145
|
+
['compute', 'compute-threads'],
|
|
146
|
+
['http', 'network'],
|
|
147
|
+
]);
|
|
148
|
+
|
|
142
149
|
function visitAst(node, visitor) {
|
|
143
150
|
if (!node || typeof node !== 'object') return;
|
|
144
151
|
if (Array.isArray(node)) {
|
|
@@ -209,6 +216,19 @@ function inspectTypeScriptAst(filename, code, addEvidence) {
|
|
|
209
216
|
return;
|
|
210
217
|
}
|
|
211
218
|
|
|
219
|
+
if (node.type === 'MemberExpression' && node.property?.type === 'Identifier') {
|
|
220
|
+
const cap = SERVER_CONTEXT_CAPS.get(node.property.name);
|
|
221
|
+
const isContextObj =
|
|
222
|
+
(node.object?.type === 'Identifier' && node.object.name === 'context') ||
|
|
223
|
+
(node.object?.type === 'MemberExpression' &&
|
|
224
|
+
node.object.property?.type === 'Identifier' &&
|
|
225
|
+
node.object.property.name === 'context');
|
|
226
|
+
if (cap && isContextObj) {
|
|
227
|
+
addEvidence(cap, filename, `context.${node.property.name}`);
|
|
228
|
+
}
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
|
|
212
232
|
if (node.type === 'Literal' && typeof node.value === 'string') {
|
|
213
233
|
if (node.value === 'file-drop' || node.value === 'ng-drop-target') {
|
|
214
234
|
addEvidence('filesystem:drop', filename, `literal '${node.value}'`);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export interface DiscoveredServerRoute {
|
|
2
|
+
method: string;
|
|
3
|
+
pattern: string;
|
|
4
|
+
filePath: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface CompileServerBundleOptions {
|
|
8
|
+
workspaceRoot: string;
|
|
9
|
+
sourceRootAbs: string;
|
|
10
|
+
outDir: string;
|
|
11
|
+
projectName: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface EsbuildBuildOptions {
|
|
15
|
+
workspaceRoot: string;
|
|
16
|
+
resolveDir?: string;
|
|
17
|
+
entrySource: string;
|
|
18
|
+
loader?: 'ts' | 'js';
|
|
19
|
+
alias?: Record<string, string>;
|
|
20
|
+
define?: Record<string, string>;
|
|
21
|
+
minifySyntax?: boolean;
|
|
22
|
+
banner?: { js?: string };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function discoverServerRoutes(sourceRootAbs: string): DiscoveredServerRoute[];
|
|
26
|
+
|
|
27
|
+
export function runEsbuildBuild(options: EsbuildBuildOptions): string;
|
|
28
|
+
|
|
29
|
+
export function compileServerBundle(options: CompileServerBundleOptions): string | undefined;
|
|
@@ -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
|
+
}
|
|
@@ -7,8 +7,20 @@ export interface StyleRuleIr {
|
|
|
7
7
|
borderRadius: number | null;
|
|
8
8
|
fontSize: number | null;
|
|
9
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;
|
|
10
20
|
}
|
|
11
21
|
|
|
12
22
|
export function parseStylesCss(cssSource: string, from?: string): StyleRuleIr[];
|
|
13
23
|
|
|
24
|
+
export function formatStyleRuleExtensions(rule: StyleRuleIr): string;
|
|
25
|
+
|
|
14
26
|
export function emitStyleInstruction(rule: StyleRuleIr): string;
|
|
@@ -7,6 +7,26 @@ function toPixelInt(raw) {
|
|
|
7
7
|
return Number.isFinite(parsed) && parsed >= 0 ? parsed : null;
|
|
8
8
|
}
|
|
9
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
|
+
|
|
10
30
|
function createEmptyRule(className) {
|
|
11
31
|
return {
|
|
12
32
|
className,
|
|
@@ -17,9 +37,108 @@ function createEmptyRule(className) {
|
|
|
17
37
|
borderRadius: null,
|
|
18
38
|
fontSize: null,
|
|
19
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,
|
|
20
50
|
};
|
|
21
51
|
}
|
|
22
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
|
+
|
|
23
142
|
function applyDeclaration(rule, prop, value) {
|
|
24
143
|
switch (prop) {
|
|
25
144
|
case 'background':
|
|
@@ -40,6 +159,14 @@ function applyDeclaration(rule, prop, value) {
|
|
|
40
159
|
if (px !== null) rule.spacing = String(px);
|
|
41
160
|
break;
|
|
42
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
|
+
}
|
|
43
170
|
case 'border-radius':
|
|
44
171
|
rule.borderRadius = toPixelInt(value);
|
|
45
172
|
break;
|
|
@@ -51,7 +178,14 @@ function applyDeclaration(rule, prop, value) {
|
|
|
51
178
|
rule.fontWeight = keyword ?? toPixelInt(value);
|
|
52
179
|
break;
|
|
53
180
|
}
|
|
181
|
+
case 'min-width':
|
|
182
|
+
rule.minWidth = toPixelInt(value);
|
|
183
|
+
break;
|
|
184
|
+
case 'min-height':
|
|
185
|
+
rule.minHeight = toPixelInt(value);
|
|
186
|
+
break;
|
|
54
187
|
default:
|
|
188
|
+
applyLayoutDeclaration(rule, prop, value);
|
|
55
189
|
break;
|
|
56
190
|
}
|
|
57
191
|
}
|
|
@@ -83,15 +217,42 @@ export function parseStylesCss(cssSource, from = 'app.css') {
|
|
|
83
217
|
return [...rulesByClass.values()];
|
|
84
218
|
}
|
|
85
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
|
+
|
|
86
250
|
/**
|
|
87
251
|
* Serializes a Style IR rule into a `.ngb` `style` instruction, appending optional
|
|
88
252
|
* `key=value` extensions only when declared (keeps legacy bundles byte-identical).
|
|
89
253
|
*/
|
|
90
254
|
export function emitStyleInstruction(rule) {
|
|
91
255
|
const base = `style ${rule.className} ${rule.background} ${rule.color} ${rule.padding} ${rule.spacing}`;
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
if (rule.fontSize !== null) extensions.push(`font-size=${rule.fontSize}`);
|
|
95
|
-
if (rule.fontWeight !== null) extensions.push(`font-weight=${rule.fontWeight}`);
|
|
96
|
-
return extensions.length > 0 ? `${base} ${extensions.join(' ')}` : base;
|
|
256
|
+
const extras = formatStyleRuleExtensions(rule);
|
|
257
|
+
return extras.length > 0 ? `${base} ${extras}` : base;
|
|
97
258
|
}
|
|
@@ -414,6 +414,73 @@ function makeCommentNode(idState, prefix, text) {
|
|
|
414
414
|
};
|
|
415
415
|
}
|
|
416
416
|
|
|
417
|
+
const HTML_TO_NATIVE_TAG = {
|
|
418
|
+
div: 'ng-box',
|
|
419
|
+
section: 'ng-box',
|
|
420
|
+
main: 'ng-box',
|
|
421
|
+
article: 'ng-box',
|
|
422
|
+
aside: 'ng-box',
|
|
423
|
+
nav: 'ng-box',
|
|
424
|
+
header: 'ng-box',
|
|
425
|
+
footer: 'ng-box',
|
|
426
|
+
form: 'ng-box',
|
|
427
|
+
ul: 'ng-box',
|
|
428
|
+
ol: 'ng-box',
|
|
429
|
+
li: 'ng-box',
|
|
430
|
+
figure: 'ng-box',
|
|
431
|
+
figcaption: 'ng-box',
|
|
432
|
+
p: 'ng-label',
|
|
433
|
+
span: 'ng-label',
|
|
434
|
+
label: 'ng-label',
|
|
435
|
+
h1: 'ng-label',
|
|
436
|
+
h2: 'ng-label',
|
|
437
|
+
h3: 'ng-label',
|
|
438
|
+
h4: 'ng-label',
|
|
439
|
+
h5: 'ng-label',
|
|
440
|
+
h6: 'ng-label',
|
|
441
|
+
strong: 'ng-label',
|
|
442
|
+
em: 'ng-label',
|
|
443
|
+
small: 'ng-label',
|
|
444
|
+
code: 'ng-label',
|
|
445
|
+
pre: 'ng-label',
|
|
446
|
+
a: 'ng-label',
|
|
447
|
+
button: 'ng-button',
|
|
448
|
+
img: 'ng-image',
|
|
449
|
+
hr: 'ng-separator',
|
|
450
|
+
input: 'ng-entry',
|
|
451
|
+
textarea: 'ng-text-view',
|
|
452
|
+
select: 'ng-select',
|
|
453
|
+
progress: 'ng-progress-bar',
|
|
454
|
+
details: 'ng-expander',
|
|
455
|
+
dialog: 'ng-dialog',
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
function resolveNativeHtmlTag(tagName, attrs) {
|
|
459
|
+
const lower = String(tagName || '')
|
|
460
|
+
.trim()
|
|
461
|
+
.toLowerCase();
|
|
462
|
+
if (lower === 'input') {
|
|
463
|
+
const inputType = String(attrs?.type || '')
|
|
464
|
+
.trim()
|
|
465
|
+
.toLowerCase();
|
|
466
|
+
switch (inputType) {
|
|
467
|
+
case 'search':
|
|
468
|
+
return 'ng-search-entry';
|
|
469
|
+
case 'password':
|
|
470
|
+
return 'ng-password-entry';
|
|
471
|
+
case 'checkbox':
|
|
472
|
+
return 'ng-checkbox';
|
|
473
|
+
case 'range':
|
|
474
|
+
return 'ng-slider';
|
|
475
|
+
case 'number':
|
|
476
|
+
return 'ng-spin-button';
|
|
477
|
+
default:
|
|
478
|
+
return 'ng-entry';
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
return HTML_TO_NATIVE_TAG[lower] || tagName;
|
|
482
|
+
}
|
|
483
|
+
|
|
417
484
|
function isControlOrElementChild(child) {
|
|
418
485
|
return (
|
|
419
486
|
typeof child.name === 'string' ||
|
|
@@ -472,6 +539,21 @@ export function parseTemplateHtml(
|
|
|
472
539
|
continue;
|
|
473
540
|
}
|
|
474
541
|
|
|
542
|
+
if (tmplNode.name === 'ng-container') {
|
|
543
|
+
if (Array.isArray(tmplNode.children) && tmplNode.children.length > 0) {
|
|
544
|
+
out.push(...transformNodes(tmplNode.children, scopeState));
|
|
545
|
+
}
|
|
546
|
+
continue;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
if (tmplNode.name === 'router-outlet') {
|
|
550
|
+
out.push(makeCommentNode(idState, 'outlet', 'router-outlet'));
|
|
551
|
+
if (Array.isArray(tmplNode.children) && tmplNode.children.length > 0) {
|
|
552
|
+
out.push(...transformNodes(tmplNode.children, scopeState));
|
|
553
|
+
}
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
|
|
475
557
|
if (Array.isArray(tmplNode.branches)) {
|
|
476
558
|
const firstExpr = tmplNode.branches[0]?.expression?.source?.trim() || 'cond';
|
|
477
559
|
out.push(makeCommentNode(idState, 'anchor', `@if(${firstExpr})`));
|
|
@@ -790,7 +872,7 @@ export function parseTemplateHtml(
|
|
|
790
872
|
|
|
791
873
|
out.push({
|
|
792
874
|
id: attrs.id || `node_${idState.next++}`,
|
|
793
|
-
tag: tmplNode.name,
|
|
875
|
+
tag: resolveNativeHtmlTag(tmplNode.name, attrs),
|
|
794
876
|
title: attrs.title || null,
|
|
795
877
|
classes: attrs.class ? attrs.class.split(/\s+/).filter(Boolean) : [],
|
|
796
878
|
attributes: customAttrs,
|