-
Notifications
You must be signed in to change notification settings - Fork 12k
Ivy app shell #15517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Ivy app shell #15517
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
69422b1
feat(@angular-devkit/build-angular): enable bundleDependencies by def…
alan-agius4 61ff8e2
feat(@schematics/angular): add export to `renderModule` in server mai…
alan-agius4 5cc4734
fix(@angular-devkit/build-angular): make app-shell work with Ivy
alan-agius4 9263fba
test: run ivy-ngcc binary to reduce flakes
alan-agius4 155dea8
test: add production app-shell test
alan-agius4 8fb8e59
feat(@schematics/angular): add export to `renderModuleFactory` in ser…
alan-agius4 7a80959
feat(@schematics/angular): add migration to add missing exports in ma…
alan-agius4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
143 changes: 143 additions & 0 deletions
143
packages/schematics/angular/migrations/update-9/update-server-main-file.ts
This file contains hidden or 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,143 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. 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.io/license | ||
*/ | ||
import { Rule } from '@angular-devkit/schematics'; | ||
import * as ts from '../../third_party/github.com/Microsoft/TypeScript/lib/typescript'; | ||
import { findNodes } from '../../utility/ast-utils'; | ||
import { findPropertyInAstObject } from '../../utility/json-utils'; | ||
import { Builders } from '../../utility/workspace-models'; | ||
import { getTargets, getWorkspace } from './utils'; | ||
|
||
/** | ||
* Update the `main.server.ts` file by adding exports to `renderModule` and `renderModuleFactory` which are | ||
* now required for Universal and App-Shell for Ivy and `bundleDependencies`. | ||
*/ | ||
export function updateServerMainFile(): Rule { | ||
return tree => { | ||
const workspace = getWorkspace(tree); | ||
|
||
for (const { target } of getTargets(workspace, 'server', Builders.Server)) { | ||
const options = findPropertyInAstObject(target, 'options'); | ||
if (!options || options.kind !== 'object') { | ||
continue; | ||
} | ||
|
||
// find the main server file | ||
const mainFile = findPropertyInAstObject(options, 'main'); | ||
if (!mainFile || typeof mainFile.value !== 'string') { | ||
continue; | ||
} | ||
|
||
const mainFilePath = mainFile.value; | ||
|
||
const content = tree.read(mainFilePath); | ||
if (!content) { | ||
continue; | ||
} | ||
|
||
const source = ts.createSourceFile( | ||
mainFilePath, | ||
content.toString().replace(/^\uFEFF/, ''), | ||
ts.ScriptTarget.Latest, | ||
true, | ||
); | ||
|
||
// find exports in main server file | ||
const exportDeclarations = findNodes(source, ts.SyntaxKind.ExportDeclaration) as ts.ExportDeclaration[]; | ||
|
||
const platformServerExports = exportDeclarations.filter(({ moduleSpecifier }) => ( | ||
moduleSpecifier && ts.isStringLiteral(moduleSpecifier) && moduleSpecifier.text === '@angular/platform-server' | ||
)); | ||
|
||
let hasRenderModule = false; | ||
let hasRenderModuleFactory = false; | ||
|
||
// find exports of renderModule or renderModuleFactory | ||
for (const { exportClause } of platformServerExports) { | ||
if (exportClause && ts.isNamedExports(exportClause)) { | ||
if (!hasRenderModuleFactory) { | ||
hasRenderModuleFactory = exportClause.elements.some(({ name }) => name.text === 'renderModuleFactory'); | ||
} | ||
|
||
if (!hasRenderModule) { | ||
hasRenderModule = exportClause.elements.some(({ name }) => name.text === 'renderModule'); | ||
} | ||
} | ||
} | ||
|
||
if (hasRenderModule && hasRenderModuleFactory) { | ||
// We have both required exports | ||
continue; | ||
} | ||
|
||
let exportSpecifiers: ts.ExportSpecifier[] = []; | ||
let updateExisting = false; | ||
|
||
// Add missing exports | ||
if (platformServerExports.length) { | ||
const { exportClause } = platformServerExports[0] as ts.ExportDeclaration; | ||
if (!exportClause) { | ||
continue; | ||
} | ||
|
||
exportSpecifiers = [...exportClause.elements]; | ||
updateExisting = true; | ||
} | ||
|
||
if (!hasRenderModule) { | ||
exportSpecifiers.push(ts.createExportSpecifier( | ||
undefined, | ||
ts.createIdentifier('renderModule'), | ||
)); | ||
} | ||
|
||
if (!hasRenderModuleFactory) { | ||
exportSpecifiers.push(ts.createExportSpecifier( | ||
undefined, | ||
ts.createIdentifier('renderModuleFactory'), | ||
)); | ||
} | ||
|
||
// Create a TS printer to get the text of the export node | ||
const printer = ts.createPrinter(); | ||
|
||
const moduleSpecifier = ts.createStringLiteral('@angular/platform-server'); | ||
|
||
// TypeScript will emit the Node with double quotes. | ||
// In schematics we usually write code with a single quotes | ||
// tslint:disable-next-line: no-any | ||
(moduleSpecifier as any).singleQuote = true; | ||
|
||
const newExportDeclarationText = printer.printNode( | ||
ts.EmitHint.Unspecified, | ||
ts.createExportDeclaration( | ||
undefined, | ||
undefined, | ||
ts.createNamedExports(exportSpecifiers), | ||
moduleSpecifier, | ||
), | ||
source, | ||
); | ||
|
||
const recorder = tree.beginUpdate(mainFilePath); | ||
if (updateExisting) { | ||
const start = platformServerExports[0].getStart(); | ||
const width = platformServerExports[0].getWidth(); | ||
|
||
recorder | ||
.remove(start, width) | ||
.insertLeft(start, newExportDeclarationText); | ||
} else { | ||
recorder.insertLeft(source.getWidth(), '\n' + newExportDeclarationText); | ||
} | ||
|
||
tree.commitUpdate(recorder); | ||
} | ||
|
||
return tree; | ||
}; | ||
} |
108 changes: 108 additions & 0 deletions
108
packages/schematics/angular/migrations/update-9/update-server-main-file_spec.ts
This file contains hidden or 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,108 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. 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.io/license | ||
*/ | ||
|
||
import { tags } from '@angular-devkit/core'; | ||
import { EmptyTree } from '@angular-devkit/schematics'; | ||
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
|
||
const mainServerContent = tags.stripIndents` | ||
import { enableProdMode } from '@angular/core'; | ||
|
||
import { environment } from './environments/environment'; | ||
|
||
if (environment.production) { | ||
enableProdMode(); | ||
} | ||
|
||
export { AppServerModule } from './app/app.server.module'; | ||
`; | ||
|
||
const mainServerFile = 'src/main.server.ts'; | ||
|
||
describe('Migration to version 9', () => { | ||
describe('Migrate Server Main File', () => { | ||
const schematicRunner = new SchematicTestRunner( | ||
'migrations', | ||
require.resolve('../migration-collection.json'), | ||
); | ||
|
||
let tree: UnitTestTree; | ||
|
||
beforeEach(async () => { | ||
tree = new UnitTestTree(new EmptyTree()); | ||
tree = await schematicRunner | ||
.runExternalSchematicAsync( | ||
require.resolve('../../collection.json'), | ||
'ng-new', | ||
{ | ||
name: 'migration-test', | ||
version: '1.2.3', | ||
directory: '.', | ||
}, | ||
tree, | ||
) | ||
.toPromise(); | ||
tree = await schematicRunner | ||
.runExternalSchematicAsync( | ||
require.resolve('../../collection.json'), | ||
'universal', | ||
{ | ||
clientProject: 'migration-test', | ||
}, | ||
tree, | ||
) | ||
.toPromise(); | ||
}); | ||
|
||
it(`should add exports from '@angular/platform-server'`, async () => { | ||
tree.overwrite(mainServerFile, mainServerContent); | ||
const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise(); | ||
expect(tree2.readContent(mainServerFile)).toContain(tags.stripIndents` | ||
export { AppServerModule } from './app/app.server.module'; | ||
export { renderModule, renderModuleFactory } from '@angular/platform-server'; | ||
`); | ||
}); | ||
|
||
it(`should add 'renderModule' and 'renderModuleFactory' to existing '@angular/platform-server' export`, async () => { | ||
tree.overwrite(mainServerFile, tags.stripIndents` | ||
${mainServerContent} | ||
export { platformDynamicServer } from '@angular/platform-server'; | ||
export { PlatformConfig } from '@angular/platform-server'; | ||
`); | ||
const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise(); | ||
expect(tree2.readContent(mainServerFile)).toContain(tags.stripIndents` | ||
export { AppServerModule } from './app/app.server.module'; | ||
export { platformDynamicServer, renderModule, renderModuleFactory } from '@angular/platform-server'; | ||
export { PlatformConfig } from '@angular/platform-server'; | ||
`); | ||
}); | ||
|
||
it(`should add 'renderModule' to existing '@angular/platform-server' export`, async () => { | ||
tree.overwrite(mainServerFile, tags.stripIndents` | ||
${mainServerContent} | ||
export { platformDynamicServer, renderModuleFactory } from '@angular/platform-server'; | ||
`); | ||
const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise(); | ||
expect(tree2.readContent(mainServerFile)).toContain(tags.stripIndents` | ||
export { AppServerModule } from './app/app.server.module'; | ||
export { platformDynamicServer, renderModuleFactory, renderModule } from '@angular/platform-server'; | ||
`); | ||
}); | ||
|
||
it(`should not update exports when 'renderModule' and 'renderModuleFactory' are already exported`, async () => { | ||
const input = tags.stripIndents` | ||
${mainServerContent} | ||
export { renderModule, renderModuleFactory } from '@angular/platform-server'; | ||
`; | ||
|
||
tree.overwrite(mainServerFile, input); | ||
const tree2 = await schematicRunner.runSchematicAsync('migration-09', {}, tree.branch()).toPromise(); | ||
expect(tree2.readContent(mainServerFile)).toBe(input); | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.