Skip to content

Commit

Permalink
fix: CXSPA-6456 Missing script in SSR app causes errors during the CC…
Browse files Browse the repository at this point in the history
…v2 build - 2211.20.1 backport (#18616)
  • Loading branch information
pawelfras authored Mar 12, 2024
1 parent df0fc0f commit 8719a7d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
29 changes: 29 additions & 0 deletions projects/schematics/src/add-ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,34 @@ function prepareDependencies(): NodeDependency[] {
return spartacusDependencies.concat(thirdPartyDependencies);
}

/**
* Adds `build:ssr` script to `package.json` as it's required for CCv2 build - process fails when script is missing.
*
* TODO: CXSPA-6466 Can be removed if Model T adjust their build process to not require this script.
*/
function addBuildSsrScript(spartacusOptions: SpartacusOptions): Rule {
return (tree: Tree, context: SchematicContext) => {
if (spartacusOptions.debug) {
context.logger.info(
`⌛️ Adding "build:ssr" script to "package.json"... (CCv2 purposes)`
);
}
const pkgPath = '/package.json';
const pkg = tree.readJson(pkgPath) as {
scripts?: Record<string, string>;
} | null;
if (pkg === null) {
throw new SchematicsException('Could not find package.json');
}
pkg.scripts = {
...pkg.scripts,
'build:ssr': 'ng build',
};

tree.overwrite(pkgPath, JSON.stringify(pkg, null, 2));
};
}

/**
* Fixes the configuration for SSR and Prerendering to be able to work with Spartacus.
*/
Expand Down Expand Up @@ -467,6 +495,7 @@ export function addSSR(options: SpartacusOptions): Rule {
externalSchematic(ANGULAR_SSR, 'ng-add', {
project: options.project,
}),
addBuildSsrScript(options),
modifyAppServerModuleFile(),
removeClientHydration(options),
modifyIndexHtmlFile(options),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ exports[`Spartacus Schematics: ng-add should add spartacus properly with SSR 2`]
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test",
"serve:ssr:schematics-test": "node dist/schematics-test/server/server.mjs"
"serve:ssr:schematics-test": "node dist/schematics-test/server/server.mjs",
"build:ssr": "ng build"
},
"private": true,
"dependencies": {
Expand Down

0 comments on commit 8719a7d

Please sign in to comment.