fix(ivy): generate correct moduleNames for factories and summaries (#27483)
In ngtsc, files loaded into the ts.Program have a "module name", set via ts.SourceFile.moduleName, which ends up being written into an AMD module name triple-slash directive in the generated .js file. For generated shim files (ngfactories, ngsummaries) that are constructed synthetically, there was previously no moduleName set, which caused some issues with downstream tests. This commit adds logic to compute and set moduleNames for both generated ngfactory and ngsummary shims. PR Close #27483
This commit is contained in:
parent
cdf5cacdc0
commit
d553ec2f36
|
@ -12,7 +12,7 @@ import * as ts from 'typescript';
|
|||
import {relativePathBetween} from '../../util/src/path';
|
||||
|
||||
import {ShimGenerator} from './host';
|
||||
import {isNonDeclarationTsFile} from './util';
|
||||
import {generatedModuleName, isNonDeclarationTsFile} from './util';
|
||||
|
||||
const TS_DTS_SUFFIX = /(\.d)?\.ts$/;
|
||||
const STRIP_NG_FACTORY = /(.*)NgFactory$/;
|
||||
|
@ -64,8 +64,13 @@ export class FactoryGenerator implements ShimGenerator {
|
|||
...varLines,
|
||||
].join('\n');
|
||||
}
|
||||
return ts.createSourceFile(
|
||||
const genFile = ts.createSourceFile(
|
||||
genFilePath, sourceText, original.languageVersion, true, ts.ScriptKind.TS);
|
||||
if (original.moduleName !== undefined) {
|
||||
genFile.moduleName =
|
||||
generatedModuleName(original.moduleName, original.fileName, '.ngfactory');
|
||||
}
|
||||
return genFile;
|
||||
}
|
||||
|
||||
static forRootFiles(files: ReadonlyArray<string>): FactoryGenerator {
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
import * as ts from 'typescript';
|
||||
|
||||
import {ShimGenerator} from './host';
|
||||
import {isNonDeclarationTsFile} from './util';
|
||||
import {generatedModuleName, isNonDeclarationTsFile} from './util';
|
||||
|
||||
export class SummaryGenerator implements ShimGenerator {
|
||||
private constructor(private map: Map<string, string>) {}
|
||||
|
@ -43,8 +43,13 @@ export class SummaryGenerator implements ShimGenerator {
|
|||
varLines.push(`export const ɵempty = null;`);
|
||||
}
|
||||
const sourceText = varLines.join('\n');
|
||||
return ts.createSourceFile(
|
||||
const genFile = ts.createSourceFile(
|
||||
genFilePath, sourceText, original.languageVersion, true, ts.ScriptKind.TS);
|
||||
if (original.moduleName !== undefined) {
|
||||
genFile.moduleName =
|
||||
generatedModuleName(original.moduleName, original.fileName, '.ngsummary');
|
||||
}
|
||||
return genFile;
|
||||
}
|
||||
|
||||
static forRootFiles(files: ReadonlyArray<string>): SummaryGenerator {
|
||||
|
|
|
@ -6,9 +6,24 @@
|
|||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import * as path from 'path';
|
||||
import * as ts from 'typescript';
|
||||
|
||||
const TS_FILE = /\.tsx?$/;
|
||||
const D_TS_FILE = /\.d\.ts$/;
|
||||
|
||||
export function isNonDeclarationTsFile(file: string): boolean {
|
||||
return TS_FILE.exec(file) !== null && D_TS_FILE.exec(file) === null;
|
||||
}
|
||||
|
||||
export function generatedModuleName(
|
||||
originalModuleName: string, originalFileName: string, genSuffix: string): string {
|
||||
let moduleName: string;
|
||||
if (originalFileName.endsWith('/index.ts')) {
|
||||
moduleName = originalModuleName + '/index' + genSuffix;
|
||||
} else {
|
||||
moduleName = originalModuleName + genSuffix;
|
||||
}
|
||||
|
||||
return moduleName;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load("//tools:defaults.bzl", "jasmine_node_test", "ts_library")
|
||||
|
||||
ts_library(
|
||||
name = "test_lib",
|
||||
testonly = True,
|
||||
srcs = glob([
|
||||
"**/*.ts",
|
||||
]),
|
||||
deps = [
|
||||
"//packages:types",
|
||||
"//packages/compiler-cli/src/ngtsc/shims",
|
||||
],
|
||||
)
|
||||
|
||||
jasmine_node_test(
|
||||
name = "test",
|
||||
bootstrap = ["angular/tools/testing/init_node_no_angular_spec.js"],
|
||||
deps = [
|
||||
":test_lib",
|
||||
"//tools/testing:node_no_angular",
|
||||
],
|
||||
)
|
|
@ -0,0 +1,25 @@
|
|||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import {generatedModuleName} from '../src/util';
|
||||
|
||||
describe('shim utilities', () => {
|
||||
describe('generatedModuleName', () => {
|
||||
it('should generate a correct module name for index files', () => {
|
||||
expect(generatedModuleName('@angular/core', '/angular/packages/core/index.ts', '.ngfactory'))
|
||||
.toBe('@angular/core/index.ngfactory');
|
||||
});
|
||||
|
||||
it('should generate a correct module name for non-index files', () => {
|
||||
expect(generatedModuleName(
|
||||
'@angular/core/src/application_ref',
|
||||
'/angular/packages/core/src/application_ref.ts', '.ngsummary'))
|
||||
.toBe('@angular/core/src/application_ref.ngsummary');
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue