Skip to content

Commit da6ef62

Browse files
committed
fix(@schematics/angular): ensure app-shell schematic consistently uses withAppShell
Previously, Webpack-based builders did not utilize this option.
1 parent 689ab8a commit da6ef62

File tree

2 files changed

+2
-140
lines changed

2 files changed

+2
-140
lines changed

packages/schematics/angular/app-shell/index.ts

+2-138
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,23 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {
10-
Rule,
11-
SchematicsException,
12-
Tree,
13-
chain,
14-
noop,
15-
schematic,
16-
} from '@angular-devkit/schematics';
9+
import { Rule, SchematicsException, Tree, chain, schematic } from '@angular-devkit/schematics';
1710
import { dirname, join } from 'node:path/posix';
1811
import ts from '../third_party/github.com/Microsoft/TypeScript/lib/typescript';
1912
import {
20-
addSymbolToNgModuleMetadata,
2113
findNode,
2214
findNodes,
2315
getDecoratorMetadata,
2416
getSourceNodes,
2517
insertImport,
26-
isImported,
2718
} from '../utility/ast-utils';
2819
import { applyToUpdateRecorder } from '../utility/change';
2920
import { getAppModulePath, isStandaloneApp } from '../utility/ng-ast-utils';
30-
import { isUsingApplicationBuilder, targetBuildNotFoundError } from '../utility/project-targets';
21+
import { targetBuildNotFoundError } from '../utility/project-targets';
3122
import { findBootstrapApplicationCall, getMainFilePath } from '../utility/standalone/util';
3223
import { getWorkspace } from '../utility/workspace';
3324
import { Schema as AppShellOptions } from './schema';
3425

35-
const APP_SHELL_ROUTE = 'shell';
36-
3726
function getSourceFile(host: Tree, path: string): ts.SourceFile {
3827
const content = host.readText(path);
3928
const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true);
@@ -156,126 +145,6 @@ function getMetadataProperty(metadata: ts.Node, propertyName: string): ts.Proper
156145
return property;
157146
}
158147

