2018-07-16 03:51:14 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2018-08-22 15:33:17 -04:00
|
|
|
import * as fs from 'fs';
|
2018-07-16 03:51:14 -04:00
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
2018-08-22 15:33:17 -04:00
|
|
|
import {DtsMapper} from '../../src/host/dts_mapper';
|
|
|
|
import {Esm2015ReflectionHost} from '../../src/host/esm2015_host';
|
|
|
|
import {getDeclaration, makeProgram} from '../helpers/utils';
|
2018-07-16 03:51:14 -04:00
|
|
|
|
2018-08-22 15:33:17 -04:00
|
|
|
const CLASSES = [
|
2018-07-16 03:51:14 -04:00
|
|
|
{
|
2018-08-22 15:33:17 -04:00
|
|
|
name: '/src/class.js',
|
2018-07-16 03:51:14 -04:00
|
|
|
contents: `
|
2018-08-22 15:33:17 -04:00
|
|
|
export class NoTypeParam {}
|
|
|
|
export class OneTypeParam {}
|
|
|
|
export class TwoTypeParams {}
|
2018-07-16 03:51:14 -04:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2018-08-22 15:33:17 -04:00
|
|
|
name: '/typings/class.d.ts',
|
2018-07-16 03:51:14 -04:00
|
|
|
contents: `
|
2018-08-22 15:33:17 -04:00
|
|
|
export class NoTypeParam {}
|
|
|
|
export class OneTypeParam<T> {}
|
|
|
|
export class TwoTypeParams<T, K> {}
|
2018-07-16 03:51:14 -04:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2018-08-17 02:50:55 -04:00
|
|
|
const MARKER_FILE = {
|
|
|
|
name: '/marker.js',
|
|
|
|
contents: `
|
refactor(ivy): obviate the Bazel component of the ivy_switch (#26550)
Originally, the ivy_switch mechanism used Bazel genrules to conditionally
compile one TS file or another depending on whether ngc or ngtsc was the
selected compiler. This was done because we wanted to avoid importing
certain modules (and thus pulling them into the build) if Ivy was on or
off. This mechanism had a major drawback: ivy_switch became a bottleneck
in the import graph, as it both imports from many places in the codebase
and is imported by many modules in the codebase. This frequently resulted
in cyclic imports which caused issues both with TS and Closure compilation.
It turns out ngcc needs both code paths in the bundle to perform the switch
during its operation anyway, so import switching was later abandoned. This
means that there's no real reason why the ivy_switch mechanism needed to
operate at the Bazel level, and for the ivy_switch file to be a bottleneck.
This commit removes the Bazel-level ivy_switch mechanism, and introduces
an additional TypeScript transform in ngtsc (and the pass-through tsc
compiler used for testing JIT) to perform the same operation that ngcc
does, and flip the switch during ngtsc compilation. This allows the
ivy_switch file to be removed, and the individual switches to be located
directly next to their consumers in the codebase, greatly mitigating the
circular import issues and making the mechanism much easier to use.
As part of this commit, the tag for marking switched variables was changed
from __PRE_NGCC__ to __PRE_R3__, since it's no longer just ngcc which
flips these tags. Most variables were renamed from R3_* to SWITCH_* as well,
since they're referenced mostly in render2 code.
Test strategy: existing test coverage is more than sufficient - if this
didn't work correctly it would break the hello world and todo apps.
PR Close #26550
2018-10-17 18:44:44 -04:00
|
|
|
let compileNgModuleFactory = compileNgModuleFactory__PRE_R3__;
|
2018-08-17 02:50:55 -04:00
|
|
|
|
refactor(ivy): obviate the Bazel component of the ivy_switch (#26550)
Originally, the ivy_switch mechanism used Bazel genrules to conditionally
compile one TS file or another depending on whether ngc or ngtsc was the
selected compiler. This was done because we wanted to avoid importing
certain modules (and thus pulling them into the build) if Ivy was on or
off. This mechanism had a major drawback: ivy_switch became a bottleneck
in the import graph, as it both imports from many places in the codebase
and is imported by many modules in the codebase. This frequently resulted
in cyclic imports which caused issues both with TS and Closure compilation.
It turns out ngcc needs both code paths in the bundle to perform the switch
during its operation anyway, so import switching was later abandoned. This
means that there's no real reason why the ivy_switch mechanism needed to
operate at the Bazel level, and for the ivy_switch file to be a bottleneck.
This commit removes the Bazel-level ivy_switch mechanism, and introduces
an additional TypeScript transform in ngtsc (and the pass-through tsc
compiler used for testing JIT) to perform the same operation that ngcc
does, and flip the switch during ngtsc compilation. This allows the
ivy_switch file to be removed, and the individual switches to be located
directly next to their consumers in the codebase, greatly mitigating the
circular import issues and making the mechanism much easier to use.
As part of this commit, the tag for marking switched variables was changed
from __PRE_NGCC__ to __PRE_R3__, since it's no longer just ngcc which
flips these tags. Most variables were renamed from R3_* to SWITCH_* as well,
since they're referenced mostly in render2 code.
Test strategy: existing test coverage is more than sufficient - if this
didn't work correctly it would break the hello world and todo apps.
PR Close #26550
2018-10-17 18:44:44 -04:00
|
|
|
function compileNgModuleFactory__PRE_R3__(injector, options, moduleType) {
|
2018-08-17 02:50:55 -04:00
|
|
|
const compilerFactory = injector.get(CompilerFactory);
|
|
|
|
const compiler = compilerFactory.createCompiler([options]);
|
|
|
|
return compiler.compileModuleAsync(moduleType);
|
|
|
|
}
|
|
|
|
|
refactor(ivy): obviate the Bazel component of the ivy_switch (#26550)
Originally, the ivy_switch mechanism used Bazel genrules to conditionally
compile one TS file or another depending on whether ngc or ngtsc was the
selected compiler. This was done because we wanted to avoid importing
certain modules (and thus pulling them into the build) if Ivy was on or
off. This mechanism had a major drawback: ivy_switch became a bottleneck
in the import graph, as it both imports from many places in the codebase
and is imported by many modules in the codebase. This frequently resulted
in cyclic imports which caused issues both with TS and Closure compilation.
It turns out ngcc needs both code paths in the bundle to perform the switch
during its operation anyway, so import switching was later abandoned. This
means that there's no real reason why the ivy_switch mechanism needed to
operate at the Bazel level, and for the ivy_switch file to be a bottleneck.
This commit removes the Bazel-level ivy_switch mechanism, and introduces
an additional TypeScript transform in ngtsc (and the pass-through tsc
compiler used for testing JIT) to perform the same operation that ngcc
does, and flip the switch during ngtsc compilation. This allows the
ivy_switch file to be removed, and the individual switches to be located
directly next to their consumers in the codebase, greatly mitigating the
circular import issues and making the mechanism much easier to use.
As part of this commit, the tag for marking switched variables was changed
from __PRE_NGCC__ to __PRE_R3__, since it's no longer just ngcc which
flips these tags. Most variables were renamed from R3_* to SWITCH_* as well,
since they're referenced mostly in render2 code.
Test strategy: existing test coverage is more than sufficient - if this
didn't work correctly it would break the hello world and todo apps.
PR Close #26550
2018-10-17 18:44:44 -04:00
|
|
|
function compileNgModuleFactory__POST_R3__(injector, options, moduleType) {
|
2018-08-17 02:50:55 -04:00
|
|
|
ngDevMode && assertNgModuleType(moduleType);
|
|
|
|
return Promise.resolve(new R3NgModuleFactory(moduleType));
|
|
|
|
}
|
|
|
|
`
|
|
|
|
};
|
|
|
|
|
2018-09-26 12:24:43 -04:00
|
|
|
const DECORATED_FILES = [
|
|
|
|
{
|
|
|
|
name: '/primary.js',
|
|
|
|
contents: `
|
|
|
|
import {Directive} from '@angular/core';
|
|
|
|
class A {}
|
|
|
|
A.decorators = [
|
|
|
|
{ type: Directive, args: [{ selector: '[a]' }] }
|
|
|
|
];
|
|
|
|
function x() {}
|
|
|
|
function y() {}
|
|
|
|
class B {}
|
|
|
|
B.decorators = [
|
|
|
|
{ type: Directive, args: [{ selector: '[b]' }] }
|
|
|
|
];
|
|
|
|
class C {}
|
|
|
|
export { A, x, C };
|
|
|
|
export { D } from '/secondary';
|
|
|
|
`
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: '/secondary.js',
|
|
|
|
contents: `
|
|
|
|
import {Directive} from '@angular/core';
|
|
|
|
class D {}
|
|
|
|
D.decorators = [
|
|
|
|
{ type: Directive, args: [{ selector: '[d]' }] }
|
|
|
|
];
|
|
|
|
export { D };
|
|
|
|
`
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2018-07-16 03:51:14 -04:00
|
|
|
describe('Esm2015ReflectionHost', () => {
|
2018-08-22 15:33:17 -04:00
|
|
|
describe('getGenericArityOfClass()', () => {
|
|
|
|
it('should properly count type parameters', () => {
|
|
|
|
// Mock out reading the `d.ts` file from disk
|
|
|
|
const readFileSyncSpy = spyOn(fs, 'readFileSync').and.returnValue(CLASSES[1].contents);
|
|
|
|
const program = makeProgram(CLASSES[0]);
|
|
|
|
|
|
|
|
const dtsMapper = new DtsMapper('/src', '/typings');
|
2018-10-03 11:59:32 -04:00
|
|
|
const host = new Esm2015ReflectionHost(false, program.getTypeChecker(), dtsMapper);
|
2018-08-22 15:33:17 -04:00
|
|
|
const noTypeParamClass =
|
|
|
|
getDeclaration(program, '/src/class.js', 'NoTypeParam', ts.isClassDeclaration);
|
|
|
|
expect(host.getGenericArityOfClass(noTypeParamClass)).toBe(0);
|
|
|
|
const oneTypeParamClass =
|
|
|
|
getDeclaration(program, '/src/class.js', 'OneTypeParam', ts.isClassDeclaration);
|
|
|
|
expect(host.getGenericArityOfClass(oneTypeParamClass)).toBe(1);
|
|
|
|
const twoTypeParamsClass =
|
|
|
|
getDeclaration(program, '/src/class.js', 'TwoTypeParams', ts.isClassDeclaration);
|
|
|
|
expect(host.getGenericArityOfClass(twoTypeParamsClass)).toBe(2);
|
2018-07-16 03:51:14 -04:00
|
|
|
});
|
|
|
|
});
|
2018-08-17 02:50:55 -04:00
|
|
|
|
|
|
|
describe('getSwitchableDeclarations()', () => {
|
|
|
|
it('should return a collection of all the switchable variable declarations in the given module',
|
|
|
|
() => {
|
|
|
|
const program = makeProgram(MARKER_FILE);
|
|
|
|
const dtsMapper = new DtsMapper('/src', '/typings');
|
2018-10-03 11:59:32 -04:00
|
|
|
const host = new Esm2015ReflectionHost(false, program.getTypeChecker(), dtsMapper);
|
2018-08-17 02:50:55 -04:00
|
|
|
const file = program.getSourceFile(MARKER_FILE.name) !;
|
|
|
|
const declarations = host.getSwitchableDeclarations(file);
|
|
|
|
expect(declarations.map(d => [d.name.getText(), d.initializer !.getText()])).toEqual([
|
refactor(ivy): obviate the Bazel component of the ivy_switch (#26550)
Originally, the ivy_switch mechanism used Bazel genrules to conditionally
compile one TS file or another depending on whether ngc or ngtsc was the
selected compiler. This was done because we wanted to avoid importing
certain modules (and thus pulling them into the build) if Ivy was on or
off. This mechanism had a major drawback: ivy_switch became a bottleneck
in the import graph, as it both imports from many places in the codebase
and is imported by many modules in the codebase. This frequently resulted
in cyclic imports which caused issues both with TS and Closure compilation.
It turns out ngcc needs both code paths in the bundle to perform the switch
during its operation anyway, so import switching was later abandoned. This
means that there's no real reason why the ivy_switch mechanism needed to
operate at the Bazel level, and for the ivy_switch file to be a bottleneck.
This commit removes the Bazel-level ivy_switch mechanism, and introduces
an additional TypeScript transform in ngtsc (and the pass-through tsc
compiler used for testing JIT) to perform the same operation that ngcc
does, and flip the switch during ngtsc compilation. This allows the
ivy_switch file to be removed, and the individual switches to be located
directly next to their consumers in the codebase, greatly mitigating the
circular import issues and making the mechanism much easier to use.
As part of this commit, the tag for marking switched variables was changed
from __PRE_NGCC__ to __PRE_R3__, since it's no longer just ngcc which
flips these tags. Most variables were renamed from R3_* to SWITCH_* as well,
since they're referenced mostly in render2 code.
Test strategy: existing test coverage is more than sufficient - if this
didn't work correctly it would break the hello world and todo apps.
PR Close #26550
2018-10-17 18:44:44 -04:00
|
|
|
['compileNgModuleFactory', 'compileNgModuleFactory__PRE_R3__']
|
2018-08-17 02:50:55 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2018-09-26 12:24:43 -04:00
|
|
|
|
|
|
|
describe('findDecoratedFiles()', () => {
|
|
|
|
it('should return an array of objects for each file that has exported and decorated classes',
|
|
|
|
() => {
|
|
|
|
const program = makeProgram(...DECORATED_FILES);
|
|
|
|
const dtsMapper = new DtsMapper('/src', '/typings');
|
|
|
|
const host = new Esm2015ReflectionHost(false, program.getTypeChecker(), dtsMapper);
|
2018-09-28 09:57:50 -04:00
|
|
|
const primaryFile = program.getSourceFile(DECORATED_FILES[0].name) !;
|
|
|
|
const secondaryFile = program.getSourceFile(DECORATED_FILES[1].name) !;
|
|
|
|
const decoratedFiles = host.findDecoratedFiles(primaryFile);
|
|
|
|
|
|
|
|
expect(decoratedFiles.size).toEqual(2);
|
|
|
|
|
|
|
|
const primary = decoratedFiles.get(primaryFile) !;
|
2018-09-26 12:24:43 -04:00
|
|
|
expect(primary.decoratedClasses.length).toEqual(1);
|
|
|
|
const classA = primary.decoratedClasses.find(c => c.name === 'A') !;
|
|
|
|
expect(classA.name).toEqual('A');
|
|
|
|
expect(ts.isClassDeclaration(classA.declaration)).toBeTruthy();
|
|
|
|
expect(classA.decorators.map(decorator => decorator.name)).toEqual(['Directive']);
|
2018-09-28 09:57:50 -04:00
|
|
|
|
|
|
|
const secondary = decoratedFiles.get(secondaryFile) !;
|
2018-09-26 12:24:43 -04:00
|
|
|
expect(secondary.decoratedClasses.length).toEqual(1);
|
|
|
|
const classD = secondary.decoratedClasses.find(c => c.name === 'D') !;
|
|
|
|
expect(classD.name).toEqual('D');
|
|
|
|
expect(ts.isClassDeclaration(classD.declaration)).toBeTruthy();
|
|
|
|
expect(classD.decorators.map(decorator => decorator.name)).toEqual(['Directive']);
|
|
|
|
});
|
|
|
|
});
|
2018-07-16 03:51:14 -04:00
|
|
|
});
|