2017-08-14 14:04:55 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2017-08-14 14:04:55 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2017-09-12 12:40:28 -04:00
|
|
|
import * as compiler from '@angular/compiler';
|
2017-08-14 14:04:55 -04:00
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
2017-09-12 12:40:28 -04:00
|
|
|
import {MetadataCollector} from '../../src/metadata/collector';
|
2017-09-21 21:05:07 -04:00
|
|
|
import {CompilerHost, CompilerOptions, LibrarySummary} from '../../src/transformers/api';
|
2020-04-07 15:43:43 -04:00
|
|
|
import {createCompilerHost, TsCompilerAotCompilerTypeCheckHostAdapter} from '../../src/transformers/compiler_host';
|
2017-08-14 14:04:55 -04:00
|
|
|
import {Directory, Entry, MockAotContext, MockCompilerHost} from '../mocks';
|
|
|
|
|
|
|
|
const dummyModule = 'export let foo: any[];';
|
2017-09-12 12:40:28 -04:00
|
|
|
const aGeneratedFile = new compiler.GeneratedFile(
|
|
|
|
'/tmp/src/index.ts', '/tmp/src/index.ngfactory.ts',
|
|
|
|
[new compiler.DeclareVarStmt('x', new compiler.LiteralExpr(1))]);
|
|
|
|
const aGeneratedFileText = `var x:any = 1;\n`;
|
2017-08-14 14:04:55 -04:00
|
|
|
|
|
|
|
describe('NgCompilerHost', () => {
|
2017-09-21 21:05:07 -04:00
|
|
|
let codeGenerator: {generateFile: jasmine.Spy; findGeneratedFileNames: jasmine.Spy;};
|
2017-09-12 12:40:28 -04:00
|
|
|
|
2017-09-21 21:05:07 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
codeGenerator = {
|
|
|
|
generateFile: jasmine.createSpy('generateFile').and.returnValue(null),
|
|
|
|
findGeneratedFileNames: jasmine.createSpy('findGeneratedFileNames').and.returnValue([]),
|
|
|
|
};
|
|
|
|
});
|
2017-09-12 12:40:28 -04:00
|
|
|
|
|
|
|
function createNgHost({files = {}}: {files?: Directory} = {}): CompilerHost {
|
2017-08-14 14:04:55 -04:00
|
|
|
const context = new MockAotContext('/tmp/', files);
|
2017-09-12 12:40:28 -04:00
|
|
|
return new MockCompilerHost(context) as ts.CompilerHost;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createHost({
|
|
|
|
files = {},
|
|
|
|
options = {
|
|
|
|
basePath: '/tmp',
|
|
|
|
moduleResolution: ts.ModuleResolutionKind.NodeJs,
|
|
|
|
},
|
2017-10-03 16:47:39 -04:00
|
|
|
rootNames = ['/tmp/index.ts'],
|
2017-09-12 12:40:28 -04:00
|
|
|
ngHost = createNgHost({files}),
|
2017-09-21 21:05:07 -04:00
|
|
|
librarySummaries = [],
|
2017-09-20 19:31:32 -04:00
|
|
|
}: {
|
|
|
|
files?: Directory,
|
|
|
|
options?: CompilerOptions,
|
2017-10-03 16:47:39 -04:00
|
|
|
rootNames?: string[],
|
2017-09-20 19:31:32 -04:00
|
|
|
ngHost?: CompilerHost,
|
2017-09-21 21:05:07 -04:00
|
|
|
librarySummaries?: LibrarySummary[]
|
2017-09-20 19:31:32 -04:00
|
|
|
} = {}) {
|
2017-09-12 12:40:28 -04:00
|
|
|
return new TsCompilerAotCompilerTypeCheckHostAdapter(
|
2017-10-03 16:47:39 -04:00
|
|
|
rootNames, options, ngHost, new MetadataCollector(), codeGenerator,
|
2020-04-07 15:43:43 -04:00
|
|
|
new Map(
|
|
|
|
librarySummaries.map(entry => [entry.fileName, entry] as [string, LibrarySummary])));
|
2017-08-14 14:04:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('fileNameToModuleName', () => {
|
2017-09-12 12:40:28 -04:00
|
|
|
let host: TsCompilerAotCompilerTypeCheckHostAdapter;
|
2020-04-07 15:43:43 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
host = createHost();
|
|
|
|
});
|
2017-08-14 14:04:55 -04:00
|
|
|
|
|
|
|
it('should use a package import when accessing a package from a source file', () => {
|
2017-09-12 12:40:28 -04:00
|
|
|
expect(host.fileNameToModuleName('/tmp/node_modules/@angular/core.d.ts', '/tmp/main.ts'))
|
2017-08-14 14:04:55 -04:00
|
|
|
.toBe('@angular/core');
|
|
|
|
});
|
|
|
|
|
2017-11-26 13:04:46 -05:00
|
|
|
it('should allow an import o a package whose name contains dot (e.g. @angular.io)', () => {
|
|
|
|
expect(host.fileNameToModuleName('/tmp/node_modules/@angular.io/core.d.ts', '/tmp/main.ts'))
|
|
|
|
.toBe('@angular.io/core');
|
|
|
|
});
|
|
|
|
|
2017-08-14 14:04:55 -04:00
|
|
|
it('should use a package import when accessing a package from another package', () => {
|
2017-09-12 12:40:28 -04:00
|
|
|
expect(host.fileNameToModuleName(
|
2017-08-14 14:04:55 -04:00
|
|
|
'/tmp/node_modules/mod1/index.d.ts', '/tmp/node_modules/mod2/index.d.ts'))
|
|
|
|
.toBe('mod1/index');
|
2017-09-12 12:40:28 -04:00
|
|
|
expect(host.fileNameToModuleName(
|
2017-08-14 14:04:55 -04:00
|
|
|
'/tmp/node_modules/@angular/core/index.d.ts',
|
|
|
|
'/tmp/node_modules/@angular/common/index.d.ts'))
|
|
|
|
.toBe('@angular/core/index');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use a relative import when accessing a file in the same package', () => {
|
2017-09-12 12:40:28 -04:00
|
|
|
expect(host.fileNameToModuleName(
|
2017-08-14 14:04:55 -04:00
|
|
|
'/tmp/node_modules/mod/a/child.d.ts', '/tmp/node_modules/mod/index.d.ts'))
|
|
|
|
.toBe('./a/child');
|
2017-09-12 12:40:28 -04:00
|
|
|
expect(host.fileNameToModuleName(
|
2017-08-14 14:04:55 -04:00
|
|
|
'/tmp/node_modules/@angular/core/src/core.d.ts',
|
|
|
|
'/tmp/node_modules/@angular/core/index.d.ts'))
|
|
|
|
.toBe('./src/core');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use a relative import when accessing a source file from a source file', () => {
|
2017-09-12 12:40:28 -04:00
|
|
|
expect(host.fileNameToModuleName('/tmp/src/a/child.ts', '/tmp/src/index.ts'))
|
2017-08-14 14:04:55 -04:00
|
|
|
.toBe('./a/child');
|
|
|
|
});
|
|
|
|
|
2017-10-30 14:54:40 -04:00
|
|
|
it('should use a relative import when accessing generated files, even if crossing packages',
|
|
|
|
() => {
|
|
|
|
expect(host.fileNameToModuleName(
|
|
|
|
'/tmp/node_modules/mod2/b.ngfactory.d.ts',
|
|
|
|
'/tmp/node_modules/mod1/a.ngfactory.d.ts'))
|
|
|
|
.toBe('../mod2/b.ngfactory');
|
|
|
|
});
|
|
|
|
|
2017-08-14 14:04:55 -04:00
|
|
|
it('should support multiple rootDirs when accessing a source file form a source file', () => {
|
2017-09-12 12:40:28 -04:00
|
|
|
const hostWithMultipleRoots = createHost({
|
2017-08-14 14:04:55 -04:00
|
|
|
options: {
|
|
|
|
basePath: '/tmp/',
|
|
|
|
rootDirs: [
|
|
|
|
'src/a',
|
|
|
|
'src/b',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
2017-08-15 17:41:48 -04:00
|
|
|
// both files are in the rootDirs
|
2017-09-12 12:40:28 -04:00
|
|
|
expect(hostWithMultipleRoots.fileNameToModuleName('/tmp/src/b/b.ts', '/tmp/src/a/a.ts'))
|
2017-08-14 14:04:55 -04:00
|
|
|
.toBe('./b');
|
2017-08-15 17:41:48 -04:00
|
|
|
|
|
|
|
// one file is not in the rootDirs
|
2017-09-12 12:40:28 -04:00
|
|
|
expect(hostWithMultipleRoots.fileNameToModuleName('/tmp/src/c/c.ts', '/tmp/src/a/a.ts'))
|
2017-08-15 17:41:48 -04:00
|
|
|
.toBe('../c/c');
|
2017-08-14 14:04:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should error if accessing a source file from a package', () => {
|
|
|
|
expect(
|
2017-09-12 12:40:28 -04:00
|
|
|
() => host.fileNameToModuleName(
|
2017-08-14 14:04:55 -04:00
|
|
|
'/tmp/src/a/child.ts', '/tmp/node_modules/@angular/core.d.ts'))
|
|
|
|
.toThrowError(
|
2017-09-12 12:40:28 -04:00
|
|
|
'Trying to import a source file from a node_modules package: ' +
|
|
|
|
'import /tmp/src/a/child.ts from /tmp/node_modules/@angular/core.d.ts');
|
2017-08-14 14:04:55 -04:00
|
|
|
});
|
|
|
|
|
2017-09-12 12:40:28 -04:00
|
|
|
it('should use the provided implementation if any', () => {
|
|
|
|
const ngHost = createNgHost();
|
|
|
|
ngHost.fileNameToModuleName = () => 'someResult';
|
|
|
|
const host = createHost({ngHost});
|
|
|
|
expect(host.fileNameToModuleName('a', 'b')).toBe('someResult');
|
|
|
|
});
|
2017-08-14 14:04:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('moduleNameToFileName', () => {
|
|
|
|
it('should resolve an import using the containing file', () => {
|
2017-09-12 12:40:28 -04:00
|
|
|
const host = createHost({files: {'tmp': {'src': {'a': {'child.d.ts': dummyModule}}}}});
|
|
|
|
expect(host.moduleNameToFileName('./a/child', '/tmp/src/index.ts'))
|
2017-08-14 14:04:55 -04:00
|
|
|
.toBe('/tmp/src/a/child.d.ts');
|
|
|
|
});
|
2017-09-12 12:40:28 -04:00
|
|
|
|
2018-03-10 12:14:58 -05:00
|
|
|
it('should allow to skip the containing file for package imports', () => {
|
2017-09-12 12:40:28 -04:00
|
|
|
const host =
|
|
|
|
createHost({files: {'tmp': {'node_modules': {'@core': {'index.d.ts': dummyModule}}}}});
|
|
|
|
expect(host.moduleNameToFileName('@core/index')).toBe('/tmp/node_modules/@core/index.d.ts');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use the provided implementation if any', () => {
|
|
|
|
const ngHost = createNgHost();
|
|
|
|
ngHost.moduleNameToFileName = () => 'someResult';
|
|
|
|
const host = createHost({ngHost});
|
|
|
|
expect(host.moduleNameToFileName('a', 'b')).toBe('someResult');
|
|
|
|
});
|
2017-10-03 16:47:39 -04:00
|
|
|
|
|
|
|
it('should work well with windows paths', () => {
|
|
|
|
const host = createHost({
|
|
|
|
rootNames: ['\\tmp\\index.ts'],
|
|
|
|
options: {basePath: '\\tmp'},
|
|
|
|
files: {'tmp': {'node_modules': {'@core': {'index.d.ts': dummyModule}}}}
|
|
|
|
});
|
|
|
|
expect(host.moduleNameToFileName('@core/index')).toBe('/tmp/node_modules/@core/index.d.ts');
|
|
|
|
});
|
2017-08-14 14:04:55 -04:00
|
|
|
});
|
2017-08-15 20:06:09 -04:00
|
|
|
|
|
|
|
describe('resourceNameToFileName', () => {
|
|
|
|
it('should resolve a relative import', () => {
|
2017-09-12 12:40:28 -04:00
|
|
|
const host = createHost({files: {'tmp': {'src': {'a': {'child.html': '<div>'}}}}});
|
|
|
|
expect(host.resourceNameToFileName('./a/child.html', '/tmp/src/index.ts'))
|
2017-08-15 20:06:09 -04:00
|
|
|
.toBe('/tmp/src/a/child.html');
|
|
|
|
|
2017-09-12 12:40:28 -04:00
|
|
|
expect(host.resourceNameToFileName('./a/non-existing.html', '/tmp/src/index.ts')).toBe(null);
|
2017-08-15 20:06:09 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should resolve package paths as relative paths', () => {
|
2017-09-12 12:40:28 -04:00
|
|
|
const host = createHost({files: {'tmp': {'src': {'a': {'child.html': '<div>'}}}}});
|
|
|
|
expect(host.resourceNameToFileName('a/child.html', '/tmp/src/index.ts'))
|
2017-08-15 20:06:09 -04:00
|
|
|
.toBe('/tmp/src/a/child.html');
|
|
|
|
});
|
|
|
|
|
2017-08-30 19:08:55 -04:00
|
|
|
it('should resolve absolute paths as package paths', () => {
|
2017-09-12 12:40:28 -04:00
|
|
|
const host = createHost({files: {'tmp': {'node_modules': {'a': {'child.html': '<div>'}}}}});
|
|
|
|
expect(host.resourceNameToFileName('/a/child.html', ''))
|
2017-08-30 19:08:55 -04:00
|
|
|
.toBe('/tmp/node_modules/a/child.html');
|
2017-08-15 20:06:09 -04:00
|
|
|
});
|
2017-09-12 12:40:28 -04:00
|
|
|
|
|
|
|
it('should use the provided implementation if any', () => {
|
|
|
|
const ngHost = createNgHost();
|
|
|
|
ngHost.resourceNameToFileName = () => 'someResult';
|
|
|
|
const host = createHost({ngHost});
|
|
|
|
expect(host.resourceNameToFileName('a', 'b')).toBe('someResult');
|
|
|
|
});
|
2019-01-15 16:35:28 -05:00
|
|
|
it('should resolve Sass imports to generated .css files', () => {
|
|
|
|
const host = createHost({files: {'tmp': {'src': {'a': {'style.css': 'h1: bold'}}}}});
|
|
|
|
expect(host.resourceNameToFileName('./a/style.scss', '/tmp/src/index.ts'))
|
|
|
|
.toBe('/tmp/src/a/style.css');
|
|
|
|
});
|
|
|
|
it('should resolve Less imports to generated .css files', () => {
|
|
|
|
const host = createHost({files: {'tmp': {'src': {'a': {'style.css': 'h1: bold'}}}}});
|
|
|
|
expect(host.resourceNameToFileName('./a/style.less', '/tmp/src/index.ts'))
|
|
|
|
.toBe('/tmp/src/a/style.css');
|
|
|
|
});
|
|
|
|
it('should resolve Stylus imports to generated .css files', () => {
|
|
|
|
const host = createHost({files: {'tmp': {'src': {'a': {'style.css': 'h1: bold'}}}}});
|
|
|
|
expect(host.resourceNameToFileName('./a/style.styl', '/tmp/src/index.ts'))
|
|
|
|
.toBe('/tmp/src/a/style.css');
|
|
|
|
});
|
2017-09-12 12:40:28 -04:00
|
|
|
});
|
|
|
|
|
2019-03-18 12:52:50 -04:00
|
|
|
describe('addGeneratedFile', () => {
|
|
|
|
function generate(path: string, files: {}) {
|
|
|
|
codeGenerator.findGeneratedFileNames.and.returnValue([`${path}.ngfactory.ts`]);
|
|
|
|
codeGenerator.generateFile.and.returnValue(
|
|
|
|
new compiler.GeneratedFile(`${path}.ts`, `${path}.ngfactory.ts`, []));
|
|
|
|
const host = createHost({
|
|
|
|
files,
|
|
|
|
options: {
|
|
|
|
basePath: '/tmp',
|
|
|
|
moduleResolution: ts.ModuleResolutionKind.NodeJs,
|
|
|
|
// Request UMD, which should get default module names
|
|
|
|
module: ts.ModuleKind.UMD
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return host.getSourceFile(`${path}.ngfactory.ts`, ts.ScriptTarget.Latest);
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should include a moduleName when the file is in node_modules', () => {
|
|
|
|
const genSf = generate(
|
|
|
|
'/tmp/node_modules/@angular/core/core',
|
|
|
|
{'tmp': {'node_modules': {'@angular': {'core': {'core.ts': `// some content`}}}}});
|
|
|
|
expect(genSf.moduleName).toBe('@angular/core/core.ngfactory');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not get tripped on nested node_modules', () => {
|
|
|
|
const genSf = generate('/tmp/node_modules/lib1/node_modules/lib2/thing', {
|
2020-04-07 15:43:43 -04:00
|
|
|
'tmp':
|
|
|
|
{'node_modules': {'lib1': {'node_modules': {'lib2': {'thing.ts': `// some content`}}}}}
|
2019-03-18 12:52:50 -04:00
|
|
|
});
|
|
|
|
expect(genSf.moduleName).toBe('lib2/thing.ngfactory');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-09-12 12:40:28 -04:00
|
|
|
describe('getSourceFile', () => {
|
|
|
|
it('should cache source files by name', () => {
|
|
|
|
const host = createHost({files: {'tmp': {'src': {'index.ts': ``}}}});
|
|
|
|
|
|
|
|
const sf1 = host.getSourceFile('/tmp/src/index.ts', ts.ScriptTarget.Latest);
|
|
|
|
const sf2 = host.getSourceFile('/tmp/src/index.ts', ts.ScriptTarget.Latest);
|
|
|
|
expect(sf1).toBe(sf2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate code when asking for the base name and add it as referencedFiles', () => {
|
2017-09-21 21:05:07 -04:00
|
|
|
codeGenerator.findGeneratedFileNames.and.returnValue(['/tmp/src/index.ngfactory.ts']);
|
|
|
|
codeGenerator.generateFile.and.returnValue(aGeneratedFile);
|
2017-09-12 12:40:28 -04:00
|
|
|
const host = createHost({
|
|
|
|
files: {
|
|
|
|
'tmp': {
|
|
|
|
'src': {
|
|
|
|
'index.ts': `
|
|
|
|
/// <reference path="main.ts"/>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const sf = host.getSourceFile('/tmp/src/index.ts', ts.ScriptTarget.Latest);
|
|
|
|
expect(sf.referencedFiles[0].fileName).toBe('main.ts');
|
|
|
|
expect(sf.referencedFiles[1].fileName).toBe('/tmp/src/index.ngfactory.ts');
|
|
|
|
|
|
|
|
const genSf = host.getSourceFile('/tmp/src/index.ngfactory.ts', ts.ScriptTarget.Latest);
|
|
|
|
expect(genSf.text).toBe(aGeneratedFileText);
|
|
|
|
|
|
|
|
// the codegen should have been cached
|
2017-09-21 21:05:07 -04:00
|
|
|
expect(codeGenerator.generateFile).toHaveBeenCalledTimes(1);
|
|
|
|
expect(codeGenerator.findGeneratedFileNames).toHaveBeenCalledTimes(1);
|
2017-09-12 12:40:28 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate code when asking for the generated name first', () => {
|
2017-09-21 21:05:07 -04:00
|
|
|
codeGenerator.findGeneratedFileNames.and.returnValue(['/tmp/src/index.ngfactory.ts']);
|
|
|
|
codeGenerator.generateFile.and.returnValue(aGeneratedFile);
|
2017-09-12 12:40:28 -04:00
|
|
|
const host = createHost({
|
|
|
|
files: {
|
|
|
|
'tmp': {
|
|
|
|
'src': {
|
|
|
|
'index.ts': `
|
|
|
|
/// <reference path="main.ts"/>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const genSf = host.getSourceFile('/tmp/src/index.ngfactory.ts', ts.ScriptTarget.Latest);
|
|
|
|
expect(genSf.text).toBe(aGeneratedFileText);
|
|
|
|
|
|
|
|
const sf = host.getSourceFile('/tmp/src/index.ts', ts.ScriptTarget.Latest);
|
|
|
|
expect(sf.referencedFiles[0].fileName).toBe('main.ts');
|
|
|
|
expect(sf.referencedFiles[1].fileName).toBe('/tmp/src/index.ngfactory.ts');
|
|
|
|
|
|
|
|
// the codegen should have been cached
|
2017-09-21 21:05:07 -04:00
|
|
|
expect(codeGenerator.generateFile).toHaveBeenCalledTimes(1);
|
|
|
|
expect(codeGenerator.findGeneratedFileNames).toHaveBeenCalledTimes(1);
|
2017-09-12 12:40:28 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should clear old generated references if the original host cached them', () => {
|
2017-09-21 21:05:07 -04:00
|
|
|
codeGenerator.findGeneratedFileNames.and.returnValue(['/tmp/src/index.ngfactory.ts']);
|
|
|
|
|
2017-09-12 12:40:28 -04:00
|
|
|
const sfText = `
|
|
|
|
/// <reference path="main.ts"/>
|
|
|
|
`;
|
2017-10-17 19:10:15 -04:00
|
|
|
const ngHost = createNgHost({files: {'tmp': {'src': {'index.ts': sfText}}}});
|
2017-09-12 12:40:28 -04:00
|
|
|
const sf = ts.createSourceFile('/tmp/src/index.ts', sfText, ts.ScriptTarget.Latest);
|
|
|
|
ngHost.getSourceFile = () => sf;
|
|
|
|
|
2017-09-21 21:05:07 -04:00
|
|
|
codeGenerator.findGeneratedFileNames.and.returnValue(['/tmp/src/index.ngfactory.ts']);
|
|
|
|
codeGenerator.generateFile.and.returnValue(
|
|
|
|
new compiler.GeneratedFile('/tmp/src/index.ts', '/tmp/src/index.ngfactory.ts', []));
|
2017-09-12 12:40:28 -04:00
|
|
|
const host1 = createHost({ngHost});
|
|
|
|
|
|
|
|
host1.getSourceFile('/tmp/src/index.ts', ts.ScriptTarget.Latest);
|
|
|
|
expect(sf.referencedFiles.length).toBe(2);
|
|
|
|
expect(sf.referencedFiles[0].fileName).toBe('main.ts');
|
|
|
|
expect(sf.referencedFiles[1].fileName).toBe('/tmp/src/index.ngfactory.ts');
|
|
|
|
|
2017-09-21 21:05:07 -04:00
|
|
|
codeGenerator.findGeneratedFileNames.and.returnValue([]);
|
|
|
|
codeGenerator.generateFile.and.returnValue(null);
|
2017-09-12 12:40:28 -04:00
|
|
|
const host2 = createHost({ngHost});
|
|
|
|
|
|
|
|
host2.getSourceFile('/tmp/src/index.ts', ts.ScriptTarget.Latest);
|
|
|
|
expect(sf.referencedFiles.length).toBe(1);
|
|
|
|
expect(sf.referencedFiles[0].fileName).toBe('main.ts');
|
|
|
|
});
|
2017-11-29 02:27:16 -05:00
|
|
|
|
|
|
|
it('should generate for tsx files', () => {
|
|
|
|
codeGenerator.findGeneratedFileNames.and.returnValue(['/tmp/src/index.ngfactory.ts']);
|
|
|
|
codeGenerator.generateFile.and.returnValue(aGeneratedFile);
|
|
|
|
const host = createHost({files: {'tmp': {'src': {'index.tsx': ``}}}});
|
|
|
|
|
|
|
|
const genSf = host.getSourceFile('/tmp/src/index.ngfactory.ts', ts.ScriptTarget.Latest);
|
|
|
|
expect(genSf.text).toBe(aGeneratedFileText);
|
|
|
|
|
|
|
|
const sf = host.getSourceFile('/tmp/src/index.tsx', ts.ScriptTarget.Latest);
|
|
|
|
expect(sf.referencedFiles[0].fileName).toBe('/tmp/src/index.ngfactory.ts');
|
|
|
|
|
|
|
|
// the codegen should have been cached
|
|
|
|
expect(codeGenerator.generateFile).toHaveBeenCalledTimes(1);
|
|
|
|
expect(codeGenerator.findGeneratedFileNames).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
2017-09-12 12:40:28 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('updateSourceFile', () => {
|
|
|
|
it('should update source files', () => {
|
2017-09-21 21:05:07 -04:00
|
|
|
codeGenerator.findGeneratedFileNames.and.returnValue(['/tmp/src/index.ngfactory.ts']);
|
|
|
|
codeGenerator.generateFile.and.returnValue(aGeneratedFile);
|
2017-09-12 12:40:28 -04:00
|
|
|
const host = createHost({files: {'tmp': {'src': {'index.ts': ''}}}});
|
|
|
|
|
|
|
|
let genSf = host.getSourceFile('/tmp/src/index.ngfactory.ts', ts.ScriptTarget.Latest);
|
|
|
|
expect(genSf.text).toBe(aGeneratedFileText);
|
|
|
|
|
|
|
|
host.updateGeneratedFile(new compiler.GeneratedFile(
|
|
|
|
'/tmp/src/index.ts', '/tmp/src/index.ngfactory.ts',
|
|
|
|
[new compiler.DeclareVarStmt('x', new compiler.LiteralExpr(2))]));
|
|
|
|
genSf = host.getSourceFile('/tmp/src/index.ngfactory.ts', ts.ScriptTarget.Latest);
|
|
|
|
expect(genSf.text).toBe(`var x:any = 2;\n`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should error if the imports changed', () => {
|
2017-09-21 21:05:07 -04:00
|
|
|
codeGenerator.findGeneratedFileNames.and.returnValue(['/tmp/src/index.ngfactory.ts']);
|
|
|
|
codeGenerator.generateFile.and.returnValue(new compiler.GeneratedFile(
|
|
|
|
'/tmp/src/index.ts', '/tmp/src/index.ngfactory.ts',
|
|
|
|
[new compiler.DeclareVarStmt(
|
|
|
|
'x',
|
|
|
|
new compiler.ExternalExpr(new compiler.ExternalReference('aModule', 'aName')))]));
|
2017-09-12 12:40:28 -04:00
|
|
|
const host = createHost({files: {'tmp': {'src': {'index.ts': ''}}}});
|
|
|
|
|
|
|
|
host.getSourceFile('/tmp/src/index.ngfactory.ts', ts.ScriptTarget.Latest);
|
|
|
|
|
|
|
|
expect(
|
|
|
|
() => host.updateGeneratedFile(new compiler.GeneratedFile(
|
|
|
|
'/tmp/src/index.ts', '/tmp/src/index.ngfactory.ts',
|
|
|
|
[new compiler.DeclareVarStmt(
|
2020-04-07 15:43:43 -04:00
|
|
|
'x',
|
|
|
|
new compiler.ExternalExpr(
|
|
|
|
new compiler.ExternalReference('otherModule', 'aName')))])))
|
2017-09-12 12:40:28 -04:00
|
|
|
.toThrowError([
|
|
|
|
`Illegal State: external references changed in /tmp/src/index.ngfactory.ts.`,
|
|
|
|
`Old: aModule.`, `New: otherModule`
|
|
|
|
].join('\n'));
|
|
|
|
});
|
2017-08-15 20:06:09 -04:00
|
|
|
});
|
|
|
|
});
|