feat(ivy): implement Renderer.getSwitchableDeclarations
(#25534)
This supports the "ngcc ivy switch" specified in #25238. PR Close #25534
This commit is contained in:

committed by
Misko Hevery

parent
e73e864f87
commit
26cd9f5433
@ -46,6 +46,19 @@ export class C {}
|
||||
C.decorators = [
|
||||
{ type: Directive, args: [{ selector: '[c]' }] },
|
||||
];
|
||||
let compileNgModuleFactory = compileNgModuleFactory__PRE_NGCC__;
|
||||
let badlyFormattedVariable = __PRE_NGCC__badlyFormattedVariable;
|
||||
|
||||
function compileNgModuleFactory__PRE_NGCC__(injector, options, moduleType) {
|
||||
const compilerFactory = injector.get(CompilerFactory);
|
||||
const compiler = compilerFactory.createCompiler([options]);
|
||||
return compiler.compileModuleAsync(moduleType);
|
||||
}
|
||||
|
||||
function compileNgModuleFactory__POST_NGCC__(injector, options, moduleType) {
|
||||
ngDevMode && assertNgModuleType(moduleType);
|
||||
return Promise.resolve(new R3NgModuleFactory(moduleType));
|
||||
}
|
||||
// Some other content`
|
||||
};
|
||||
|
||||
@ -82,6 +95,29 @@ export class A {}`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('rewriteSwitchableDeclarations', () => {
|
||||
it('should switch marked declaration initializers', () => {
|
||||
const {renderer, program} = setup(PROGRAM);
|
||||
const file = program.getSourceFile('some/file.js');
|
||||
if (file === undefined) {
|
||||
throw new Error(`Could not find source file`);
|
||||
}
|
||||
const output = new MagicString(PROGRAM.contents);
|
||||
renderer.rewriteSwitchableDeclarations(output, file);
|
||||
expect(output.toString())
|
||||
.not.toContain(`let compileNgModuleFactory = compileNgModuleFactory__PRE_NGCC__;`);
|
||||
expect(output.toString())
|
||||
.toContain(`let badlyFormattedVariable = __PRE_NGCC__badlyFormattedVariable;`);
|
||||
expect(output.toString())
|
||||
.toContain(`let compileNgModuleFactory = compileNgModuleFactory__POST_NGCC__;`);
|
||||
expect(output.toString())
|
||||
.toContain(
|
||||
`function compileNgModuleFactory__PRE_NGCC__(injector, options, moduleType) {`);
|
||||
expect(output.toString())
|
||||
.toContain(
|
||||
`function compileNgModuleFactory__POST_NGCC__(injector, options, moduleType) {`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('addDefinitions', () => {
|
||||
it('should insert the definitions directly after the class declaration', () => {
|
||||
|
@ -29,6 +29,9 @@ class TestRenderer extends Renderer {
|
||||
removeDecorators(output: MagicString, decoratorsToRemove: Map<ts.Node, ts.Node[]>) {
|
||||
output.prepend('\n// REMOVE DECORATORS\n');
|
||||
}
|
||||
rewriteSwitchableDeclarations(output: MagicString, sourceFile: ts.SourceFile): void {
|
||||
output.prepend('\n// REWRITTEN DECLARATIONS\n');
|
||||
}
|
||||
}
|
||||
|
||||
function createTestRenderer() {
|
||||
@ -68,7 +71,7 @@ describe('Renderer', () => {
|
||||
]
|
||||
});
|
||||
const RENDERED_CONTENTS =
|
||||
`\n// REMOVE DECORATORS\n\n// ADD IMPORTS\n\n// ADD CONSTANTS\n\n// ADD DEFINITIONS\n` +
|
||||
`\n// REWRITTEN DECLARATIONS\n\n// REMOVE DECORATORS\n\n// ADD IMPORTS\n\n// ADD CONSTANTS\n\n// ADD DEFINITIONS\n` +
|
||||
INPUT_PROGRAM.contents;
|
||||
const OUTPUT_PROGRAM_MAP = fromObject({
|
||||
'version': 3,
|
||||
@ -78,14 +81,14 @@ describe('Renderer', () => {
|
||||
'import { Directive } from \'@angular/core\';\nexport class A {\n foo(x) {\n return x;\n }\n}\nA.decorators = [\n { type: Directive, args: [{ selector: \'[a]\' }] }\n];\n'
|
||||
],
|
||||
'names': [],
|
||||
'mappings': ';;;;;;;;AAAA;;;;;;;;;'
|
||||
'mappings': ';;;;;;;;;;AAAA;;;;;;;;;'
|
||||
});
|
||||
|
||||
const MERGED_OUTPUT_PROGRAM_MAP = fromObject({
|
||||
'version': 3,
|
||||
'sources': ['/file.ts'],
|
||||
'names': [],
|
||||
'mappings': ';;;;;;;;AAAA',
|
||||
'mappings': ';;;;;;;;;;AAAA',
|
||||
'file': '/output_file.js',
|
||||
'sourcesContent': [
|
||||
'import { Directive } from \'@angular/core\';\nexport class A {\n foo(x: string): string {\n return x;\n }\n static decorators = [\n { type: Directive, args: [{ selector: \'[a]\' }] }\n ];\n}'
|
||||
|
Reference in New Issue
Block a user