Skip to content

Commit

Permalink
fix(@schematics/angular): add helper script to spawn SSR server from …
Browse files Browse the repository at this point in the history
…`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)
  • Loading branch information
alan-agius4 committed Nov 13, 2023
1 parent d3b5491 commit d9f7d43
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
31 changes: 18 additions & 13 deletions packages/schematics/angular/ssr/index.ts
Expand Up @@ -57,23 +57,28 @@ async function getOutputPath(
return outputPath;
}

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

const serverDist = await getOutputPath(host, options.project, 'server');
const pkg = JSON.parse(buffer.toString()) as { scripts?: Record<string, string> };
pkg.scripts = {
...pkg.scripts,
'dev:ssr': `ng run ${options.project}:${SERVE_SSR_TARGET_NAME}`,
'serve:ssr': `node ${serverDist}/main.js`,
'build:ssr': `ng build && ng run ${options.project}:server`,
'prerender': `ng run ${options.project}:${PRERENDER_TARGET_NAME}`,
};
if (isUsingApplicationBuilder) {
const distPath = await getOutputPath(host, project, 'build');
pkg.scripts ??= {};
pkg.scripts[`serve:ssr:${project}`] = `node ${distPath}/server/server.mjs`;
} else {
const serverDist = await getOutputPath(host, project, 'server');
pkg.scripts = {
...pkg.scripts,
'dev:ssr': `ng run ${project}:${SERVE_SSR_TARGET_NAME}`,
'serve:ssr': `node ${serverDist}/main.js`,
'build:ssr': `ng build && ng run ${project}:server`,
'prerender': `ng run ${project}:${PRERENDER_TARGET_NAME}`,
};
}

host.overwrite(pkgPath, JSON.stringify(pkg, null, 2));
};
Expand Down Expand Up @@ -278,11 +283,11 @@ export default function (options: SSROptions): Rule {
updateApplicationBuilderTsConfigRule(options),
]
: [
addScriptsRule(options),
updateWebpackBuilderServerTsConfigRule(options),
updateWebpackBuilderWorkspaceConfigRule(options),
]),
addServerFile(options, isStandalone),
addScriptsRule(options, isUsingApplicationBuilder),
addDependencies(),
]);
};
Expand Down
7 changes: 7 additions & 0 deletions packages/schematics/angular/ssr/index_spec.ts
Expand Up @@ -136,6 +136,13 @@ describe('SSR Schematic', () => {
bootstrap,
`);
});

it('should add script section in package.json', async () => {
const tree = await schematicRunner.runSchematic('ssr', defaultOptions, appTree);
const { scripts } = tree.readJson('/package.json') as { scripts: Record<string, string> };

expect(scripts['serve:ssr:test-app']).toBe(`node dist/test-app/server/server.mjs`);
});
});

describe('Legacy browser builder', () => {
Expand Down

0 comments on commit d9f7d43

Please sign in to comment.