2018-07-16 05:23:37 -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-10-04 07:19:11 -04:00
|
|
|
import {dirname} from 'canonical-path';
|
2018-07-16 05:23:37 -04:00
|
|
|
import MagicString from 'magic-string';
|
2018-11-25 17:07:51 -05:00
|
|
|
import * as ts from 'typescript';
|
2019-03-20 09:47:58 -04:00
|
|
|
import {AbsoluteFsPath} from '../../../src/ngtsc/path';
|
2018-09-26 15:53:45 -04:00
|
|
|
import {DecorationAnalyzer} from '../../src/analysis/decoration_analyzer';
|
2018-11-25 16:40:25 -05:00
|
|
|
import {NgccReferencesRegistry} from '../../src/analysis/ngcc_references_registry';
|
2018-10-04 07:19:11 -04:00
|
|
|
import {SwitchMarkerAnalyzer} from '../../src/analysis/switch_marker_analyzer';
|
2018-10-10 09:17:32 -04:00
|
|
|
import {Esm2015ReflectionHost} from '../../src/host/esm2015_host';
|
2018-10-10 12:52:55 -04:00
|
|
|
import {EsmRenderer} from '../../src/rendering/esm_renderer';
|
2018-11-25 17:07:51 -05:00
|
|
|
import {makeTestEntryPointBundle} from '../helpers/utils';
|
2019-03-29 06:13:14 -04:00
|
|
|
import {MockLogger} from '../helpers/mock_logger';
|
2018-07-16 05:23:37 -04:00
|
|
|
|
2018-11-25 16:40:25 -05:00
|
|
|
function setup(file: {name: string, contents: string}) {
|
2019-03-29 06:13:14 -04:00
|
|
|
const logger = new MockLogger();
|
2018-10-04 07:19:11 -04:00
|
|
|
const dir = dirname(file.name);
|
2019-03-20 09:47:59 -04:00
|
|
|
const bundle = makeTestEntryPointBundle('es2015', 'esm2015', false, [file]) !;
|
2018-11-25 16:40:25 -05:00
|
|
|
const typeChecker = bundle.src.program.getTypeChecker();
|
2019-03-29 06:13:14 -04:00
|
|
|
const host = new Esm2015ReflectionHost(logger, false, typeChecker);
|
2018-11-25 16:40:25 -05:00
|
|
|
const referencesRegistry = new NgccReferencesRegistry(host);
|
feat(ivy): use fileNameToModuleName to emit imports when it's available (#28523)
The ultimate goal of this commit is to make use of fileNameToModuleName to
get the module specifier to use when generating an import, when that API is
available in the CompilerHost that ngtsc is created with.
As part of getting there, the way in which ngtsc tracks references and
generates import module specifiers is refactored considerably. References
are tracked with the Reference class, and previously ngtsc had several
different kinds of Reference. An AbsoluteReference represented a declaration
which needed to be imported via an absolute module specifier tracked in the
AbsoluteReference, and a RelativeReference represented a declaration from
the local program, imported via relative path or referred to directly by
identifier if possible. Thus, how to refer to a particular declaration was
encoded into the Reference type _at the time of creation of the Reference_.
This commit refactors that logic and reduces Reference to a single class
with no subclasses. A Reference represents a node being referenced, plus
context about how the node was located. This context includes a
"bestGuessOwningModule", the compiler's best guess at which absolute
module specifier has defined this reference. For example, if the compiler
arrives at the declaration of CommonModule via an import to @angular/common,
then any references obtained from CommonModule (e.g. NgIf) will also be
considered to be owned by @angular/common.
A ReferenceEmitter class and accompanying ReferenceEmitStrategy interface
are introduced. To produce an Expression referring to a given Reference'd
node, the ReferenceEmitter consults a sequence of ReferenceEmitStrategy
implementations.
Several different strategies are defined:
- LocalIdentifierStrategy: use local ts.Identifiers if available.
- AbsoluteModuleStrategy: if the Reference has a bestGuessOwningModule,
import the node via an absolute import from that module specifier.
- LogicalProjectStrategy: if the Reference is in the logical project
(is under the project rootDirs), import the node via a relative import.
- FileToModuleStrategy: use a FileToModuleHost to generate the module
specifier by which to import the node.
Depending on the availability of fileNameToModuleName in the CompilerHost,
then, a different collection of these strategies is used for compilation.
PR Close #28523
2019-02-01 20:24:21 -05:00
|
|
|
const decorationAnalyses =
|
|
|
|
new DecorationAnalyzer(
|
|
|
|
bundle.src.program, bundle.src.options, bundle.src.host, typeChecker, host,
|
|
|
|
referencesRegistry, [AbsoluteFsPath.fromUnchecked('/')], false)
|
|
|
|
.analyzeProgram();
|
2018-11-25 16:40:25 -05:00
|
|
|
const switchMarkerAnalyses = new SwitchMarkerAnalyzer(host).analyzeProgram(bundle.src.program);
|
2019-03-29 06:13:14 -04:00
|
|
|
const renderer = new EsmRenderer(logger, host, false, bundle, dir);
|
2018-11-25 16:40:25 -05:00
|
|
|
return {
|
|
|
|
host,
|
|
|
|
program: bundle.src.program,
|
|
|
|
sourceFile: bundle.src.file, renderer, decorationAnalyses, switchMarkerAnalyses
|
|
|
|
};
|
2018-07-16 05:23:37 -04:00
|
|
|
}
|
|
|
|
|
2018-08-17 02:35:01 -04:00
|
|
|
const PROGRAM = {
|
2018-11-25 16:40:25 -05:00
|
|
|
name: '/some/file.js',
|
2018-08-17 02:35:01 -04:00
|
|
|
contents: `
|
2018-07-16 05:23:37 -04:00
|
|
|
/* A copyright notice */
|
|
|
|
import {Directive} from '@angular/core';
|
|
|
|
export class A {}
|
|
|
|
A.decorators = [
|
|
|
|
{ type: Directive, args: [{ selector: '[a]' }] },
|
2018-08-17 02:35:01 -04:00
|
|
|
{ type: OtherA }
|
|
|
|
];
|
|
|
|
export class B {}
|
|
|
|
B.decorators = [
|
|
|
|
{ type: OtherB },
|
|
|
|
{ type: Directive, args: [{ selector: '[b]' }] }
|
|
|
|
];
|
|
|
|
export class C {}
|
|
|
|
C.decorators = [
|
|
|
|
{ type: Directive, args: [{ selector: '[c]' }] },
|
2018-07-16 05:23:37 -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
|
|
|
let compileNgModuleFactory = compileNgModuleFactory__PRE_R3__;
|
|
|
|
let badlyFormattedVariable = __PRE_R3__badlyFormattedVariable;
|
2018-08-17 02:50:45 -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:45 -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:45 -04:00
|
|
|
ngDevMode && assertNgModuleType(moduleType);
|
|
|
|
return Promise.resolve(new R3NgModuleFactory(moduleType));
|
|
|
|
}
|
2018-07-16 05:23:37 -04:00
|
|
|
// Some other content`
|
2018-08-17 02:35:01 -04:00
|
|
|
};
|
|
|
|
|
2018-09-24 15:50:25 -04:00
|
|
|
const PROGRAM_DECORATE_HELPER = {
|
2018-11-25 16:40:25 -05:00
|
|
|
name: '/some/file.js',
|
2018-09-24 15:50:25 -04:00
|
|
|
contents: `
|
|
|
|
import * as tslib_1 from "tslib";
|
|
|
|
var D_1;
|
|
|
|
/* A copyright notice */
|
|
|
|
import { Directive } from '@angular/core';
|
|
|
|
const OtherA = () => (node) => { };
|
|
|
|
const OtherB = () => (node) => { };
|
|
|
|
let A = class A {
|
|
|
|
};
|
|
|
|
A = tslib_1.__decorate([
|
|
|
|
Directive({ selector: '[a]' }),
|
|
|
|
OtherA()
|
|
|
|
], A);
|
|
|
|
export { A };
|
|
|
|
let B = class B {
|
|
|
|
};
|
|
|
|
B = tslib_1.__decorate([
|
|
|
|
OtherB(),
|
|
|
|
Directive({ selector: '[b]' })
|
|
|
|
], B);
|
|
|
|
export { B };
|
|
|
|
let C = class C {
|
|
|
|
};
|
|
|
|
C = tslib_1.__decorate([
|
|
|
|
Directive({ selector: '[c]' })
|
|
|
|
], C);
|
|
|
|
export { C };
|
|
|
|
let D = D_1 = class D {
|
|
|
|
};
|
|
|
|
D = D_1 = tslib_1.__decorate([
|
|
|
|
Directive({ selector: '[d]', providers: [D_1] })
|
|
|
|
], D);
|
|
|
|
export { D };
|
|
|
|
// Some other content`
|
|
|
|
};
|
|
|
|
|
2018-08-17 02:35:01 -04:00
|
|
|
describe('Esm2015Renderer', () => {
|
|
|
|
|
|
|
|
describe('addImports', () => {
|
|
|
|
it('should insert the given imports at the start of the source file', () => {
|
2018-07-16 05:23:37 -04:00
|
|
|
const {renderer} = setup(PROGRAM);
|
|
|
|
const output = new MagicString(PROGRAM.contents);
|
2019-03-06 19:35:08 -05:00
|
|
|
renderer.addImports(output, [
|
fix(ivy): reuse default imports in type-to-value references (#29266)
This fixes an issue with commit b6f6b117. In this commit, default imports
processed in a type-to-value conversion were recorded as non-local imports
with a '*' name, and the ImportManager generated a new default import for
them. When transpiled to ES2015 modules, this resulted in the following
correct code:
import i3 from './module';
// somewhere in the file, a value reference of i3:
{type: i3}
However, when the AST with this synthetic import and reference was
transpiled to non-ES2015 modules (for example, to commonjs) an issue
appeared:
var module_1 = require('./module');
{type: i3}
TypeScript renames the imported identifier from i3 to module_1, but doesn't
substitute later references to i3. This is because the import and reference
are both synthetic, and never went through the TypeScript AST step of
"binding" which associates the reference to its import. This association is
important during emit when the identifiers might change.
Synthetic (transformer-added) imports will never be bound properly. The only
possible solution is to reuse the user's original import and the identifier
from it, which will be properly downleveled. The issue with this approach
(which prompted the fix in b6f6b117) is that if the import is only used in a
type position, TypeScript will mark it for deletion in the generated JS,
even though additional non-type usages are added in the transformer. This
again would leave a dangling import.
To work around this, it's necessary for the compiler to keep track of
identifiers that it emits which came from default imports, and tell TS not
to remove those imports during transpilation. A `DefaultImportTracker` class
is implemented to perform this tracking. It implements a
`DefaultImportRecorder` interface, which is used to record two significant
pieces of information:
* when a WrappedNodeExpr is generated which refers to a default imported
value, the ts.Identifier is associated to the ts.ImportDeclaration via
the recorder.
* when that WrappedNodeExpr is later emitted as part of the statement /
expression translators, the fact that the ts.Identifier was used is
also recorded.
Combined, this tracking gives the `DefaultImportTracker` enough information
to implement another TS transformer, which can recognize default imports
which were used in the output of the Ivy transform and can prevent them
from being elided. This is done by creating a new ts.ImportDeclaration for
the imports with the same ts.ImportClause. A test verifies that this works.
PR Close #29266
2019-03-11 19:54:07 -04:00
|
|
|
{specifier: '@angular/core', qualifier: 'i0'},
|
|
|
|
{specifier: '@angular/common', qualifier: 'i1'}
|
2019-03-06 19:35:08 -05:00
|
|
|
]);
|
2018-08-17 02:35:01 -04:00
|
|
|
expect(output.toString()).toContain(`import * as i0 from '@angular/core';
|
|
|
|
import * as i1 from '@angular/common';
|
|
|
|
|
|
|
|
/* A copyright notice */`);
|
2018-07-16 05:23:37 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-11-25 17:07:51 -05:00
|
|
|
describe('addExports', () => {
|
|
|
|
it('should insert the given exports at the end of the source file', () => {
|
|
|
|
const {renderer} = setup(PROGRAM);
|
|
|
|
const output = new MagicString(PROGRAM.contents);
|
|
|
|
renderer.addExports(output, PROGRAM.name.replace(/\.js$/, ''), [
|
2019-02-14 12:59:46 -05:00
|
|
|
{from: '/some/a.js', dtsFrom: '/some/a.d.ts', identifier: 'ComponentA1'},
|
|
|
|
{from: '/some/a.js', dtsFrom: '/some/a.d.ts', identifier: 'ComponentA2'},
|
|
|
|
{from: '/some/foo/b.js', dtsFrom: '/some/foo/b.d.ts', identifier: 'ComponentB'},
|
|
|
|
{from: PROGRAM.name, dtsFrom: PROGRAM.name, identifier: 'TopLevelComponent'},
|
2018-11-25 17:07:51 -05:00
|
|
|
]);
|
|
|
|
expect(output.toString()).toContain(`
|
|
|
|
// Some other content
|
|
|
|
export {ComponentA1} from './a';
|
|
|
|
export {ComponentA2} from './a';
|
|
|
|
export {ComponentB} from './foo/b';
|
|
|
|
export {TopLevelComponent};`);
|
|
|
|
});
|
2019-02-14 12:59:46 -05:00
|
|
|
|
|
|
|
it('should not insert alias exports in js output', () => {
|
|
|
|
const {renderer} = setup(PROGRAM);
|
|
|
|
const output = new MagicString(PROGRAM.contents);
|
|
|
|
renderer.addExports(output, PROGRAM.name.replace(/\.js$/, ''), [
|
|
|
|
{from: '/some/a.js', alias: 'eComponentA1', identifier: 'ComponentA1'},
|
|
|
|
{from: '/some/a.js', alias: 'eComponentA2', identifier: 'ComponentA2'},
|
|
|
|
{from: '/some/foo/b.js', alias: 'eComponentB', identifier: 'ComponentB'},
|
|
|
|
{from: PROGRAM.name, alias: 'eTopLevelComponent', identifier: 'TopLevelComponent'},
|
|
|
|
]);
|
|
|
|
const outputString = output.toString();
|
|
|
|
expect(outputString).not.toContain(`{eComponentA1 as ComponentA1}`);
|
|
|
|
expect(outputString).not.toContain(`{eComponentB as ComponentB}`);
|
|
|
|
expect(outputString).not.toContain(`{eTopLevelComponent as TopLevelComponent}`);
|
|
|
|
});
|
2018-11-25 17:07:51 -05:00
|
|
|
});
|
2018-07-16 05:23:37 -04:00
|
|
|
|
2018-06-27 11:41:11 -04:00
|
|
|
describe('addConstants', () => {
|
|
|
|
it('should insert the given constants after imports in the source file', () => {
|
|
|
|
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.addConstants(output, 'const x = 3;', file);
|
|
|
|
expect(output.toString()).toContain(`
|
|
|
|
import {Directive} from '@angular/core';
|
|
|
|
const x = 3;
|
|
|
|
|
|
|
|
export class A {}`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-17 02:50:45 -04:00
|
|
|
describe('rewriteSwitchableDeclarations', () => {
|
|
|
|
it('should switch marked declaration initializers', () => {
|
2018-10-04 07:19:11 -04:00
|
|
|
const {renderer, program, switchMarkerAnalyses, sourceFile} = setup(PROGRAM);
|
2018-08-17 02:50:45 -04:00
|
|
|
const file = program.getSourceFile('some/file.js');
|
|
|
|
if (file === undefined) {
|
|
|
|
throw new Error(`Could not find source file`);
|
|
|
|
}
|
|
|
|
const output = new MagicString(PROGRAM.contents);
|
2018-10-04 07:19:11 -04:00
|
|
|
renderer.rewriteSwitchableDeclarations(
|
|
|
|
output, file, switchMarkerAnalyses.get(sourceFile) !.declarations);
|
2018-08-17 02:50:45 -04:00
|
|
|
expect(output.toString())
|
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
|
|
|
.not.toContain(`let compileNgModuleFactory = compileNgModuleFactory__PRE_R3__;`);
|
2018-08-17 02:50:45 -04:00
|
|
|
expect(output.toString())
|
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
|
|
|
.toContain(`let badlyFormattedVariable = __PRE_R3__badlyFormattedVariable;`);
|
2018-08-17 02:50:45 -04:00
|
|
|
expect(output.toString())
|
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
|
|
|
.toContain(`let compileNgModuleFactory = compileNgModuleFactory__POST_R3__;`);
|
2018-08-17 02:50:45 -04:00
|
|
|
expect(output.toString())
|
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
|
|
|
.toContain(`function compileNgModuleFactory__PRE_R3__(injector, options, moduleType) {`);
|
2018-08-17 02:50:45 -04:00
|
|
|
expect(output.toString())
|
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
|
|
|
.toContain(`function compileNgModuleFactory__POST_R3__(injector, options, moduleType) {`);
|
2018-08-17 02:50:45 -04:00
|
|
|
});
|
|
|
|
});
|
2018-06-27 11:41:11 -04:00
|
|
|
|
2018-07-16 05:23:37 -04:00
|
|
|
describe('addDefinitions', () => {
|
|
|
|
it('should insert the definitions directly after the class declaration', () => {
|
2018-10-04 07:19:11 -04:00
|
|
|
const {renderer, decorationAnalyses, sourceFile} = setup(PROGRAM);
|
2018-07-16 05:23:37 -04:00
|
|
|
const output = new MagicString(PROGRAM.contents);
|
2018-10-16 03:56:54 -04:00
|
|
|
const compiledClass =
|
|
|
|
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'A') !;
|
|
|
|
renderer.addDefinitions(output, compiledClass, 'SOME DEFINITION TEXT');
|
2018-08-17 02:35:01 -04:00
|
|
|
expect(output.toString()).toContain(`
|
2018-07-16 05:23:37 -04:00
|
|
|
export class A {}
|
|
|
|
SOME DEFINITION TEXT
|
|
|
|
A.decorators = [
|
2018-08-17 02:35:01 -04:00
|
|
|
`);
|
2018-07-16 05:23:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('removeDecorators', () => {
|
2018-09-24 15:50:25 -04:00
|
|
|
describe('[static property declaration]', () => {
|
|
|
|
it('should delete the decorator (and following comma) that was matched in the analysis',
|
|
|
|
() => {
|
2018-10-04 07:19:11 -04:00
|
|
|
const {decorationAnalyses, sourceFile, renderer} = setup(PROGRAM);
|
2018-09-24 15:50:25 -04:00
|
|
|
const output = new MagicString(PROGRAM.contents);
|
2018-10-16 03:56:54 -04:00
|
|
|
const compiledClass =
|
|
|
|
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'A') !;
|
|
|
|
const decorator = compiledClass.decorators[0];
|
2018-09-24 15:50:25 -04:00
|
|
|
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
|
|
|
|
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
|
|
|
|
renderer.removeDecorators(output, decoratorsToRemove);
|
|
|
|
expect(output.toString())
|
|
|
|
.not.toContain(`{ type: Directive, args: [{ selector: '[a]' }] },`);
|
|
|
|
expect(output.toString()).toContain(`{ type: OtherA }`);
|
|
|
|
expect(output.toString()).toContain(`{ type: Directive, args: [{ selector: '[b]' }] }`);
|
|
|
|
expect(output.toString()).toContain(`{ type: OtherB }`);
|
|
|
|
expect(output.toString()).toContain(`{ type: Directive, args: [{ selector: '[c]' }] }`);
|
|
|
|
});
|
|
|
|
|
2018-07-16 05:23:37 -04:00
|
|
|
|
2018-09-24 15:50:25 -04:00
|
|
|
it('should delete the decorator (but cope with no trailing comma) that was matched in the analysis',
|
|
|
|
() => {
|
2018-10-04 07:19:11 -04:00
|
|
|
const {decorationAnalyses, sourceFile, renderer} = setup(PROGRAM);
|
2018-09-24 15:50:25 -04:00
|
|
|
const output = new MagicString(PROGRAM.contents);
|
2018-10-16 03:56:54 -04:00
|
|
|
const compiledClass =
|
|
|
|
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'B') !;
|
|
|
|
const decorator = compiledClass.decorators[0];
|
2018-09-24 15:50:25 -04:00
|
|
|
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
|
|
|
|
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
|
|
|
|
renderer.removeDecorators(output, decoratorsToRemove);
|
|
|
|
expect(output.toString()).toContain(`{ type: Directive, args: [{ selector: '[a]' }] },`);
|
|
|
|
expect(output.toString()).toContain(`{ type: OtherA }`);
|
|
|
|
expect(output.toString())
|
|
|
|
.not.toContain(`{ type: Directive, args: [{ selector: '[b]' }] }`);
|
|
|
|
expect(output.toString()).toContain(`{ type: OtherB }`);
|
|
|
|
expect(output.toString()).toContain(`{ type: Directive, args: [{ selector: '[c]' }] }`);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should delete the decorator (and its container if there are no other decorators left) that was matched in the analysis',
|
|
|
|
() => {
|
2018-10-04 07:19:11 -04:00
|
|
|
const {decorationAnalyses, sourceFile, renderer} = setup(PROGRAM);
|
2018-09-24 15:50:25 -04:00
|
|
|
const output = new MagicString(PROGRAM.contents);
|
2018-10-16 03:56:54 -04:00
|
|
|
const compiledClass =
|
|
|
|
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'C') !;
|
|
|
|
const decorator = compiledClass.decorators[0];
|
2018-09-24 15:50:25 -04:00
|
|
|
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
|
|
|
|
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
|
|
|
|
renderer.removeDecorators(output, decoratorsToRemove);
|
|
|
|
expect(output.toString()).toContain(`{ type: Directive, args: [{ selector: '[a]' }] },`);
|
|
|
|
expect(output.toString()).toContain(`{ type: OtherA }`);
|
|
|
|
expect(output.toString()).toContain(`{ type: Directive, args: [{ selector: '[b]' }] }`);
|
|
|
|
expect(output.toString()).toContain(`{ type: OtherB }`);
|
|
|
|
expect(output.toString())
|
|
|
|
.not.toContain(`{ type: Directive, args: [{ selector: '[c]' }] }`);
|
|
|
|
expect(output.toString()).not.toContain(`C.decorators = [`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('[__decorate declarations]', () => {
|
2018-07-16 05:23:37 -04:00
|
|
|
it('should delete the decorator (and following comma) that was matched in the analysis', () => {
|
2018-10-04 07:19:11 -04:00
|
|
|
const {renderer, decorationAnalyses, sourceFile} = setup(PROGRAM_DECORATE_HELPER);
|
2018-09-24 15:50:25 -04:00
|
|
|
const output = new MagicString(PROGRAM_DECORATE_HELPER.contents);
|
2018-10-16 03:56:54 -04:00
|
|
|
const compiledClass =
|
|
|
|
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'A') !;
|
|
|
|
const decorator = compiledClass.decorators.find(d => d.name === 'Directive') !;
|
2018-07-16 05:23:37 -04:00
|
|
|
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
|
2018-08-17 02:35:01 -04:00
|
|
|
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
|
2018-07-16 05:23:37 -04:00
|
|
|
renderer.removeDecorators(output, decoratorsToRemove);
|
2018-09-24 15:50:25 -04:00
|
|
|
expect(output.toString()).not.toContain(`Directive({ selector: '[a]' }),`);
|
|
|
|
expect(output.toString()).toContain(`OtherA()`);
|
|
|
|
expect(output.toString()).toContain(`Directive({ selector: '[b]' })`);
|
|
|
|
expect(output.toString()).toContain(`OtherB()`);
|
|
|
|
expect(output.toString()).toContain(`Directive({ selector: '[c]' })`);
|
2018-07-16 05:23:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should delete the decorator (but cope with no trailing comma) that was matched in the analysis',
|
|
|
|
() => {
|
2018-10-04 07:19:11 -04:00
|
|
|
const {renderer, decorationAnalyses, sourceFile} = setup(PROGRAM_DECORATE_HELPER);
|
2018-09-24 15:50:25 -04:00
|
|
|
const output = new MagicString(PROGRAM_DECORATE_HELPER.contents);
|
2018-10-16 03:56:54 -04:00
|
|
|
const compiledClass =
|
|
|
|
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'B') !;
|
|
|
|
const decorator = compiledClass.decorators.find(d => d.name === 'Directive') !;
|
2018-07-16 05:23:37 -04:00
|
|
|
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
|
2018-08-17 02:35:01 -04:00
|
|
|
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
|
2018-07-16 05:23:37 -04:00
|
|
|
renderer.removeDecorators(output, decoratorsToRemove);
|
2018-09-24 15:50:25 -04:00
|
|
|
expect(output.toString()).toContain(`Directive({ selector: '[a]' }),`);
|
|
|
|
expect(output.toString()).toContain(`OtherA()`);
|
|
|
|
expect(output.toString()).not.toContain(`Directive({ selector: '[b]' })`);
|
|
|
|
expect(output.toString()).toContain(`OtherB()`);
|
|
|
|
expect(output.toString()).toContain(`Directive({ selector: '[c]' })`);
|
2018-07-16 05:23:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should delete the decorator (and its container if there are not other decorators left) that was matched in the analysis',
|
|
|
|
() => {
|
2018-10-04 07:19:11 -04:00
|
|
|
const {renderer, decorationAnalyses, sourceFile} = setup(PROGRAM_DECORATE_HELPER);
|
2018-09-24 15:50:25 -04:00
|
|
|
const output = new MagicString(PROGRAM_DECORATE_HELPER.contents);
|
2018-10-16 03:56:54 -04:00
|
|
|
const compiledClass =
|
|
|
|
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'C') !;
|
|
|
|
const decorator = compiledClass.decorators.find(d => d.name === 'Directive') !;
|
2018-07-16 05:23:37 -04:00
|
|
|
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
|
2018-08-17 02:35:01 -04:00
|
|
|
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
|
2018-07-16 05:23:37 -04:00
|
|
|
renderer.removeDecorators(output, decoratorsToRemove);
|
2018-09-24 15:50:25 -04:00
|
|
|
expect(output.toString()).toContain(`Directive({ selector: '[a]' }),`);
|
|
|
|
expect(output.toString()).toContain(`OtherA()`);
|
|
|
|
expect(output.toString()).toContain(`Directive({ selector: '[b]' })`);
|
|
|
|
expect(output.toString()).toContain(`OtherB()`);
|
|
|
|
expect(output.toString()).not.toContain(`Directive({ selector: '[c]' })`);
|
|
|
|
expect(output.toString()).not.toContain(`C = tslib_1.__decorate([`);
|
|
|
|
expect(output.toString()).toContain(`let C = class C {\n};\nexport { C };`);
|
2018-07-16 05:23:37 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|