-
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.
fix(angular): clean up ssr main.server.ts generation
- Loading branch information
1 parent
32cbab7
commit 44955aa
Showing
13 changed files
with
261 additions
and
57 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
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.
1 change: 1 addition & 0 deletions
1
packages/angular/src/generators/setup-ssr/files/base/src/__main__
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 @@ | ||
export { <%= rootModuleClassName %> } from './app/<%= rootModuleFileName.slice(0, -3) %>'; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
85 changes: 85 additions & 0 deletions
85
...angular/src/migrations/update-16-1-0/remove-render-module-platform-server-exports.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,85 @@ | ||
import type { Tree } from '@nx/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing-pre16'; | ||
import migration from './remove-render-module-platform-server-exports'; | ||
|
||
describe('remove-render-module-platform-server-exports migration', () => { | ||
let tree: Tree; | ||
const testTypeScriptFilePath = 'test.ts'; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
}); | ||
|
||
describe(`Migration to remove '@angular/platform-server' exports`, () => { | ||
it(`should delete '@angular/platform-server' export when 'renderModule' is the only exported symbol`, async () => { | ||
tree.write( | ||
testTypeScriptFilePath, | ||
` | ||
import { Path, join } from '@angular-devkit/core'; | ||
export { renderModule } from '@angular/platform-server'; | ||
` | ||
); | ||
|
||
await migration(tree); | ||
|
||
const content = tree.read(testTypeScriptFilePath, 'utf-8'); | ||
expect(content).not.toContain('@angular/platform-server'); | ||
expect(content).toContain( | ||
`import { Path, join } from '@angular-devkit/core';` | ||
); | ||
}); | ||
|
||
it(`should delete only 'renderModule' when there are additional exports`, async () => { | ||
tree.write( | ||
testTypeScriptFilePath, | ||
` | ||
import { Path, join } from '@angular-devkit/core'; | ||
export { renderModule, ServerModule } from '@angular/platform-server'; | ||
` | ||
); | ||
|
||
await migration(tree); | ||
|
||
const content = tree.read(testTypeScriptFilePath, 'utf-8'); | ||
|
||
expect(content).toContain( | ||
`import { Path, join } from '@angular-devkit/core';` | ||
); | ||
expect(content).toContain( | ||
`export { ServerModule } from '@angular/platform-server';` | ||
); | ||
}); | ||
|
||
it(`should not delete 'renderModule' when it's exported from another module`, async () => { | ||
tree.write( | ||
testTypeScriptFilePath, | ||
` | ||
export { renderModule } from '@angular/core'; | ||
` | ||
); | ||
|
||
await migration(tree); | ||
|
||
const content = tree.read(testTypeScriptFilePath, 'utf-8'); | ||
expect(content).toContain( | ||
`export { renderModule } from '@angular/core';` | ||
); | ||
}); | ||
|
||
it(`should not delete 'renderModule' when it's imported from '@angular/platform-server'`, async () => { | ||
tree.write( | ||
testTypeScriptFilePath, | ||
` | ||
import { renderModule } from '@angular/platform-server'; | ||
` | ||
); | ||
|
||
await migration(tree); | ||
|
||
const content = tree.read(testTypeScriptFilePath, 'utf-8'); | ||
expect(content).toContain( | ||
`import { renderModule } from '@angular/platform-server'` | ||
); | ||
}); | ||
}); | ||
}); |
87 changes: 87 additions & 0 deletions
87
...ages/angular/src/migrations/update-16-1-0/remove-render-module-platform-server-exports.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,87 @@ | ||
import type { Tree } from '@nx/devkit'; | ||
import { formatFiles, visitNotIgnoredFiles } from '@nx/devkit'; | ||
import * as ts from 'typescript'; | ||
import { FileChangeRecorder } from '../../utils/file-change-recorder'; | ||
|
||
export default async function (tree: Tree) { | ||
visitNotIgnoredFiles(tree, '/', (path) => { | ||
if (path.endsWith('.ts') && !path.endsWith('.d.ts')) { | ||
const content = tree.read(path, 'utf8'); | ||
if ( | ||
content.includes('@angular/platform-server') && | ||
content.includes('renderModule') | ||
) { | ||
const source = ts.createSourceFile( | ||
path, | ||
content.toString().replace(/^\uFEFF/, ''), | ||
ts.ScriptTarget.Latest, | ||
true | ||
); | ||
|
||
let recorder: FileChangeRecorder | undefined; | ||
let printer: ts.Printer | undefined; | ||
|
||
ts.forEachChild(source, function analyze(node) { | ||
if ( | ||
!( | ||
ts.isExportDeclaration(node) && | ||
node.moduleSpecifier && | ||
ts.isStringLiteral(node.moduleSpecifier) && | ||
node.moduleSpecifier.text === '@angular/platform-server' && | ||
node.exportClause && | ||
ts.isNamedExports(node.exportClause) | ||
) | ||
) { | ||
// Not a @angular/platform-server named export. | ||
return; | ||
} | ||
|
||
const exportClause = node.exportClause; | ||
const newElements: ts.ExportSpecifier[] = []; | ||
for (const element of exportClause.elements) { | ||
if (element.name.text !== 'renderModule') { | ||
newElements.push(element); | ||
} | ||
} | ||
|
||
if (newElements.length === exportClause.elements.length) { | ||
// No changes | ||
return; | ||
} | ||
|
||
recorder ??= new FileChangeRecorder(tree, path); | ||
|
||
if (newElements.length) { | ||
// Update named exports as there are leftovers. | ||
const newExportClause = ts.factory.updateNamedExports( | ||
exportClause, | ||
newElements | ||
); | ||
printer ??= ts.createPrinter(); | ||
const fix = printer.printNode( | ||
ts.EmitHint.Unspecified, | ||
newExportClause, | ||
source | ||
); | ||
|
||
const index = exportClause.getStart(); | ||
const length = exportClause.getWidth(); | ||
recorder.remove(index, index + length); | ||
recorder.insertLeft(index, fix); | ||
} else { | ||
// Delete export as no exports remain. | ||
recorder.remove(node.getStart(), node.getStart() + node.getWidth()); | ||
} | ||
|
||
ts.forEachChild(node, analyze); | ||
}); | ||
|
||
if (recorder) { | ||
recorder.applyChanges(); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
await formatFiles(tree); | ||
} |