fix(bazel): ng_package should include private exports in fesms (#23054)

PR Close #23054
This commit is contained in:
Alex Eagle
2018-03-28 18:15:36 -07:00
committed by Igor Minar
parent 9fb08e2377
commit 0d9140cdce
10 changed files with 133 additions and 175 deletions

View File

@ -37,7 +37,11 @@ function createSyntheticIndexHost<H extends ts.CompilerHost>(
newHost.getSourceFile =
(fileName: string, languageVersion: ts.ScriptTarget, onError?: (message: string) => void) => {
if (path.normalize(fileName) == normalSyntheticIndexName) {
return ts.createSourceFile(fileName, indexContent, languageVersion, true);
const sf = ts.createSourceFile(fileName, indexContent, languageVersion, true);
if ((delegate as any).fileNameToModuleName) {
sf.moduleName = (delegate as any).fileNameToModuleName(fileName);
}
return sf;
}
return delegate.getSourceFile(fileName, languageVersion, onError);
};
@ -48,10 +52,10 @@ function createSyntheticIndexHost<H extends ts.CompilerHost>(
sourceFiles: Readonly<ts.SourceFile>[]) => {
delegate.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles);
if (fileName.match(DTS) && sourceFiles && sourceFiles.length == 1 &&
path.normalize(sourceFiles[0].fileName) == normalSyntheticIndexName) {
path.normalize(sourceFiles[0].fileName) === normalSyntheticIndexName) {
// If we are writing the synthetic index, write the metadata along side.
const metadataName = fileName.replace(DTS, '.metadata.json');
fs.writeFileSync(metadataName, indexMetadata, {encoding: 'utf8'});
delegate.writeFile(metadataName, indexMetadata, writeByteOrderMark, onError, []);
}
};
return newHost;
@ -61,7 +65,20 @@ export function createBundleIndexHost<H extends ts.CompilerHost>(
ngOptions: CompilerOptions, rootFiles: ReadonlyArray<string>,
host: H): {host: H, indexName?: string, errors?: ts.Diagnostic[]} {
const files = rootFiles.filter(f => !DTS.test(f));
if (files.length != 1) {
let indexFile: string|undefined;
if (files.length === 1) {
indexFile = files[0];
} else {
for (const f of files) {
// Assume the shortest file path called index.ts is the entry point
if (f.endsWith(path.sep + 'index.ts')) {
if (!indexFile || indexFile.length > f.length) {
indexFile = f;
}
}
}
}
if (!indexFile) {
return {
host,
errors: [{
@ -75,8 +92,8 @@ export function createBundleIndexHost<H extends ts.CompilerHost>(
}]
};
}
const file = files[0];
const indexModule = file.replace(/\.ts$/, '');
const indexModule = indexFile.replace(/\.ts$/, '');
const bundler = new MetadataBundler(
indexModule, ngOptions.flatModuleId, new CompilerHostAdapter(host),
ngOptions.flatModulePrivateSymbolPrefix);

View File

@ -1,43 +0,0 @@
#!/usr/bin/env node
/**
* @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
*/
// Must be imported first, because Angular decorators throw on load.
import 'reflect-metadata';
import * as ts from 'typescript';
import * as path from 'path';
import {readCommandLineAndConfiguration} from '../main';
import {createBundleIndexHost} from './bundle_index_host';
import * as ng from '../transformers/entry_points';
export function main(args: string[], consoleError: (s: string) => void = console.error): number {
const {options, rootNames} = readCommandLineAndConfiguration(args);
const host = ng.createCompilerHost({options});
const {host: bundleHost, indexName, errors} = createBundleIndexHost(options, rootNames, host);
if (!indexName) {
console.error('Did not find an index.ts in the top-level of the package.');
return 1;
}
// The index file is synthetic, so we have to add it to the program after parsing the tsconfig
rootNames.push(indexName);
const program = ts.createProgram(rootNames, options, bundleHost);
const indexSourceFile = program.getSourceFile(indexName);
if (!indexSourceFile) {
console.error(`${indexSourceFile} is not in the program. Please file a bug.`);
return 1;
}
program.emit(indexSourceFile);
return 0;
}
// CLI entry point
if (require.main === module) {
const args = process.argv.slice(2);
process.exitCode = main(args);
}