159-
function addServerRoutes(options: AppShellOptions): Rule {
160-
return async (host: Tree) => {
161-
// The workspace gets updated so this needs to be reloaded
162-
const workspace = await getWorkspace(host);
163-
const project = workspace.projects.get(options.project);
164-
if (!project) {
165-
throw new SchematicsException(`Invalid project name (${options.project})`);
166-
}
167-
168-
const modulePath = getServerModulePath(host, project.sourceRoot || 'src', 'main.server.ts');
169-
if (modulePath === null) {
170-
throw new SchematicsException('Server module not found.');
171-
}
172-
173-
let moduleSource = getSourceFile(host, modulePath);
174-
if (!isImported(moduleSource, 'Routes', '@angular/router')) {
175-
const recorder = host.beginUpdate(modulePath);
176-
const routesChange = insertImport(moduleSource, modulePath, 'Routes', '@angular/router');
177-
if (routesChange) {
178-
applyToUpdateRecorder(recorder, [routesChange]);
179-
}
180-
181-
const imports = getSourceNodes(moduleSource)
182-
.filter((node) => node.kind === ts.SyntaxKind.ImportDeclaration)
183-
.sort((a, b) => a.getStart() - b.getStart());
184-
const insertPosition = imports[imports.length - 1].getEnd();
185-
const routeText = `\n\nconst routes: Routes = [ { path: '${APP_SHELL_ROUTE}', component: AppShell }];`;
186-
recorder.insertRight(insertPosition, routeText);
187-
host.commitUpdate(recorder);
188-
}
189-
190-
moduleSource = getSourceFile(host, modulePath);
191-
if (!isImported(moduleSource, 'RouterModule', '@angular/router')) {
192-
const recorder = host.beginUpdate(modulePath);
193-
const routerModuleChange = insertImport(
194-
moduleSource,
195-
modulePath,
196-
'RouterModule',
197-
'@angular/router',
198-
);
199-
200-
if (routerModuleChange) {
201-
applyToUpdateRecorder(recorder, [routerModuleChange]);
202-
}
203-
204-
const metadataChange = addSymbolToNgModuleMetadata(
205-
moduleSource,
206-
modulePath,
207-
'imports',
208-
'RouterModule.forRoot(routes)',
209-
);
210-
if (metadataChange) {
211-
applyToUpdateRecorder(recorder, metadataChange);
212-
}
213-
host.commitUpdate(recorder);
214-
}
215-
};
216-
}
217-
218-
function addStandaloneServerRoute(options: AppShellOptions): Rule {
219-
return async (host: Tree) => {
220-
const workspace = await getWorkspace(host);
221-
const project = workspace.projects.get(options.project);
222-
if (!project) {
223-
throw new SchematicsException(`Project name "${options.project}" doesn't not exist.`);
224-
}
225-
226-
const configFilePath = join(project.sourceRoot ?? 'src', 'app/app.config.server.ts');
227-
if (!host.exists(configFilePath)) {
228-
throw new SchematicsException(`Cannot find "${configFilePath}".`);
229-
}
230-
231-
const recorder = host.beginUpdate(configFilePath);
232-
let configSourceFile = getSourceFile(host, configFilePath);
233-
if (!isImported(configSourceFile, 'ROUTES', '@angular/router')) {
234-
const routesChange = insertImport(
235-
configSourceFile,
236-
configFilePath,
237-
'ROUTES',
238-
'@angular/router',
239-
);
240-
241-
if (routesChange) {
242-
applyToUpdateRecorder(recorder, [routesChange]);
243-
}
244-
}
245-
246-
configSourceFile = getSourceFile(host, configFilePath);
247-
const providersLiteral = findNodes(configSourceFile, ts.isPropertyAssignment).find(
248-
(n) => ts.isArrayLiteralExpression(n.initializer) && n.name.getText() === 'providers',
249-
)?.initializer as ts.ArrayLiteralExpression | undefined;
250-
if (!providersLiteral) {
251-
throw new SchematicsException(
252-
`Cannot find the "providers" configuration in "${configFilePath}".`,
253-
);
254-
}
255-
256-
// Add route to providers literal.
257-
recorder.remove(providersLiteral.getStart(), providersLiteral.getWidth());
258-
const updatedProvidersString = [
259-
...providersLiteral.elements.map((element) => ' ' + element.getText()),
260-
` {
261-
provide: ROUTES,
262-
multi: true,
263-
useValue: [{
264-
path: '${APP_SHELL_ROUTE}',
265-
component: AppShell
266-
}]
267-
}\n `,
268-
];
269-
270-
recorder.insertRight(providersLiteral.getStart(), `[\n${updatedProvidersString.join(',\n')}]`);
271-
272-
applyToUpdateRecorder(recorder, [
273-
insertImport(configSourceFile, configFilePath, 'AppShell', './app-shell/app-shell'),
274-
]);
275-
host.commitUpdate(recorder);
276-
};
277-
}
278-
279148
function addServerRoutingConfig(options: AppShellOptions, isStandalone: boolean): Rule {
280149
return async (host: Tree) => {
281150
const workspace = await getWorkspace(host);
@@ -335,11 +204,6 @@ export default function (options: AppShellOptions): Rule {
335204
return chain([
336205
validateProject(browserEntryPoint),
337206
schematic('server', options),
338-
...(isUsingApplicationBuilder(project)
339-
? [noop()]
340-
: isStandalone
341-
? [addStandaloneServerRoute(options)]
342-
: [addServerRoutes(options)]),
343207
addServerRoutingConfig(options, isStandalone),
344208
schematic('component', {
345209
name: 'app-shell',

packages/schematics/angular/server/index.ts

-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ import {
1414
apply,
1515
applyTemplates,
1616
chain,
17-
filter,
1817
mergeWith,
1918
move,
20-
noop,
2119
strings,
2220
url,
2321
} from '@angular-devkit/schematics';

0 commit comments

Comments
 (0)