Skip to content

Commit 8f9a0d7

Browse files
committed
feat(@angular-devkit/build-angular): support standalone apps route discovery during prerendering
This fixes an issue were routes could not be discovered automatically in a standalone application. This is a total overhaul of the route extraction process as instead of using `guess-parser` NPM package, we now use the Angular Router. This enables a number of exciting possibilities for the future which were not possible before. # How it works? The application is bootstrapped and through DI injection we get the injector and router config instance and recursively build the routes tree.
1 parent 5a204b8 commit 8f9a0d7

29 files changed

+802
-194
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@
145145
"eslint-plugin-import": "2.28.1",
146146
"express": "4.18.2",
147147
"fast-glob": "3.3.1",
148-
"guess-parser": "0.4.22",
149148
"http-proxy": "^1.18.1",
150149
"http-proxy-middleware": "2.0.6",
151150
"https-proxy-agent": "7.0.2",

packages/angular/ssr/schematics/ng-add/files/server-builder/server.ts.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,4 @@ if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
6464
run();
6565
}
6666

67-
<% if (isStandalone) { %>export default bootstrap;<% } else { %>export * from './src/main.server';<% } %>
67+
export default <% if (isStandalone) { %>bootstrap<% } else { %>AppServerModule<% } %>;

packages/angular/ssr/schematics/ng-add/index_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ describe('SSR Schematic', () => {
194194
const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree);
195195

196196
const content = tree.readContent('/projects/test-app/server.ts');
197-
expect(content).toContain(`export * from './src/main.server'`);
197+
expect(content).toContain(`export default AppServerModule`);
198198
});
199199

200200
it(`should add correct value to 'distFolder'`, async () => {

packages/angular/ssr/src/common-engine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ async function exists(path: fs.PathLike): Promise<boolean> {
162162
}
163163

164164
function isBootstrapFn(value: unknown): value is () => Promise<ApplicationRef> {
165-
// We can differentiate between a module and a bootstrap function by reading `cmp`:
165+
// We can differentiate between a module and a bootstrap function by reading compiler-generated `ɵmod` static property:
166166
return typeof value === 'function' && !('ɵmod' in value);
167167
}
168168

packages/angular_devkit/build_angular/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ ts_library(
122122
module_root = "src/index.d.ts",
123123
deps = [
124124
"//packages/angular_devkit/architect",
125+
"//packages/angular_devkit/build_angular/src/utils/routes-extractor",
125126
"//packages/angular_devkit/build_webpack",
126127
"//packages/angular_devkit/core",
127128
"//packages/angular_devkit/core/node",
@@ -168,7 +169,6 @@ ts_library(
168169
"@npm//esbuild",
169170
"@npm//esbuild-wasm",
170171
"@npm//fast-glob",
171-
"@npm//guess-parser",
172172
"@npm//http-proxy-middleware",
173173
"@npm//https-proxy-agent",
174174
"@npm//inquirer",

packages/angular_devkit/build_angular/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
"css-loader": "6.8.1",
3535
"esbuild-wasm": "0.19.3",
3636
"fast-glob": "3.3.1",
37-
"guess-parser": "0.4.22",
3837
"https-proxy-agent": "7.0.2",
3938
"http-proxy-middleware": "2.0.6",
4039
"inquirer": "8.2.6",

packages/angular_devkit/build_angular/src/builders/app-shell/render-worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ async function render({ serverBundlePath, document, url }: RenderRequest): Promi
102102
}
103103

104104
function isBootstrapFn(value: unknown): value is () => Promise<ApplicationRef> {
105-
// We can differentiate between a module and a bootstrap function by reading `cmp`:
105+
// We can differentiate between a module and a bootstrap function by reading compiler-generated `ɵmod` static property:
106106
return typeof value === 'function' && !('ɵmod' in value);
107107
}
108108

packages/angular_devkit/build_angular/src/builders/application/execute-build.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export async function executeBuild(
5353
prerenderOptions,
5454
appShellOptions,
5555
ssrOptions,
56+
verbose,
5657
} = options;
5758

5859
const browsers = getSupportedBrowsers(projectRoot, context.logger);
@@ -182,13 +183,13 @@ export async function executeBuild(
182183

183184
const { output, warnings, errors } = await prerenderPages(
184185
workspaceRoot,
185-
options.tsconfig,
186186
appShellOptions,
187187
prerenderOptions,
188188
executionResult.outputFiles,
189189
indexContentOutputNoCssInlining,
190190
optimizationOptions.styles.inlineCritical,
191191
maxWorkers,
192+
verbose,
192193
);
193194

194195
printWarningsAndErrorsToConsole(context, warnings, errors);
@@ -242,6 +243,7 @@ export async function executeBuild(
242243
if (optimizationOptions.scripts || optimizationOptions.styles.minify) {
243244
estimatedTransferSizes = await calculateEstimatedTransferSizes(executionResult.outputFiles);
244245
}
246+
245247
logBuildStats(context, metafile, initialFiles, estimatedTransferSizes);
246248

247249
const buildTime = Number(process.hrtime.bigint() - startTime) / 10 ** 9;

packages/angular_devkit/build_angular/src/builders/application/options.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,11 @@ export async function normalizeOptions(
181181

182182
let prerenderOptions;
183183
if (options.prerender) {
184-
const {
185-
discoverRoutes = true,
186-
routes = [],
187-
routesFile = undefined,
188-
} = options.prerender === true ? {} : options.prerender;
184+
const { discoverRoutes = true, routesFile = undefined } =
185+
options.prerender === true ? {} : options.prerender;
189186

190187
prerenderOptions = {
191188
discoverRoutes,
192-
routes,
193189
routesFile: routesFile && path.join(workspaceRoot, routesFile),
194190
};
195191
}

packages/angular_devkit/build_angular/src/builders/application/schema.json

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -423,19 +423,9 @@
423423
"type": "string",
424424
"description": "The path to a file containing routes separated by newlines."
425425
},
426-
"routes": {
427-
"type": "array",
428-
"description": "The routes to render.",
429-
"items": {
430-
"minItems": 1,
431-
"type": "string",
432-
"uniqueItems": true
433-
},
434-
"default": []
435-
},
436426
"discoverRoutes": {
437427
"type": "boolean",
438-
"description": "Whether the builder should statically discover routes.",
428+
"description": "Whether the builder should discover routers using the Angular Router.",
439429
"default": true
440430
}
441431
},

0 commit comments

Comments
 (0)