-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(angular): add flag to include hydration when setting up ssr (#18675
- Loading branch information
Showing
9 changed files
with
221 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/angular/src/generators/setup-ssr/lib/add-hydration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
joinPathFragments, | ||
readProjectConfiguration, | ||
type Tree, | ||
} from '@nx/devkit'; | ||
import { type Schema } from '../schema'; | ||
import { | ||
addProviderToAppConfig, | ||
addProviderToModule, | ||
} from '../../../utils/nx-devkit/ast-utils'; | ||
import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript'; | ||
import { SourceFile } from 'typescript'; | ||
import { insertImport } from '@nx/js'; | ||
|
||
let tsModule: typeof import('typescript'); | ||
|
||
export function addHydration(tree: Tree, options: Schema) { | ||
const projectConfig = readProjectConfiguration(tree, options.project); | ||
|
||
if (!tsModule) { | ||
tsModule = ensureTypescript(); | ||
} | ||
const addImport = ( | ||
source: SourceFile, | ||
symbolName: string, | ||
packageName: string, | ||
filePath: string, | ||
isDefault = false | ||
): SourceFile => { | ||
return insertImport( | ||
tree, | ||
source, | ||
filePath, | ||
symbolName, | ||
packageName, | ||
isDefault | ||
); | ||
}; | ||
|
||
const pathToClientConfigFile = options.standalone | ||
? joinPathFragments(projectConfig.sourceRoot, 'app/app.config.ts') | ||
: joinPathFragments(projectConfig.sourceRoot, 'app/app.module.ts'); | ||
|
||
const sourceText = tree.read(pathToClientConfigFile, 'utf-8'); | ||
let sourceFile = tsModule.createSourceFile( | ||
pathToClientConfigFile, | ||
sourceText, | ||
tsModule.ScriptTarget.Latest, | ||
true | ||
); | ||
|
||
sourceFile = addImport( | ||
sourceFile, | ||
'provideClientHydration', | ||
'@angular/platform-browser', | ||
pathToClientConfigFile | ||
); | ||
|
||
if (options.standalone) { | ||
addProviderToAppConfig( | ||
tree, | ||
pathToClientConfigFile, | ||
'provideClientHydration()' | ||
); | ||
} else { | ||
addProviderToModule( | ||
tree, | ||
sourceFile, | ||
pathToClientConfigFile, | ||
'provideClientHydration()' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/angular/src/generators/setup-ssr/lib/validate-options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,28 @@ | ||
import type { Tree } from '@nx/devkit'; | ||
import { stripIndents } from '@nx/devkit'; | ||
import { | ||
validateProject, | ||
validateStandaloneOption, | ||
} from '../../utils/validations'; | ||
import type { Schema } from '../schema'; | ||
import { getInstalledAngularVersionInfo } from '../../utils/version-utils'; | ||
import { lt } from 'semver'; | ||
|
||
export function validateOptions(tree: Tree, options: Schema): void { | ||
validateProject(tree, options.project); | ||
validateStandaloneOption(tree, options.standalone); | ||
validateHydrationOption(tree, options.hydration); | ||
} | ||
|
||
function validateHydrationOption(tree: Tree, hydration: boolean): void { | ||
if (!hydration) { | ||
return; | ||
} | ||
|
||
const installedAngularVersion = getInstalledAngularVersionInfo(tree).version; | ||
|
||
if (lt(installedAngularVersion, '16.0.0')) { | ||
throw new Error(stripIndents`The "hydration" option is only supported in Angular >= 16.0.0. You are currently using "${installedAngularVersion}". | ||
You can resolve this error by removing the "hydration" option or by migrating to Angular 16.0.0.`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b9ca7ce
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nx-dev – ./
nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev