fix(compiler): make tsx file aot compatible

fixes #20555
This commit is contained in:
Trotyl
2017-11-29 15:27:16 +08:00
committed by Alex Rickabaugh
parent 267ebf323e
commit 756dd34519
7 changed files with 80 additions and 8 deletions

View File

@ -326,7 +326,7 @@ export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHos
return {generate: false};
}
const [, base, genSuffix, suffix] = genMatch;
if (suffix !== 'ts') {
if (suffix !== 'ts' && suffix !== 'tsx') {
return {generate: false};
}
let baseFileName: string|undefined;
@ -337,9 +337,9 @@ export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHos
}
} else {
// Note: on-the-fly generated files always have a `.ts` suffix,
// but the file from which we generated it can be a `.ts`/ `.d.ts`
// but the file from which we generated it can be a `.ts`/ `.tsx`/ `.d.ts`
// (see options.generateCodeForLibraries).
baseFileName = [`${base}.ts`, `${base}.d.ts`].find(
baseFileName = [`${base}.ts`, `${base}.tsx`, `${base}.d.ts`].find(
baseFileName => this.isSourceFile(baseFileName) && this.originalFileExists(baseFileName));
if (!baseFileName) {
return {generate: false};

View File

@ -337,7 +337,7 @@ class AngularCompilerProgram implements Program {
metadataJsonCount++;
const metadata = this.metadataCache.getMetadata(sf);
const metadataText = JSON.stringify([metadata]);
const outFileName = srcToOutPath(sf.fileName.replace(/\.ts$/, '.metadata.json'));
const outFileName = srcToOutPath(sf.fileName.replace(/\.tsx?$/, '.metadata.json'));
this.writeFile(outFileName, metadataText, false, undefined, undefined, [sf]);
}
});

View File

@ -285,6 +285,22 @@ describe('NgCompilerHost', () => {
expect(sf.referencedFiles.length).toBe(1);
expect(sf.referencedFiles[0].fileName).toBe('main.ts');
});
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);
});
});
describe('updateSourceFile', () => {

View File

@ -391,6 +391,22 @@ describe('ng program', () => {
testSupport.shouldExist('built/src/main.ngfactory.d.ts');
});
it('should work with tsx files', () => {
// create a temporary ts program to get the list of all files from angular...
testSupport.writeFiles({
'src/main.tsx': createModuleAndCompSource('main'),
});
const allRootNames = resolveFiles([path.resolve(testSupport.basePath, 'src/main.tsx')]);
const program = compile(undefined, {jsx: ts.JsxEmit.React}, allRootNames);
testSupport.shouldExist('built/src/main.js');
testSupport.shouldExist('built/src/main.d.ts');
testSupport.shouldExist('built/src/main.ngfactory.js');
testSupport.shouldExist('built/src/main.ngfactory.d.ts');
testSupport.shouldExist('built/src/main.ngsummary.json');
});
it('should emit also empty generated files depending on the options', () => {
testSupport.writeFiles({
'src/main.ts': `