@ngrs/build 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -2
- package/bin/ngrs-build.mjs +604 -0
- package/bin/ngrs.mjs +207 -0
- package/builders.json +34 -0
- package/package.json +88 -4
- package/schemas/application.schema.json +316 -0
- package/schemas/dev-server.schema.json +108 -0
- package/schemas/extract-i18n.schema.json +35 -0
- package/schemas/unit-test.schema.json +118 -0
- package/schematics/collection.json +10 -0
- package/schematics/ng-add/index.cjs +231 -0
- package/schematics/ng-add/schema.json +14 -0
- package/src/architect-compat.cjs +54 -0
- package/src/architect-compat.mjs +151 -0
- package/src/budgets.mjs +212 -0
- package/src/builders/application/index.cjs +13 -0
- package/src/builders/application/index.mjs +994 -0
- package/src/builders/dev-server/index.cjs +15 -0
- package/src/builders/dev-server/index.mjs +1088 -0
- package/src/builders/extract-i18n/index.cjs +13 -0
- package/src/builders/extract-i18n/index.mjs +60 -0
- package/src/builders/unit-test/index.cjs +13 -0
- package/src/builders/unit-test/index.mjs +346 -0
- package/src/index-html.mjs +126 -0
- package/src/index.cjs +29 -0
- package/src/index.d.ts +158 -0
- package/src/index.mjs +71 -0
- package/src/ng-add.mjs +92 -0
- package/src/proxy.mjs +136 -0
- package/src/stats-table.mjs +403 -0
- package/src/workspace.d.ts +213 -0
- package/src/workspace.mjs +850 -0
|
@@ -0,0 +1,1088 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import { readFile } from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { angular, formatDiagnostics } from '@ngrs/vite';
|
|
6
|
+
import { lookup as lookupMimeType } from 'mrmime';
|
|
7
|
+
import { createLogger as createViteLogger, createServer } from 'vite';
|
|
8
|
+
import { createBuilder, createConsoleLogger } from '../../architect-compat.mjs';
|
|
9
|
+
import { transformIndexHtmlContent } from '../../index-html.mjs';
|
|
10
|
+
import { loadProxyConfiguration } from '../../proxy.mjs';
|
|
11
|
+
import {
|
|
12
|
+
formatBuildMessage,
|
|
13
|
+
generateEsbuildBuildStatsTable,
|
|
14
|
+
getEntryPointName,
|
|
15
|
+
isInteractiveLogger,
|
|
16
|
+
resolveColorsOption,
|
|
17
|
+
withProgress,
|
|
18
|
+
} from '../../stats-table.mjs';
|
|
19
|
+
import {
|
|
20
|
+
loadWorkspace,
|
|
21
|
+
normalizeApplicationOptions,
|
|
22
|
+
normalizeDevServerOptions,
|
|
23
|
+
resolveAssets,
|
|
24
|
+
resolveProjectName,
|
|
25
|
+
resolveTargetOptions,
|
|
26
|
+
toPosixPath,
|
|
27
|
+
} from '../../workspace.mjs';
|
|
28
|
+
import {
|
|
29
|
+
compileGlobalStyleBundle,
|
|
30
|
+
resolvePolyfillImportSpecifier,
|
|
31
|
+
} from '../application/index.mjs';
|
|
32
|
+
|
|
33
|
+
const VIRTUAL_POLYFILLS_ID = '/@ngrs/polyfills.js';
|
|
34
|
+
const VIRTUAL_ENTRY_ID = '/@ngrs/entry.js';
|
|
35
|
+
const RESOLVED_POLYFILLS_ID = '\0ngrs:dev-polyfills.js';
|
|
36
|
+
const RESOLVED_ENTRY_ID = '\0ngrs:dev-entry.js';
|
|
37
|
+
|
|
38
|
+
const STATIC_IMPORT_RE =
|
|
39
|
+
/(?:^|[\n;])\s*(?:import|export)\s+(?:[^'"\n;]*?\s+from\s+)?['"](\.[^'"]+)['"]/g;
|
|
40
|
+
const DYNAMIC_IMPORT_RE = /\bimport\s*\(\s*['"](\.[^'"]+)['"]\s*\)/g;
|
|
41
|
+
const INLINE_MAP_TAIL_RE =
|
|
42
|
+
/\n?\/\/# sourceMappingURL=data:application\/json;(?:charset=utf-8;)?base64,[A-Za-z0-9+/=]+\s*$/;
|
|
43
|
+
|
|
44
|
+
function shortHash(text) {
|
|
45
|
+
return createHash('sha256').update(text).digest('hex').slice(0, 8).toUpperCase();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function pathnameWithoutBasePath(rawUrl, basePath) {
|
|
49
|
+
const parsed = new URL(rawUrl, 'http://localhost');
|
|
50
|
+
const pathname = decodeURIComponent(parsed.pathname);
|
|
51
|
+
if (basePath && basePath !== '/' && pathname.startsWith(basePath)) {
|
|
52
|
+
return '/' + pathname.slice(basePath.length).replace(/^\/+/, '');
|
|
53
|
+
}
|
|
54
|
+
return pathname;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function lookupMime(filePathOrUrl) {
|
|
58
|
+
const clean = filePathOrUrl.split('?')[0];
|
|
59
|
+
const ext = path.extname(clean).toLowerCase();
|
|
60
|
+
if (ext === '.ico') {
|
|
61
|
+
return 'image/x-icon';
|
|
62
|
+
}
|
|
63
|
+
return (ext && lookupMimeType(ext)) || undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function weakEtag(buf) {
|
|
67
|
+
const hash = createHash('sha1').update(buf).digest('hex').slice(0, 16);
|
|
68
|
+
return `W/"${buf.byteLength}-${hash}"`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function resolveLocalModulePath(fromFile, specifier, replacementMap) {
|
|
72
|
+
const base = path.resolve(path.dirname(fromFile), specifier);
|
|
73
|
+
const candidates = [base];
|
|
74
|
+
if (/\.[cm]?js$/.test(base)) {
|
|
75
|
+
candidates.push(base.replace(/\.[cm]?js$/, '.ts'));
|
|
76
|
+
candidates.push(base.replace(/\.[cm]?js$/, '.mts'));
|
|
77
|
+
} else if (!path.extname(base) || !/\.[cm]?[jt]sx?$/.test(base)) {
|
|
78
|
+
candidates.push(
|
|
79
|
+
`${base}.ts`,
|
|
80
|
+
`${base}.tsx`,
|
|
81
|
+
`${base}.mts`,
|
|
82
|
+
`${base}.js`,
|
|
83
|
+
`${base}.mjs`,
|
|
84
|
+
path.join(base, 'index.ts'),
|
|
85
|
+
path.join(base, 'index.js'),
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
for (const cand of candidates) {
|
|
90
|
+
const replaced = replacementMap.get(cand) ?? cand;
|
|
91
|
+
if (fs.existsSync(replaced) && fs.statSync(replaced).isFile()) {
|
|
92
|
+
return replaced;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Computes initial dev-mode bundle statistics (`Initial chunk files` + `Lazy chunk files`) in
|
|
100
|
+
* sub-millisecond time directly from `NapiProjectCompiler`'s warm emitted JS graph + global entries.
|
|
101
|
+
*/
|
|
102
|
+
async function collectDevServerInitialStats(compiler, appOptions) {
|
|
103
|
+
const replacementMap = new Map();
|
|
104
|
+
for (const rep of appOptions.fileReplacements || []) {
|
|
105
|
+
replacementMap.set(path.resolve(rep.replace), path.resolve(rep.with));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const moduleInfoCache = new Map();
|
|
109
|
+
function inspectModule(absFile) {
|
|
110
|
+
const resolved = replacementMap.get(absFile) ?? absFile;
|
|
111
|
+
if (moduleInfoCache.has(resolved)) {
|
|
112
|
+
return moduleInfoCache.get(resolved);
|
|
113
|
+
}
|
|
114
|
+
let rawCode = compiler?.getEmittedJs(resolved) ?? null;
|
|
115
|
+
if ((rawCode === null || rawCode === undefined) && /\.[cm]?js$/.test(resolved)) {
|
|
116
|
+
try {
|
|
117
|
+
rawCode = fs.readFileSync(resolved, 'utf8');
|
|
118
|
+
} catch {
|
|
119
|
+
rawCode = '';
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const cleanCode = (rawCode || '').replace(INLINE_MAP_TAIL_RE, '\n');
|
|
123
|
+
const size = Buffer.byteLength(cleanCode, 'utf8');
|
|
124
|
+
const staticImports = [];
|
|
125
|
+
const dynamicImports = [];
|
|
126
|
+
|
|
127
|
+
STATIC_IMPORT_RE.lastIndex = 0;
|
|
128
|
+
let m;
|
|
129
|
+
while ((m = STATIC_IMPORT_RE.exec(cleanCode)) !== null) {
|
|
130
|
+
const target = resolveLocalModulePath(resolved, m[1], replacementMap);
|
|
131
|
+
if (target) staticImports.push(target);
|
|
132
|
+
}
|
|
133
|
+
DYNAMIC_IMPORT_RE.lastIndex = 0;
|
|
134
|
+
while ((m = DYNAMIC_IMPORT_RE.exec(cleanCode)) !== null) {
|
|
135
|
+
const target = resolveLocalModulePath(resolved, m[1], replacementMap);
|
|
136
|
+
if (target) dynamicImports.push(target);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const info = { file: resolved, size, staticImports, dynamicImports };
|
|
140
|
+
moduleInfoCache.set(resolved, info);
|
|
141
|
+
return info;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const mainModules = new Set();
|
|
145
|
+
const discoveredLazyEntries = new Set();
|
|
146
|
+
|
|
147
|
+
if (fs.existsSync(appOptions.browserEntry)) {
|
|
148
|
+
const queue = [appOptions.browserEntry];
|
|
149
|
+
mainModules.add(appOptions.browserEntry);
|
|
150
|
+
for (let i = 0; i < queue.length; i++) {
|
|
151
|
+
const info = inspectModule(queue[i]);
|
|
152
|
+
for (const imp of info.staticImports) {
|
|
153
|
+
if (!mainModules.has(imp)) {
|
|
154
|
+
mainModules.add(imp);
|
|
155
|
+
queue.push(imp);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
for (const dyn of info.dynamicImports) {
|
|
159
|
+
if (!mainModules.has(dyn)) {
|
|
160
|
+
discoveredLazyEntries.add(dyn);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Walk lazy entries (including nested dynamic imports inside lazy routes)
|
|
167
|
+
const lazyQueue = [...discoveredLazyEntries];
|
|
168
|
+
const lazySubgraphs = new Map();
|
|
169
|
+
for (let idx = 0; idx < lazyQueue.length; idx++) {
|
|
170
|
+
const lazyEntry = lazyQueue[idx];
|
|
171
|
+
if (mainModules.has(lazyEntry) || lazySubgraphs.has(lazyEntry)) {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
const sub = new Set([lazyEntry]);
|
|
175
|
+
const bfs = [lazyEntry];
|
|
176
|
+
for (let j = 0; j < bfs.length; j++) {
|
|
177
|
+
const info = inspectModule(bfs[j]);
|
|
178
|
+
for (const imp of info.staticImports) {
|
|
179
|
+
if (!mainModules.has(imp) && !sub.has(imp)) {
|
|
180
|
+
sub.add(imp);
|
|
181
|
+
bfs.push(imp);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
for (const dyn of info.dynamicImports) {
|
|
185
|
+
if (!mainModules.has(dyn) && !discoveredLazyEntries.has(dyn)) {
|
|
186
|
+
discoveredLazyEntries.add(dyn);
|
|
187
|
+
lazyQueue.push(dyn);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
lazySubgraphs.set(lazyEntry, sub);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Identify modules shared across >=2 lazy entries (and not themselves a lazy entry)
|
|
195
|
+
const lazyConsumersByModule = new Map();
|
|
196
|
+
for (const [lazyEntry, sub] of lazySubgraphs.entries()) {
|
|
197
|
+
for (const mod of sub) {
|
|
198
|
+
let consumers = lazyConsumersByModule.get(mod);
|
|
199
|
+
if (!consumers) {
|
|
200
|
+
consumers = [];
|
|
201
|
+
lazyConsumersByModule.set(mod, consumers);
|
|
202
|
+
}
|
|
203
|
+
consumers.push(lazyEntry);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const browserStats = [];
|
|
208
|
+
let mainBytes = 0;
|
|
209
|
+
for (const mod of mainModules) {
|
|
210
|
+
mainBytes += inspectModule(mod).size;
|
|
211
|
+
}
|
|
212
|
+
if (mainBytes > 0) {
|
|
213
|
+
browserStats.push({
|
|
214
|
+
initial: true,
|
|
215
|
+
stats: ['main.js', 'main', mainBytes, '-'],
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (appOptions.polyfills.length > 0) {
|
|
220
|
+
let polyBytes = 0;
|
|
221
|
+
for (const spec of appOptions.polyfills) {
|
|
222
|
+
const resolved = resolvePolyfillImportSpecifier(
|
|
223
|
+
spec,
|
|
224
|
+
appOptions.workspaceRoot,
|
|
225
|
+
appOptions.projectRoot,
|
|
226
|
+
);
|
|
227
|
+
if (
|
|
228
|
+
!resolved.includes('/node_modules/') &&
|
|
229
|
+
fs.existsSync(resolved) &&
|
|
230
|
+
fs.statSync(resolved).isFile()
|
|
231
|
+
) {
|
|
232
|
+
polyBytes += inspectModule(resolved).size || fs.statSync(resolved).size;
|
|
233
|
+
} else {
|
|
234
|
+
polyBytes += Buffer.byteLength(`import ${JSON.stringify(spec)};\n`, 'utf8');
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
browserStats.push({
|
|
238
|
+
initial: true,
|
|
239
|
+
stats: ['polyfills.js', 'polyfills', polyBytes, '-'],
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Global styles (compiled in dev mode without minification)
|
|
244
|
+
const devAppOpts = {
|
|
245
|
+
...appOptions,
|
|
246
|
+
optimization: {
|
|
247
|
+
...appOptions.optimization,
|
|
248
|
+
scripts: false,
|
|
249
|
+
styles: { ...appOptions.optimization.styles, minify: false },
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
for (const styleBundle of appOptions.globalStyles) {
|
|
253
|
+
try {
|
|
254
|
+
const css = await compileGlobalStyleBundle(styleBundle, devAppOpts);
|
|
255
|
+
browserStats.push({
|
|
256
|
+
initial: styleBundle.initial,
|
|
257
|
+
stats: [`${styleBundle.name}.css`, styleBundle.name, Buffer.byteLength(css, 'utf8'), '-'],
|
|
258
|
+
});
|
|
259
|
+
} catch {
|
|
260
|
+
// Style error will surface on request/diagnostics
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
for (const scriptBundle of appOptions.globalScripts) {
|
|
265
|
+
let scriptBytes = 0;
|
|
266
|
+
for (const rawFile of scriptBundle.files) {
|
|
267
|
+
const abs = resolvePolyfillImportSpecifier(
|
|
268
|
+
rawFile,
|
|
269
|
+
appOptions.workspaceRoot,
|
|
270
|
+
appOptions.projectRoot,
|
|
271
|
+
);
|
|
272
|
+
try {
|
|
273
|
+
scriptBytes += fs.statSync(abs).size;
|
|
274
|
+
} catch {
|
|
275
|
+
// Ignore missing file here
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
browserStats.push({
|
|
279
|
+
initial: scriptBundle.initial,
|
|
280
|
+
stats: [`${scriptBundle.name}.js`, scriptBundle.name, scriptBytes, '-'],
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Shared lazy chunks (modules imported by >= 2 lazy routes)
|
|
285
|
+
const sharedGroups = new Map();
|
|
286
|
+
const sharedModuleSet = new Set();
|
|
287
|
+
for (const [mod, consumers] of lazyConsumersByModule.entries()) {
|
|
288
|
+
if (consumers.length >= 2 && !lazySubgraphs.has(mod)) {
|
|
289
|
+
sharedModuleSet.add(mod);
|
|
290
|
+
const key = consumers.slice().sort().join('\0');
|
|
291
|
+
let grp = sharedGroups.get(key);
|
|
292
|
+
if (!grp) {
|
|
293
|
+
grp = [];
|
|
294
|
+
sharedGroups.set(key, grp);
|
|
295
|
+
}
|
|
296
|
+
grp.push(mod);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
for (const [key, mods] of sharedGroups.entries()) {
|
|
301
|
+
const bytes = mods.reduce((sum, m) => sum + inspectModule(m).size, 0);
|
|
302
|
+
if (bytes > 0) {
|
|
303
|
+
browserStats.push({
|
|
304
|
+
initial: false,
|
|
305
|
+
stats: [`chunk-${shortHash(key)}.js`, '-', bytes, '-'],
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// Named lazy route chunks
|
|
311
|
+
for (const [lazyEntry, sub] of lazySubgraphs.entries()) {
|
|
312
|
+
let bytes = 0;
|
|
313
|
+
for (const mod of sub) {
|
|
314
|
+
if (!sharedModuleSet.has(mod)) {
|
|
315
|
+
bytes += inspectModule(mod).size;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
if (bytes > 0) {
|
|
319
|
+
const chunkFile = `chunk-${shortHash(lazyEntry)}.js`;
|
|
320
|
+
const chunkName = getEntryPointName(lazyEntry);
|
|
321
|
+
browserStats.push({
|
|
322
|
+
initial: false,
|
|
323
|
+
stats: [chunkFile, chunkName, bytes, '-'],
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return browserStats;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function createAngularViteCustomLogger(logger, verbose = false, clearScreen = false) {
|
|
332
|
+
const base = createViteLogger('info', { allowClearScreen: clearScreen });
|
|
333
|
+
const isInternalViteChatter = (msg) => {
|
|
334
|
+
const plain = String(msg);
|
|
335
|
+
return (
|
|
336
|
+
plain.includes('[vite]') &&
|
|
337
|
+
(plain.includes('hmr update') ||
|
|
338
|
+
plain.includes('page reload') ||
|
|
339
|
+
plain.includes('connected.') ||
|
|
340
|
+
plain.includes('optimizing dependencies') ||
|
|
341
|
+
plain.includes('optimized dependencies changed'))
|
|
342
|
+
);
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
return {
|
|
346
|
+
...base,
|
|
347
|
+
info(msg) {
|
|
348
|
+
if (!verbose && isInternalViteChatter(msg)) {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
logger.info(msg);
|
|
352
|
+
},
|
|
353
|
+
warn(msg) {
|
|
354
|
+
base.hasWarned = true;
|
|
355
|
+
logger.warn(msg);
|
|
356
|
+
},
|
|
357
|
+
warnOnce(msg) {
|
|
358
|
+
if (!verbose && isInternalViteChatter(msg)) {
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
base.hasWarned = true;
|
|
362
|
+
logger.warn(msg);
|
|
363
|
+
},
|
|
364
|
+
error(msg) {
|
|
365
|
+
logger.error(msg);
|
|
366
|
+
},
|
|
367
|
+
clearScreen(type) {
|
|
368
|
+
if (clearScreen) {
|
|
369
|
+
base.clearScreen(type);
|
|
370
|
+
}
|
|
371
|
+
},
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function createDevServerPlugin(serveOptions, appOptions, extensions = {}) {
|
|
376
|
+
const polyfillImports = appOptions.polyfills.map((spec) =>
|
|
377
|
+
resolvePolyfillImportSpecifier(spec, appOptions.workspaceRoot, appOptions.projectRoot),
|
|
378
|
+
);
|
|
379
|
+
const jitImport = appOptions.aot === false ? `import '@angular/compiler';\n` : '';
|
|
380
|
+
const polyfillCode =
|
|
381
|
+
jitImport +
|
|
382
|
+
polyfillImports.map((imp) => `import ${JSON.stringify(imp)};`).join('\n') +
|
|
383
|
+
'\n';
|
|
384
|
+
const entryCode =
|
|
385
|
+
polyfillCode +
|
|
386
|
+
`import ${JSON.stringify(toPosixPath(appOptions.browserEntry))};\n`;
|
|
387
|
+
|
|
388
|
+
/** @type {Map<string, string>} urlPathname -> sourceAbsPath */
|
|
389
|
+
let assetRouteMap = new Map();
|
|
390
|
+
|
|
391
|
+
async function refreshAssetRoutes() {
|
|
392
|
+
const entries = await resolveAssets(appOptions.assets, appOptions.workspaceRoot);
|
|
393
|
+
const map = new Map();
|
|
394
|
+
for (const item of entries) {
|
|
395
|
+
const route = '/' + toPosixPath(item.destination).replace(/^\/+/, '');
|
|
396
|
+
map.set(route, item.source);
|
|
397
|
+
}
|
|
398
|
+
assetRouteMap = map;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
return {
|
|
402
|
+
name: 'ngrs:dev-server-host',
|
|
403
|
+
enforce: 'pre',
|
|
404
|
+
|
|
405
|
+
async buildStart() {
|
|
406
|
+
await refreshAssetRoutes();
|
|
407
|
+
},
|
|
408
|
+
|
|
409
|
+
resolveId(source) {
|
|
410
|
+
if (source === VIRTUAL_POLYFILLS_ID) {
|
|
411
|
+
return RESOLVED_POLYFILLS_ID;
|
|
412
|
+
}
|
|
413
|
+
if (source === VIRTUAL_ENTRY_ID) {
|
|
414
|
+
return RESOLVED_ENTRY_ID;
|
|
415
|
+
}
|
|
416
|
+
return null;
|
|
417
|
+
},
|
|
418
|
+
|
|
419
|
+
load(id) {
|
|
420
|
+
if (id === RESOLVED_POLYFILLS_ID) {
|
|
421
|
+
return polyfillCode;
|
|
422
|
+
}
|
|
423
|
+
if (id === RESOLVED_ENTRY_ID) {
|
|
424
|
+
return entryCode;
|
|
425
|
+
}
|
|
426
|
+
return null;
|
|
427
|
+
},
|
|
428
|
+
|
|
429
|
+
configureServer(server) {
|
|
430
|
+
// 1. Custom HTTP response headers middleware (`serveOptions.headers`)
|
|
431
|
+
if (serveOptions.headers && Object.keys(serveOptions.headers).length > 0) {
|
|
432
|
+
server.middlewares.use((_req, res, next) => {
|
|
433
|
+
for (const [headerName, headerValue] of Object.entries(serveOptions.headers)) {
|
|
434
|
+
res.setHeader(headerName, headerValue);
|
|
435
|
+
}
|
|
436
|
+
next();
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// 2. Optional programmatic extension middlewares
|
|
441
|
+
if (Array.isArray(extensions.middleware)) {
|
|
442
|
+
for (const mw of extensions.middleware) {
|
|
443
|
+
server.middlewares.use(mw);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// 3. Global non-module scripts middleware (`/@ngrs/scripts/<bundleName>.js`)
|
|
448
|
+
server.middlewares.use(async (req, res, next) => {
|
|
449
|
+
if (!req.url || res.writableEnded) {
|
|
450
|
+
next();
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
const pathname = pathnameWithoutBasePath(req.url, server.config.base);
|
|
454
|
+
if (!pathname.startsWith('/@ngrs/scripts/')) {
|
|
455
|
+
next();
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
const bundleName = path.basename(pathname, '.js');
|
|
459
|
+
const bundle = appOptions.globalScripts.find((b) => b.name === bundleName);
|
|
460
|
+
if (!bundle) {
|
|
461
|
+
res.statusCode = 404;
|
|
462
|
+
res.end();
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
try {
|
|
466
|
+
const chunks = [];
|
|
467
|
+
for (const rawFile of bundle.files) {
|
|
468
|
+
const abs = resolvePolyfillImportSpecifier(
|
|
469
|
+
rawFile,
|
|
470
|
+
appOptions.workspaceRoot,
|
|
471
|
+
appOptions.projectRoot,
|
|
472
|
+
);
|
|
473
|
+
chunks.push(await readFile(abs, 'utf8'));
|
|
474
|
+
}
|
|
475
|
+
const body = chunks.join('\n;\n');
|
|
476
|
+
res.statusCode = 200;
|
|
477
|
+
res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
|
|
478
|
+
res.setHeader('Cache-Control', 'no-cache');
|
|
479
|
+
res.end(body);
|
|
480
|
+
} catch (err) {
|
|
481
|
+
next(err);
|
|
482
|
+
}
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
// 4. Angular workspace `assets` middleware (`"public"`, `"src/assets"`, `{ glob, input, output }`)
|
|
486
|
+
server.middlewares.use(async (req, res, next) => {
|
|
487
|
+
if (!req.url || res.writableEnded || (req.method !== 'GET' && req.method !== 'HEAD')) {
|
|
488
|
+
next();
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
const pathname = pathnameWithoutBasePath(req.url, server.config.base);
|
|
492
|
+
if (
|
|
493
|
+
pathname === '/' ||
|
|
494
|
+
pathname === '/index.html' ||
|
|
495
|
+
pathname.startsWith('/@') ||
|
|
496
|
+
pathname.startsWith('/node_modules/')
|
|
497
|
+
) {
|
|
498
|
+
next();
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
let assetSource = assetRouteMap.get(pathname);
|
|
503
|
+
if (!assetSource) {
|
|
504
|
+
await refreshAssetRoutes();
|
|
505
|
+
assetSource = assetRouteMap.get(pathname);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
if (!assetSource || !fs.existsSync(assetSource)) {
|
|
509
|
+
next();
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
try {
|
|
514
|
+
const buf = await readFile(assetSource);
|
|
515
|
+
const etag = weakEtag(buf);
|
|
516
|
+
if (req.headers['if-none-match'] === etag) {
|
|
517
|
+
res.statusCode = 304;
|
|
518
|
+
res.end();
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
const mime = lookupMime(assetSource);
|
|
522
|
+
if (mime) {
|
|
523
|
+
res.setHeader('Content-Type', mime);
|
|
524
|
+
}
|
|
525
|
+
res.setHeader('ETag', etag);
|
|
526
|
+
res.setHeader('Cache-Control', 'no-cache');
|
|
527
|
+
res.statusCode = 200;
|
|
528
|
+
res.end(req.method === 'HEAD' ? undefined : buf);
|
|
529
|
+
} catch (err) {
|
|
530
|
+
next(err);
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
// 5. Post-Vite SPA `index.html` transform & fallback middleware
|
|
535
|
+
return () => {
|
|
536
|
+
server.middlewares.use(async (req, res, next) => {
|
|
537
|
+
if (
|
|
538
|
+
!req.url ||
|
|
539
|
+
res.writableEnded ||
|
|
540
|
+
(req.method !== 'GET' && req.method !== 'HEAD')
|
|
541
|
+
) {
|
|
542
|
+
next();
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
const pathname = pathnameWithoutBasePath(req.url, server.config.base);
|
|
547
|
+
const isExplicitIndex = pathname === '/' || pathname === '/index.html';
|
|
548
|
+
if (!isExplicitIndex) {
|
|
549
|
+
if (lookupMime(pathname) || pathname.startsWith('/@')) {
|
|
550
|
+
next();
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
const accept = req.headers.accept || '';
|
|
554
|
+
if (
|
|
555
|
+
accept &&
|
|
556
|
+
!accept.includes('text/html') &&
|
|
557
|
+
!accept.includes('text/*') &&
|
|
558
|
+
!accept.includes('*/*')
|
|
559
|
+
) {
|
|
560
|
+
next();
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
if (!appOptions.indexHtmlOptions || !fs.existsSync(appOptions.indexHtmlOptions.input)) {
|
|
566
|
+
next();
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
try {
|
|
571
|
+
const rawHtml = await readFile(appOptions.indexHtmlOptions.input, 'utf8');
|
|
572
|
+
const basePrefix = (server.config.base || '/').replace(/\/+$/, '');
|
|
573
|
+
|
|
574
|
+
const styleLinks = [];
|
|
575
|
+
for (const styleBundle of appOptions.globalStyles) {
|
|
576
|
+
if (!styleBundle.initial) continue;
|
|
577
|
+
for (const rawStyle of styleBundle.files) {
|
|
578
|
+
const absStyle = resolvePolyfillImportSpecifier(
|
|
579
|
+
rawStyle,
|
|
580
|
+
appOptions.workspaceRoot,
|
|
581
|
+
appOptions.projectRoot,
|
|
582
|
+
);
|
|
583
|
+
styleLinks.push({
|
|
584
|
+
href: `${basePrefix}/@fs/${encodeURI(toPosixPath(absStyle).replace(/^\/+/, ''))}`,
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
const scriptTags = [];
|
|
590
|
+
for (const scriptBundle of appOptions.globalScripts) {
|
|
591
|
+
if (!scriptBundle.initial) continue;
|
|
592
|
+
scriptTags.push({
|
|
593
|
+
src: `${basePrefix}/@ngrs/scripts/${scriptBundle.name}.js`,
|
|
594
|
+
isModule: false,
|
|
595
|
+
defer: true,
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
scriptTags.push({
|
|
599
|
+
src: `${basePrefix}${VIRTUAL_ENTRY_ID}`,
|
|
600
|
+
isModule: true,
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
let html = transformIndexHtmlContent({
|
|
604
|
+
html: rawHtml,
|
|
605
|
+
baseHref: appOptions.baseHref ?? serveOptions.servePath,
|
|
606
|
+
deployUrl: appOptions.deployUrl,
|
|
607
|
+
crossOrigin: appOptions.crossOrigin,
|
|
608
|
+
sri: false,
|
|
609
|
+
styles: styleLinks,
|
|
610
|
+
scripts: scriptTags,
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
html = await server.transformIndexHtml(req.url, html, req.originalUrl);
|
|
614
|
+
if (typeof extensions.indexHtmlTransformer === 'function') {
|
|
615
|
+
html = await extensions.indexHtmlTransformer(html);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
res.statusCode = 200;
|
|
619
|
+
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
|
620
|
+
res.setHeader('Cache-Control', 'no-cache');
|
|
621
|
+
res.end(req.method === 'HEAD' ? undefined : html);
|
|
622
|
+
} catch (err) {
|
|
623
|
+
next(err);
|
|
624
|
+
}
|
|
625
|
+
});
|
|
626
|
+
};
|
|
627
|
+
},
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Starts a Vite 8 (Rolldown) development server configured 1:1 from `angular.json` `serve` + `build` options.
|
|
633
|
+
*
|
|
634
|
+
* @param {import('../../workspace.mjs').NormalizedDevServerOptions} serveOptions
|
|
635
|
+
* @param {import('../../workspace.mjs').NormalizedApplicationOptions} appOptions
|
|
636
|
+
* @param {object} [logger]
|
|
637
|
+
* @param {object} [extensions]
|
|
638
|
+
*/
|
|
639
|
+
export async function startDevServer(
|
|
640
|
+
serveOptions,
|
|
641
|
+
appOptions,
|
|
642
|
+
logger = createConsoleLogger(serveOptions.verbose),
|
|
643
|
+
extensions = {},
|
|
644
|
+
) {
|
|
645
|
+
const colors = resolveColorsOption(serveOptions.colors ?? appOptions.colors);
|
|
646
|
+
const progressEnabled = serveOptions.progress ?? appOptions.progress ?? true;
|
|
647
|
+
const clearScreen = Boolean(serveOptions.clearScreen ?? appOptions.clearScreen ?? false);
|
|
648
|
+
const startTime = performance.now();
|
|
649
|
+
|
|
650
|
+
let activeCompiler = null;
|
|
651
|
+
let initialDiagnostics = [];
|
|
652
|
+
let serverReady = false;
|
|
653
|
+
|
|
654
|
+
const logFormattedDiagnostics = (diags) => {
|
|
655
|
+
const { warnings, errors } = formatDiagnostics(diags, appOptions.workspaceRoot);
|
|
656
|
+
if (warnings.length > 0) {
|
|
657
|
+
logger.warn(warnings.map((w) => formatBuildMessage(w, 'warning', colors)).join('\n'));
|
|
658
|
+
}
|
|
659
|
+
if (errors.length > 0) {
|
|
660
|
+
logger.error(errors.map((e) => formatBuildMessage(e, 'error', colors)).join('\n'));
|
|
661
|
+
}
|
|
662
|
+
return { warnings, errors };
|
|
663
|
+
};
|
|
664
|
+
|
|
665
|
+
const { server, browserStats } = await withProgress(
|
|
666
|
+
'Building...',
|
|
667
|
+
progressEnabled,
|
|
668
|
+
colors,
|
|
669
|
+
logger,
|
|
670
|
+
async () => {
|
|
671
|
+
const proxy = await loadProxyConfiguration(
|
|
672
|
+
serveOptions.workspaceRoot,
|
|
673
|
+
serveOptions.proxyConfig,
|
|
674
|
+
);
|
|
675
|
+
|
|
676
|
+
let httpsConfig;
|
|
677
|
+
if (serveOptions.ssl) {
|
|
678
|
+
if (serveOptions.sslKey && serveOptions.sslCert) {
|
|
679
|
+
const [key, cert] = await Promise.all([
|
|
680
|
+
readFile(serveOptions.sslKey),
|
|
681
|
+
readFile(serveOptions.sslCert),
|
|
682
|
+
]);
|
|
683
|
+
httpsConfig = { key, cert };
|
|
684
|
+
} else {
|
|
685
|
+
httpsConfig = {};
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
const prebundleDisabled = serveOptions.prebundle === false;
|
|
690
|
+
const prebundleExclude = [
|
|
691
|
+
...appOptions.externalDependencies,
|
|
692
|
+
...(typeof serveOptions.prebundle === 'object' &&
|
|
693
|
+
Array.isArray(serveOptions.prebundle.exclude)
|
|
694
|
+
? serveOptions.prebundle.exclude
|
|
695
|
+
: []),
|
|
696
|
+
];
|
|
697
|
+
|
|
698
|
+
const defineMap = {
|
|
699
|
+
...(appOptions.define || {}),
|
|
700
|
+
...(serveOptions.define || {}),
|
|
701
|
+
};
|
|
702
|
+
|
|
703
|
+
const baseServePath = serveOptions.servePath || appOptions.baseHref || '/';
|
|
704
|
+
const normalizedBase = baseServePath.startsWith('/') ? baseServePath : `/${baseServePath}`;
|
|
705
|
+
|
|
706
|
+
const allowDirs = new Set([
|
|
707
|
+
appOptions.workspaceRoot,
|
|
708
|
+
appOptions.projectRoot,
|
|
709
|
+
process.cwd(),
|
|
710
|
+
]);
|
|
711
|
+
for (const d of [...allowDirs]) {
|
|
712
|
+
try {
|
|
713
|
+
allowDirs.add(fs.realpathSync(d));
|
|
714
|
+
} catch {
|
|
715
|
+
// Ignore missing realpath
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
const customLogger = createAngularViteCustomLogger(
|
|
720
|
+
logger,
|
|
721
|
+
Boolean(serveOptions.verbose),
|
|
722
|
+
clearScreen,
|
|
723
|
+
);
|
|
724
|
+
|
|
725
|
+
const viteServer = await createServer({
|
|
726
|
+
configFile: false,
|
|
727
|
+
envDir: false,
|
|
728
|
+
root: appOptions.workspaceRoot,
|
|
729
|
+
base: normalizedBase.endsWith('/') ? normalizedBase : `${normalizedBase}/`,
|
|
730
|
+
mode: 'development',
|
|
731
|
+
appType: 'custom',
|
|
732
|
+
publicDir: false,
|
|
733
|
+
customLogger,
|
|
734
|
+
clearScreen,
|
|
735
|
+
cacheDir: path.join(
|
|
736
|
+
appOptions.workspaceRoot,
|
|
737
|
+
'.angular',
|
|
738
|
+
'cache',
|
|
739
|
+
'ngrs-vite',
|
|
740
|
+
serveOptions.projectName,
|
|
741
|
+
),
|
|
742
|
+
define: Object.keys(defineMap).length > 0 ? defineMap : undefined,
|
|
743
|
+
resolve: {
|
|
744
|
+
preserveSymlinks: appOptions.preserveSymlinks,
|
|
745
|
+
alias: appOptions.fileReplacements.map((r) => ({
|
|
746
|
+
find: r.replace,
|
|
747
|
+
replacement: r.with,
|
|
748
|
+
})),
|
|
749
|
+
...(appOptions.conditions ? { conditions: appOptions.conditions } : {}),
|
|
750
|
+
},
|
|
751
|
+
css: {
|
|
752
|
+
devSourcemap: Boolean(appOptions.sourceMap.styles ?? true),
|
|
753
|
+
preprocessorOptions: {
|
|
754
|
+
scss: { loadPaths: appOptions.stylePreprocessorOptions.includePaths },
|
|
755
|
+
sass: { loadPaths: appOptions.stylePreprocessorOptions.includePaths },
|
|
756
|
+
},
|
|
757
|
+
},
|
|
758
|
+
optimizeDeps: {
|
|
759
|
+
noDiscovery: prebundleDisabled,
|
|
760
|
+
entries: prebundleDisabled ? undefined : [toPosixPath(appOptions.browserEntry)],
|
|
761
|
+
include: prebundleDisabled
|
|
762
|
+
? undefined
|
|
763
|
+
: appOptions.polyfills.filter((p) => !p.startsWith('.') && !path.isAbsolute(p)),
|
|
764
|
+
exclude: prebundleExclude.length > 0 ? prebundleExclude : undefined,
|
|
765
|
+
},
|
|
766
|
+
plugins: [
|
|
767
|
+
createDevServerPlugin(serveOptions, appOptions, extensions),
|
|
768
|
+
...angular({
|
|
769
|
+
tsconfig: appOptions.tsconfigPath,
|
|
770
|
+
workspaceRoot: appOptions.workspaceRoot,
|
|
771
|
+
fileReplacements: appOptions.fileReplacements,
|
|
772
|
+
hmr: serveOptions.hmr,
|
|
773
|
+
sourceMap: Boolean(appOptions.sourceMap.scripts ?? true),
|
|
774
|
+
inlineStyleLanguage: appOptions.inlineStyleLanguage,
|
|
775
|
+
stylePreprocessorOptions: appOptions.stylePreprocessorOptions,
|
|
776
|
+
jit: !appOptions.aot,
|
|
777
|
+
suppressViteDiagnosticsLog: true,
|
|
778
|
+
onCompilerReady(compiler) {
|
|
779
|
+
activeCompiler = compiler;
|
|
780
|
+
},
|
|
781
|
+
onDiagnostics(diags) {
|
|
782
|
+
if (!serverReady) {
|
|
783
|
+
initialDiagnostics = diags || [];
|
|
784
|
+
} else {
|
|
785
|
+
logFormattedDiagnostics(diags || []);
|
|
786
|
+
}
|
|
787
|
+
},
|
|
788
|
+
onDevServerUpdate(event) {
|
|
789
|
+
if (!serverReady) return;
|
|
790
|
+
if (event.kind === 'component-update') {
|
|
791
|
+
logger.info('Component update sent to client(s).');
|
|
792
|
+
} else if (event.kind === 'style-update') {
|
|
793
|
+
logger.info('Stylesheet update sent to client(s).');
|
|
794
|
+
} else if (event.kind === 'reload') {
|
|
795
|
+
const rebuildSec = ((event.durationMs ?? 0) / 1000).toFixed(3);
|
|
796
|
+
logger.info(
|
|
797
|
+
`Application bundle generation complete. [${rebuildSec} seconds] - ${new Date().toISOString()}\n`,
|
|
798
|
+
);
|
|
799
|
+
if (serveOptions.liveReload !== false) {
|
|
800
|
+
logger.info('Page reload sent to client(s).');
|
|
801
|
+
}
|
|
802
|
+
} else if (event.kind === 'error' && event.message) {
|
|
803
|
+
logger.error(formatBuildMessage(event.message, 'error', colors));
|
|
804
|
+
}
|
|
805
|
+
},
|
|
806
|
+
}),
|
|
807
|
+
...(extensions?.plugins || []),
|
|
808
|
+
],
|
|
809
|
+
server: {
|
|
810
|
+
port: serveOptions.port,
|
|
811
|
+
strictPort: serveOptions.strictPort,
|
|
812
|
+
host: serveOptions.host,
|
|
813
|
+
open: serveOptions.open,
|
|
814
|
+
https: httpsConfig,
|
|
815
|
+
proxy,
|
|
816
|
+
headers: serveOptions.headers,
|
|
817
|
+
allowedHosts:
|
|
818
|
+
Array.isArray(serveOptions.allowedHosts) && serveOptions.allowedHosts.length === 0
|
|
819
|
+
? undefined
|
|
820
|
+
: serveOptions.allowedHosts,
|
|
821
|
+
hmr: serveOptions.hmr ? true : false,
|
|
822
|
+
ws: !serveOptions.hmr && !serveOptions.liveReload ? false : undefined,
|
|
823
|
+
watch:
|
|
824
|
+
serveOptions.watch === false
|
|
825
|
+
? null
|
|
826
|
+
: serveOptions.poll
|
|
827
|
+
? { usePolling: true, interval: serveOptions.poll }
|
|
828
|
+
: undefined,
|
|
829
|
+
fs: {
|
|
830
|
+
allow: [...allowDirs],
|
|
831
|
+
},
|
|
832
|
+
},
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
await viteServer.listen();
|
|
836
|
+
const stats = await collectDevServerInitialStats(activeCompiler, appOptions);
|
|
837
|
+
return { server: viteServer, browserStats: stats };
|
|
838
|
+
},
|
|
839
|
+
);
|
|
840
|
+
|
|
841
|
+
serverReady = true;
|
|
842
|
+
|
|
843
|
+
// Browser client logging & HMR invalidation handlers (1:1 with `@angular/build` `dev-server/vite/index.ts`)
|
|
844
|
+
server.hot?.on('angular:log', (data) => {
|
|
845
|
+
if (typeof data?.text !== 'string') {
|
|
846
|
+
logger.warn('Development server client sent invalid internal log event.');
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
switch (data.kind) {
|
|
850
|
+
case 'error':
|
|
851
|
+
logger.error(`[CLIENT ERROR]: ${data.text}`);
|
|
852
|
+
break;
|
|
853
|
+
case 'warning':
|
|
854
|
+
logger.warn(`[CLIENT WARNING]: ${data.text}`);
|
|
855
|
+
break;
|
|
856
|
+
default:
|
|
857
|
+
logger.info(`[CLIENT INFO]: ${data.text}`);
|
|
858
|
+
break;
|
|
859
|
+
}
|
|
860
|
+
});
|
|
861
|
+
|
|
862
|
+
server.hot?.on('angular:invalidate', (data) => {
|
|
863
|
+
if (data?.error) {
|
|
864
|
+
logger.error(
|
|
865
|
+
`Component update failed${data.message ? `: ${data.message}` : '.'}` +
|
|
866
|
+
'\nPlease consider reporting the error at https://github.com/angular/angular-cli/issues',
|
|
867
|
+
);
|
|
868
|
+
} else {
|
|
869
|
+
logger.warn(`Component update unsupported${data?.message ? `: ${data.message}` : '.'}`);
|
|
870
|
+
}
|
|
871
|
+
server.ws?.send({ type: 'full-reload', path: '*' });
|
|
872
|
+
logger.info('Page reload sent to client(s).');
|
|
873
|
+
});
|
|
874
|
+
|
|
875
|
+
const durationSec = ((performance.now() - startTime) / 1000).toFixed(3);
|
|
876
|
+
const { warnings: initWarnings, errors: initErrors } = formatDiagnostics(
|
|
877
|
+
initialDiagnostics,
|
|
878
|
+
appOptions.workspaceRoot,
|
|
879
|
+
);
|
|
880
|
+
const hasError = initErrors.length > 0;
|
|
881
|
+
|
|
882
|
+
const tableText = generateEsbuildBuildStatsTable(
|
|
883
|
+
[browserStats, []],
|
|
884
|
+
colors,
|
|
885
|
+
true,
|
|
886
|
+
false,
|
|
887
|
+
undefined,
|
|
888
|
+
serveOptions.verbose,
|
|
889
|
+
);
|
|
890
|
+
|
|
891
|
+
const logs = [];
|
|
892
|
+
if (tableText) {
|
|
893
|
+
logs.push(tableText + '\n');
|
|
894
|
+
}
|
|
895
|
+
logs.push(
|
|
896
|
+
`Application bundle generation ${hasError ? 'failed' : 'complete'}. [${durationSec} seconds] - ${new Date().toISOString()}\n`,
|
|
897
|
+
);
|
|
898
|
+
logger.info(logs.join('\n'));
|
|
899
|
+
|
|
900
|
+
if (initWarnings.length > 0) {
|
|
901
|
+
logger.warn(initWarnings.map((w) => formatBuildMessage(w, 'warning', colors)).join('\n'));
|
|
902
|
+
}
|
|
903
|
+
if (initErrors.length > 0) {
|
|
904
|
+
logger.error(initErrors.map((e) => formatBuildMessage(e, 'error', colors)).join('\n'));
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
if (serveOptions.watch !== false && progressEnabled) {
|
|
908
|
+
logger.info('Watch mode enabled. Watching for file changes...');
|
|
909
|
+
}
|
|
910
|
+
logger.info(
|
|
911
|
+
'NOTE: Raw file sizes do not reflect development server per-request transformations.',
|
|
912
|
+
);
|
|
913
|
+
|
|
914
|
+
const baseServePath = serveOptions.servePath || appOptions.baseHref || '/';
|
|
915
|
+
const normalizedBase = baseServePath.startsWith('/') ? baseServePath : `/${baseServePath}`;
|
|
916
|
+
const addressInfo = server.httpServer?.address();
|
|
917
|
+
const actualPort =
|
|
918
|
+
typeof addressInfo === 'object' && addressInfo !== null
|
|
919
|
+
? addressInfo.port
|
|
920
|
+
: serveOptions.port;
|
|
921
|
+
const protocol = serveOptions.ssl ? 'https' : 'http';
|
|
922
|
+
const displayHost =
|
|
923
|
+
serveOptions.host === '0.0.0.0' || serveOptions.host === '::'
|
|
924
|
+
? 'localhost'
|
|
925
|
+
: serveOptions.host;
|
|
926
|
+
const resolvedLocal = server.resolvedUrls?.local?.[0];
|
|
927
|
+
const baseUrl =
|
|
928
|
+
resolvedLocal ||
|
|
929
|
+
`${protocol}://${displayHost}:${actualPort}${
|
|
930
|
+
normalizedBase.endsWith('/') ? normalizedBase : `${normalizedBase}/`
|
|
931
|
+
}`;
|
|
932
|
+
|
|
933
|
+
server.printUrls();
|
|
934
|
+
|
|
935
|
+
if (
|
|
936
|
+
process.stdin.isTTY &&
|
|
937
|
+
process.stdout.isTTY &&
|
|
938
|
+
isInteractiveLogger(logger) &&
|
|
939
|
+
typeof server.bindCLIShortcuts === 'function'
|
|
940
|
+
) {
|
|
941
|
+
server.bindCLIShortcuts({
|
|
942
|
+
print: true,
|
|
943
|
+
customShortcuts: [
|
|
944
|
+
{
|
|
945
|
+
key: 'r',
|
|
946
|
+
description: 'force reload browser',
|
|
947
|
+
action(srv) {
|
|
948
|
+
srv.ws.send({
|
|
949
|
+
type: 'full-reload',
|
|
950
|
+
path: '*',
|
|
951
|
+
});
|
|
952
|
+
},
|
|
953
|
+
},
|
|
954
|
+
],
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
return {
|
|
959
|
+
server,
|
|
960
|
+
port: actualPort,
|
|
961
|
+
baseUrl,
|
|
962
|
+
};
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
/**
|
|
966
|
+
* Architect-compatible `executeDevServerBuilder` / `serveApplication` async generator
|
|
967
|
+
* (`@ngrs/build:dev-server`).
|
|
968
|
+
*/
|
|
969
|
+
export async function* executeDevServerBuilder(options = {}, context = {}, extensions) {
|
|
970
|
+
const workspaceRoot = context?.workspaceRoot || options?.workspaceRoot || process.cwd();
|
|
971
|
+
const logger = context?.logger || createConsoleLogger(Boolean(options?.verbose));
|
|
972
|
+
|
|
973
|
+
const loaded = await loadWorkspace(workspaceRoot);
|
|
974
|
+
const projectName = resolveProjectName(
|
|
975
|
+
loaded.workspace,
|
|
976
|
+
context?.target?.project || options?.project,
|
|
977
|
+
);
|
|
978
|
+
const projectMetadata = loaded.workspace.projects[projectName] || {};
|
|
979
|
+
const targets = projectMetadata.architect || projectMetadata.targets || {};
|
|
980
|
+
|
|
981
|
+
const overrides = { ...(options || {}) };
|
|
982
|
+
delete overrides.workspaceRoot;
|
|
983
|
+
delete overrides.project;
|
|
984
|
+
const requestedConfig = context?.target?.configuration || overrides.configuration;
|
|
985
|
+
delete overrides.configuration;
|
|
986
|
+
|
|
987
|
+
let baseServeOpts = {};
|
|
988
|
+
const targetName = context?.target?.target || 'serve';
|
|
989
|
+
if (targets[targetName]) {
|
|
990
|
+
const serveConfigs = targets[targetName].configurations || {};
|
|
991
|
+
if (requestedConfig && !serveConfigs[requestedConfig]) {
|
|
992
|
+
baseServeOpts = {
|
|
993
|
+
...resolveTargetOptions(
|
|
994
|
+
loaded.workspace,
|
|
995
|
+
{ project: projectName, target: targetName },
|
|
996
|
+
'development',
|
|
997
|
+
),
|
|
998
|
+
buildTarget: `${projectName}:build:${requestedConfig}`,
|
|
999
|
+
};
|
|
1000
|
+
} else {
|
|
1001
|
+
baseServeOpts = resolveTargetOptions(
|
|
1002
|
+
loaded.workspace,
|
|
1003
|
+
{
|
|
1004
|
+
project: projectName,
|
|
1005
|
+
target: targetName,
|
|
1006
|
+
...(requestedConfig ? { configuration: requestedConfig } : {}),
|
|
1007
|
+
},
|
|
1008
|
+
'development',
|
|
1009
|
+
);
|
|
1010
|
+
}
|
|
1011
|
+
} else if (requestedConfig) {
|
|
1012
|
+
baseServeOpts = {
|
|
1013
|
+
buildTarget: `${projectName}:build:${requestedConfig}`,
|
|
1014
|
+
};
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
const mergedServeOpts = {
|
|
1018
|
+
...baseServeOpts,
|
|
1019
|
+
...overrides,
|
|
1020
|
+
};
|
|
1021
|
+
|
|
1022
|
+
const normalizedServe = normalizeDevServerOptions(
|
|
1023
|
+
mergedServeOpts,
|
|
1024
|
+
loaded.workspace,
|
|
1025
|
+
projectName,
|
|
1026
|
+
loaded.workspaceRoot,
|
|
1027
|
+
);
|
|
1028
|
+
|
|
1029
|
+
// Merge target build options (ensuring dev-friendly defaults when unconfigured)
|
|
1030
|
+
const devBuildRaw = {
|
|
1031
|
+
optimization: false,
|
|
1032
|
+
sourceMap: true,
|
|
1033
|
+
outputHashing: 'none',
|
|
1034
|
+
...normalizedServe.buildTargetOptions,
|
|
1035
|
+
};
|
|
1036
|
+
const targetProjectMeta =
|
|
1037
|
+
loaded.workspace.projects[normalizedServe.buildTarget.project] || projectMetadata;
|
|
1038
|
+
const normalizedApp = normalizeApplicationOptions(
|
|
1039
|
+
devBuildRaw,
|
|
1040
|
+
targetProjectMeta,
|
|
1041
|
+
loaded.workspaceRoot,
|
|
1042
|
+
normalizedServe.buildTarget.project,
|
|
1043
|
+
);
|
|
1044
|
+
|
|
1045
|
+
const started = await startDevServer(normalizedServe, normalizedApp, logger, extensions);
|
|
1046
|
+
|
|
1047
|
+
let closed = false;
|
|
1048
|
+
let stopResolve;
|
|
1049
|
+
const stopPromise = new Promise((resolve) => {
|
|
1050
|
+
stopResolve = resolve;
|
|
1051
|
+
});
|
|
1052
|
+
|
|
1053
|
+
const closeServer = async () => {
|
|
1054
|
+
if (closed) {
|
|
1055
|
+
return;
|
|
1056
|
+
}
|
|
1057
|
+
closed = true;
|
|
1058
|
+
await started.server.close();
|
|
1059
|
+
stopResolve();
|
|
1060
|
+
};
|
|
1061
|
+
|
|
1062
|
+
context?.addTeardown?.(closeServer);
|
|
1063
|
+
started.server.httpServer?.once('close', () => {
|
|
1064
|
+
closed = true;
|
|
1065
|
+
stopResolve();
|
|
1066
|
+
});
|
|
1067
|
+
|
|
1068
|
+
try {
|
|
1069
|
+
yield {
|
|
1070
|
+
success: true,
|
|
1071
|
+
port: started.port,
|
|
1072
|
+
baseUrl: started.baseUrl,
|
|
1073
|
+
server: started.server,
|
|
1074
|
+
close: closeServer,
|
|
1075
|
+
};
|
|
1076
|
+
|
|
1077
|
+
await stopPromise;
|
|
1078
|
+
} finally {
|
|
1079
|
+
await closeServer();
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
export const devServerBuilder = createBuilder(executeDevServerBuilder);
|
|
1084
|
+
export {
|
|
1085
|
+
executeDevServerBuilder as executeDevServer,
|
|
1086
|
+
executeDevServerBuilder as serveApplication,
|
|
1087
|
+
};
|
|
1088
|
+
export default devServerBuilder;
|