-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@angular/ssr): move
CommonEngine
API to /node
entry-point
Refactored the `CommonEngine` API import path to remove Node.js dependencies from the `@angular/ssr` main entry-point. BREAKING CHANGE: The `CommonEngine` API now needs to be imported from `@angular/ssr/node`. **Before** ```ts import { CommonEngine } from '@angular/ssr'; ``` **After** ```ts import { CommonEngine } from '@angular/ssr/node'; ```
- Loading branch information
1 parent
ac6935d
commit 4b09887
Showing
21 changed files
with
257 additions
and
45 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
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,40 @@ | ||
## API Report File for "@angular/ssr_node" | ||
|
||
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). | ||
```ts | ||
|
||
import { ApplicationRef } from '@angular/core'; | ||
import { StaticProvider } from '@angular/core'; | ||
import { Type } from '@angular/core'; | ||
|
||
// @public | ||
export class CommonEngine { | ||
constructor(options?: CommonEngineOptions | undefined); | ||
render(opts: CommonEngineRenderOptions): Promise<string>; | ||
} | ||
|
||
// @public (undocumented) | ||
export interface CommonEngineOptions { | ||
bootstrap?: Type<{}> | (() => Promise<ApplicationRef>); | ||
enablePerformanceProfiler?: boolean; | ||
providers?: StaticProvider[]; | ||
} | ||
|
||
// @public (undocumented) | ||
export interface CommonEngineRenderOptions { | ||
bootstrap?: Type<{}> | (() => Promise<ApplicationRef>); | ||
// (undocumented) | ||
document?: string; | ||
// (undocumented) | ||
documentFilePath?: string; | ||
inlineCriticalCss?: boolean; | ||
providers?: StaticProvider[]; | ||
publicPath?: string; | ||
// (undocumented) | ||
url?: string; | ||
} | ||
|
||
// (No @packageDocumentation comment for this package) | ||
|
||
``` |
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 |
---|---|---|
|
@@ -44,6 +44,7 @@ ng_package( | |
tags = ["release-package"], | ||
deps = [ | ||
":ssr", | ||
"//packages/angular/ssr/node", | ||
], | ||
) | ||
|
||
|
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,20 @@ | ||
load("//tools:defaults.bzl", "ts_library") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
ts_library( | ||
name = "node", | ||
srcs = glob( | ||
[ | ||
"*.ts", | ||
"src/**/*.ts", | ||
], | ||
), | ||
module_name = "@angular/ssr/node", | ||
deps = [ | ||
"//packages/angular/ssr", | ||
"@npm//@angular/core", | ||
"@npm//@angular/platform-server", | ||
"@npm//@types/node", | ||
], | ||
) |
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,9 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
export * from './public_api'; |
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,13 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
export { | ||
CommonEngine, | ||
type CommonEngineRenderOptions, | ||
type CommonEngineOptions, | ||
} from './src/common-engine/common-engine'; |
File renamed without changes.
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
File renamed without changes.
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
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
92 changes: 92 additions & 0 deletions
92
packages/schematics/angular/migrations/update-ssr-imports/migration.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,92 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import { DirEntry, Rule, UpdateRecorder } from '@angular-devkit/schematics'; | ||
import * as ts from '../../third_party/github.com/Microsoft/TypeScript/lib/typescript'; | ||
|
||
function* visit(directory: DirEntry): IterableIterator<ts.SourceFile> { | ||
for (const path of directory.subfiles) { | ||
if (path.endsWith('.ts') && !path.endsWith('.d.ts')) { | ||
const entry = directory.file(path); | ||
if (entry) { | ||
const content = entry.content; | ||
if (content.includes('CommonEngine') && !content.includes('@angular/ssr/node')) { | ||
const source = ts.createSourceFile( | ||
entry.path, | ||
content.toString().replace(/^\uFEFF/, ''), | ||
ts.ScriptTarget.Latest, | ||
true, | ||
); | ||
|
||
yield source; | ||
} | ||
} | ||
} | ||
} | ||
|
||
for (const path of directory.subdirs) { | ||
if (path === 'node_modules' || path.startsWith('.')) { | ||
continue; | ||
} | ||
|
||
yield* visit(directory.dir(path)); | ||
} | ||
} | ||
|
||
/** | ||
* Schematics rule that identifies and updates import declarations in TypeScript files. | ||
* Specifically, it modifies imports of '@angular/ssr' by appending '/node' if the | ||
* `CommonEngine` is used from the old entry point. | ||
* | ||
*/ | ||
export default function (): Rule { | ||
return (tree) => { | ||
for (const sourceFile of visit(tree.root)) { | ||
let recorder: UpdateRecorder | undefined; | ||
|
||
const allImportDeclarations = sourceFile.statements.filter((n) => ts.isImportDeclaration(n)); | ||
if (allImportDeclarations.length === 0) { | ||
continue; | ||
} | ||
|
||
const ssrImports = allImportDeclarations.filter( | ||
(n) => ts.isStringLiteral(n.moduleSpecifier) && n.moduleSpecifier.text === '@angular/ssr', | ||
); | ||
for (const ssrImport of ssrImports) { | ||
const ssrNamedBinding = getNamedImports(ssrImport); | ||
if (ssrNamedBinding) { | ||
const isUsingOldEntryPoint = ssrNamedBinding.elements.some((e) => | ||
e.name.text.startsWith('CommonEngine'), | ||
); | ||
|
||
if (!isUsingOldEntryPoint) { | ||
continue; | ||
} | ||
|
||
recorder ??= tree.beginUpdate(sourceFile.fileName); | ||
recorder.insertRight(ssrImport.moduleSpecifier.getEnd() - 1, '/node'); | ||
} | ||
} | ||
|
||
if (recorder) { | ||
tree.commitUpdate(recorder); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
function getNamedImports( | ||
importDeclaration: ts.ImportDeclaration | undefined, | ||
): ts.NamedImports | undefined { | ||
const namedBindings = importDeclaration?.importClause?.namedBindings; | ||
if (namedBindings && ts.isNamedImports(namedBindings)) { | ||
return namedBindings; | ||
} | ||
|
||
return undefined; | ||
} |
67 changes: 67 additions & 0 deletions
67
packages/schematics/angular/migrations/update-ssr-imports/migration_spec.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,67 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import { tags } from '@angular-devkit/core'; | ||
import { EmptyTree } from '@angular-devkit/schematics'; | ||
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
|
||
describe('CommonEngine migration', () => { | ||
const schematicRunner = new SchematicTestRunner( | ||
'migrations', | ||
require.resolve('../migration-collection.json'), | ||
); | ||
|
||
let tree: UnitTestTree; | ||
beforeEach(() => { | ||
tree = new UnitTestTree(new EmptyTree()); | ||
}); | ||
|
||
function runMigration(): Promise<UnitTestTree> { | ||
return schematicRunner.runSchematic('update-ssr-imports', {}, tree); | ||
} | ||
|
||
it(`should replace 'CommonEngine*' imports from '@angular/ssr' to '@angular/ssr/node'`, async () => { | ||
tree.create( | ||
'/index.ts', | ||
tags.stripIndents` | ||
import { CommonEngine } from '@angular/ssr'; | ||
import type { CommonEngineOptions, CommonEngineRenderOptions } from '@angular/ssr'; | ||
`, | ||
); | ||
|
||
const newTree = await runMigration(); | ||
expect(newTree.readContent('/index.ts')).toBe(tags.stripIndents` | ||
import { CommonEngine } from '@angular/ssr/node'; | ||
import type { CommonEngineOptions, CommonEngineRenderOptions } from '@angular/ssr/node'; | ||
`); | ||
}); | ||
|
||
it(`should not replace 'CommonEngine*' imports from '@angular/ssr/node'`, async () => { | ||
const input = tags.stripIndents` | ||
import { CommonEngine } from '@angular/ssr/node'; | ||
import type { CommonEngineOptions, CommonEngineRenderOptions } from '@angular/ssr/node'; | ||
`; | ||
|
||
tree.create('/index.ts', input); | ||
|
||
const newTree = await runMigration(); | ||
expect(newTree.readContent('/index.ts')).toBe(input); | ||
}); | ||
|
||
it(`should not replace 'CommonEngine*' imports from other package`, async () => { | ||
const input = tags.stripIndents` | ||
import { CommonEngine } from 'unknown'; | ||
import type { CommonEngineOptions, CommonEngineRenderOptions } from 'unknown'; | ||
`; | ||
|
||
tree.create('/index.ts', input); | ||
|
||
const newTree = await runMigration(); | ||
expect(newTree.readContent('/index.ts')).toBe(input); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
packages/schematics/angular/ssr/files/application-builder/server.ts.template
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
2 changes: 1 addition & 1 deletion
2
packages/schematics/angular/ssr/files/server-builder/server.ts.template
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