Skip to content

Commit d9f7d43

Browse files
committed
fix(@schematics/angular): add helper script to spawn SSR server from dist
This commit adds a helper script in the `package.json` when running `ng add @angular/ssr` or `ng new --ssr` that can be used to spawn the SSR server. Example of script: ```json { "scripts": { "serve:ssr:my-app": "node dist/my-app/server/server.mjs" } } ``` Closes #26315 (cherry picked from commit 80b024b)
1 parent d3b5491 commit d9f7d43

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

packages/schematics/angular/ssr/index.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,28 @@ async function getOutputPath(
5757
return outputPath;
5858
}
5959

60-
function addScriptsRule(options: SSROptions): Rule {
60+
function addScriptsRule({ project }: SSROptions, isUsingApplicationBuilder: boolean): Rule {
6161
return async (host) => {
6262
const pkgPath = '/package.json';
63-
const buffer = host.read(pkgPath);
64-
if (buffer === null) {
63+
const pkg = host.readJson(pkgPath) as { scripts?: Record<string, string> } | null;
64+
if (pkg === null) {
6565
throw new SchematicsException('Could not find package.json');
6666
}
6767

68-
const serverDist = await getOutputPath(host, options.project, 'server');
69-
const pkg = JSON.parse(buffer.toString()) as { scripts?: Record<string, string> };
70-
pkg.scripts = {
71-
...pkg.scripts,
72-
'dev:ssr': `ng run ${options.project}:${SERVE_SSR_TARGET_NAME}`,
73-
'serve:ssr': `node ${serverDist}/main.js`,
74-
'build:ssr': `ng build && ng run ${options.project}:server`,
75-
'prerender': `ng run ${options.project}:${PRERENDER_TARGET_NAME}`,
76-
};
68+
if (isUsingApplicationBuilder) {
69+
const distPath = await getOutputPath(host, project, 'build');
70+
pkg.scripts ??= {};
71+
pkg.scripts[`serve:ssr:${project}`] = `node ${distPath}/server/server.mjs`;
72+
} else {
73+
const serverDist = await getOutputPath(host, project, 'server');
74+
pkg.scripts = {
75+
...pkg.scripts,
76+
'dev:ssr': `ng run ${project}:${SERVE_SSR_TARGET_NAME}`,
77+
'serve:ssr': `node ${serverDist}/main.js`,
78+
'build:ssr': `ng build && ng run ${project}:server`,
79+
'prerender': `ng run ${project}:${PRERENDER_TARGET_NAME}`,
80+
};
81+
}
7782

7883
host.overwrite(pkgPath, JSON.stringify(pkg, null, 2));
7984
};
@@ -278,11 +283,11 @@ export default function (options: SSROptions): Rule {
278283
updateApplicationBuilderTsConfigRule(options),
279284
]
280285
: [
281-
addScriptsRule(options),
282286
updateWebpackBuilderServerTsConfigRule(options),
283287
updateWebpackBuilderWorkspaceConfigRule(options),
284288
]),
285289
addServerFile(options, isStandalone),
290+
addScriptsRule(options, isUsingApplicationBuilder),
286291
addDependencies(),
287292
]);
288293
};

packages/schematics/angular/ssr/index_spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ describe('SSR Schematic', () => {
136136
bootstrap,
137137
`);
138138
});
139+
140+
it('should add script section in package.json', async () => {
141+
const tree = await schematicRunner.runSchematic('ssr', defaultOptions, appTree);
142+
const { scripts } = tree.readJson('/package.json') as { scripts: Record<string, string> };
143+
144+
expect(scripts['serve:ssr:test-app']).toBe(`node dist/test-app/server/server.mjs`);
145+
});
139146
});
140147

141148
describe('Legacy browser builder', () => {

0 commit comments

Comments
 (0)