@angular/bazel
Advanced tools
Comparing version 5.0.0-beta.6 to 5.0.0-beta.7
{ | ||
"name": "@angular/bazel", | ||
"version": "5.0.0-beta.6", | ||
"version": "5.0.0-beta.7", | ||
"description": "Angular - bazel build rules", | ||
@@ -8,7 +8,7 @@ "author": "angular", | ||
"peerDependencies": { | ||
"@angular/compiler-cli": "5.0.0-beta.6", | ||
"typescript": "~2.3" | ||
"@angular/compiler-cli": "5.0.0-beta.7", | ||
"typescript": "^2.4.2" | ||
}, | ||
"dependencies": { | ||
"@bazel/typescript": "0.0.9" | ||
"@bazel/typescript": "0.1.x" | ||
}, | ||
@@ -15,0 +15,0 @@ "repository": { |
@@ -9,3 +9,3 @@ /** | ||
import * as ng from '@angular/compiler-cli'; | ||
import {CachedFileLoader, CompilerHost, FileCache, FileLoader, UncachedFileLoader, debug, parseTsconfig, runAsWorker, runWorkerLoop} from '@bazel/typescript'; | ||
import {BazelOptions, CachedFileLoader, CompilerHost, FileCache, FileLoader, UncachedFileLoader, constructManifest, debug, parseTsconfig, runAsWorker, runWorkerLoop} from '@bazel/typescript'; | ||
import * as fs from 'fs'; | ||
@@ -17,7 +17,6 @@ import * as path from 'path'; | ||
const EXT = /(\.ts|\.d\.ts|\.js|\.jsx|\.tsx)$/; | ||
const NGC_GEN_FILES = /^(.*?)\.(ngfactory|ngsummary|ngstyle|shim\.ngstyle)(.*)$/; | ||
// FIXME: we should be able to add the assets to the tsconfig so FileLoader | ||
// knows about them | ||
const NGC_NON_TS_INPUTS = | ||
/(\.(ngsummary|ngstyle|ngfactory)(\.d)?\.ts|\.ngsummary\.json|\.css|\.html)$/; | ||
// FIXME should need only summary, css, html | ||
const NGC_ASSETS = /\.(css|html|ngsummary\.json)$/; | ||
@@ -28,35 +27,2 @@ // TODO(alexeagle): probably not needed, see | ||
function topologicalSort( | ||
result: tsickle.FileMap<boolean>, current: string, modulesManifest: tsickle.ModulesManifest, | ||
visiting: tsickle.FileMap<boolean>) { | ||
const referencedModules = modulesManifest.getReferencedModules(current); | ||
if (!referencedModules) return; // not in the local set of sources. | ||
for (const referencedModule of referencedModules) { | ||
const referencedFileName = modulesManifest.getFileNameFromModule(referencedModule); | ||
if (!referencedFileName) continue; // Ambient modules. | ||
if (!result[referencedFileName]) { | ||
if (visiting[referencedFileName]) { | ||
const path = current + ' -> ' + Object.keys(visiting).join(' -> '); | ||
throw new Error('Cyclical dependency between files:\n' + path); | ||
} | ||
visiting[referencedFileName] = true; | ||
topologicalSort(result, referencedFileName, modulesManifest, visiting); | ||
delete visiting[referencedFileName]; | ||
} | ||
} | ||
result[current] = true; | ||
} | ||
// TODO(alexeagle): move to tsc-wrapped in third_party so it's shared | ||
export function constructManifest( | ||
modulesManifest: tsickle.ModulesManifest, | ||
host: {flattenOutDir: (f: string) => string}): string { | ||
const result: tsickle.FileMap<boolean> = {}; | ||
for (const file of modulesManifest.fileNames) { | ||
topologicalSort(result, file, modulesManifest, {}); | ||
} | ||
// NB: The object literal maintains insertion order. | ||
return Object.keys(result).map(fn => host.flattenOutDir(fn)).join('\n') + '\n'; | ||
} | ||
export function main(args) { | ||
@@ -81,3 +47,8 @@ if (runAsWorker(args)) { | ||
fileLoader = new CachedFileLoader(fileCache, ALLOW_NON_HERMETIC_READS); | ||
fileCache.updateCache(inputs); | ||
// Resolve the inputs to absolute paths to match TypeScript internals | ||
const resolvedInputs: {[path: string]: string} = {}; | ||
for (const key of Object.keys(inputs)) { | ||
resolvedInputs[path.resolve(key)] = inputs[key]; | ||
} | ||
fileCache.updateCache(resolvedInputs); | ||
} else { | ||
@@ -87,25 +58,39 @@ fileLoader = new UncachedFileLoader(); | ||
const [{options: tsOptions, bazelOpts, files, config}] = parseTsconfig(project); | ||
const expectedOuts = config['angularCompilerOptions']['expectedOut']; | ||
const {basePath} = ng.calcProjectFileAndBasePath(project); | ||
const ngOptions = ng.createNgCompilerOptions(basePath, config, tsOptions); | ||
const compilerOpts = ng.createNgCompilerOptions(basePath, config, tsOptions); | ||
const {diagnostics} = compile({fileLoader, compilerOpts, bazelOpts, files, expectedOuts}); | ||
return diagnostics.every(d => d.category !== ts.DiagnosticCategory.Error); | ||
} | ||
export function relativeToRootDirs(filePath: string, rootDirs: string[]): string { | ||
if (!filePath) return filePath; | ||
// NB: the rootDirs should have been sorted longest-first | ||
for (const dir of rootDirs || []) { | ||
const rel = path.relative(dir, filePath); | ||
if (rel.indexOf('.') != 0) return rel; | ||
} | ||
return filePath; | ||
} | ||
export function compile( | ||
{fileLoader, compilerOpts, bazelOpts, files, expectedOuts, gatherDiagnostics}: { | ||
fileLoader: FileLoader, | ||
compilerOpts: ng.CompilerOptions, | ||
bazelOpts: BazelOptions, | ||
files: string[], | ||
expectedOuts: string[], gatherDiagnostics?: (program: ng.Program) => ng.Diagnostics | ||
}): {diagnostics: ng.Diagnostics, program: ng.Program} { | ||
if (!bazelOpts.es5Mode) { | ||
ngOptions.annotateForClosureCompiler = true; | ||
ngOptions.annotationsAs = 'static fields'; | ||
compilerOpts.annotateForClosureCompiler = true; | ||
compilerOpts.annotationsAs = 'static fields'; | ||
} | ||
if (!tsOptions.rootDirs) { | ||
if (!compilerOpts.rootDirs) { | ||
throw new Error('rootDirs is not set!'); | ||
} | ||
function relativeToRootDirs(filePath: string, rootDirs: string[]): string { | ||
if (!filePath) return filePath; | ||
// NB: the rootDirs should have been sorted longest-first | ||
for (const dir of rootDirs || []) { | ||
const rel = path.relative(dir, filePath); | ||
if (rel.indexOf('.') !== 0) return rel; | ||
} | ||
return filePath; | ||
} | ||
const expectedOuts = [...config['angularCompilerOptions']['expectedOut']]; | ||
const tsHost = ts.createCompilerHost(tsOptions, true); | ||
const writtenExpectedOuts = [...expectedOuts]; | ||
const tsHost = ts.createCompilerHost(compilerOpts, true); | ||
@@ -116,6 +101,6 @@ const originalWriteFile = tsHost.writeFile.bind(tsHost); | ||
onError?: (message: string) => void, sourceFiles?: ts.SourceFile[]) => { | ||
const relative = relativeToRootDirs(fileName, [tsOptions.rootDir]); | ||
const expectedIdx = expectedOuts.findIndex(o => o === relative); | ||
const relative = relativeToRootDirs(fileName, [compilerOpts.rootDir]); | ||
const expectedIdx = writtenExpectedOuts.findIndex(o => o === relative); | ||
if (expectedIdx >= 0) { | ||
expectedOuts.splice(expectedIdx, 1); | ||
writtenExpectedOuts.splice(expectedIdx, 1); | ||
originalWriteFile(fileName, content, writeByteOrderMark, onError, sourceFiles); | ||
@@ -125,3 +110,2 @@ } | ||
// Patch fileExists when resolving modules, so that ngc can ask TypeScript to | ||
@@ -132,3 +116,3 @@ // resolve non-existing generated files that don't exist on disk, but are | ||
generatedFileModuleResolverHost.fileExists = (fileName: string) => { | ||
const match = /^(.*?)\.(ngfactory|ngsummary|ngstyle|shim\.ngstyle)(.*)$/.exec(fileName); | ||
const match = NGC_GEN_FILES.exec(fileName); | ||
if (match) { | ||
@@ -156,30 +140,28 @@ const [, file, suffix, ext] = match; | ||
// TODO(alexeagle): does this also work in third_party? | ||
const allowNonHermeticRead = false; | ||
const bazelHost = new CompilerHost( | ||
files, tsOptions, bazelOpts, tsHost, fileLoader, ALLOW_NON_HERMETIC_READS, | ||
files, compilerOpts, bazelOpts, tsHost, fileLoader, ALLOW_NON_HERMETIC_READS, | ||
generatedFileModuleResolver); | ||
// The file cache is populated by Bazel with workspace-relative filenames | ||
// so we must relativize paths before looking them up in the cache. | ||
const originalGetSourceFile = bazelHost.getSourceFile.bind(bazelHost); | ||
bazelHost.getSourceFile = (fileName: string, languageVersion: ts.ScriptTarget) => { | ||
return originalGetSourceFile(relativeToRootDirs(fileName, [tsOptions.rootDir])); | ||
const origBazelHostFileExist = bazelHost.fileExists; | ||
bazelHost.fileExists = (fileName: string) => { | ||
if (NGC_ASSETS.test(fileName)) { | ||
return tsHost.fileExists(fileName); | ||
} | ||
return origBazelHostFileExist.call(bazelHost, fileName); | ||
}; | ||
// TODO(tbosch): fix tsickle to still run regular transformers even | ||
// if tsickle is not processing a file, and then remove this override, | ||
// as this is only required to keep the ng transformer running, | ||
// but produces e.g. too many externs. | ||
bazelHost.shouldSkipTsickleProcessing = (fileName: string): boolean => | ||
bazelOpts.compilationTargetSrc.indexOf(fileName) === -1 && !NGC_NON_TS_INPUTS.test(fileName); | ||
bazelOpts.compilationTargetSrc.indexOf(fileName) === -1 && !NGC_GEN_FILES.test(fileName); | ||
const ngHost = ng.createCompilerHost({options: ngOptions, tsHost: bazelHost}); | ||
const ngHost = ng.createCompilerHost({options: compilerOpts, tsHost: bazelHost}); | ||
ngHost.fileNameToModuleName = (importedFilePath: string, containingFilePath: string) => | ||
relativeToRootDirs(importedFilePath, tsOptions.rootDirs).replace(EXT, ''); | ||
relativeToRootDirs(importedFilePath, compilerOpts.rootDirs).replace(EXT, ''); | ||
ngHost.toSummaryFileName = (fileName: string, referringSrcFileName: string) => | ||
ngHost.fileNameToModuleName(fileName, referringSrcFileName); | ||
const tsickleOpts = { | ||
googmodule: bazelOpts.googmodule, | ||
es5Mode: bazelOpts.es5Mode, | ||
prelude: bazelOpts.prelude, | ||
untyped: bazelOpts.untyped, | ||
typeBlackListPaths: new Set(bazelOpts.typeBlackListPaths), | ||
transformDecorators: bazelOpts.tsickle, | ||
transformTypesToClosure: bazelOpts.tsickle, | ||
}; | ||
const emitCallback: ng.TsEmitCallback = ({ | ||
@@ -194,3 +176,3 @@ program, | ||
tsickle.emitWithTsickle( | ||
program, bazelHost, tsickleOpts, bazelHost, ngOptions, targetSourceFile, writeFile, | ||
program, bazelHost, bazelHost, compilerOpts, targetSourceFile, writeFile, | ||
cancellationToken, emitOnlyDtsFiles, { | ||
@@ -201,8 +183,8 @@ beforeTs: customTransformers.before, | ||
const {diagnostics, emitResult} = | ||
ng.performCompilation({rootNames: files, options: ngOptions, host: ngHost, emitCallback}); | ||
const {diagnostics, emitResult, program} = ng.performCompilation( | ||
{rootNames: files, options: compilerOpts, host: ngHost, emitCallback, gatherDiagnostics}); | ||
const tsickleEmitResult = emitResult as tsickle.EmitResult; | ||
let externs = '/** @externs */\n'; | ||
if (diagnostics.length) { | ||
console.error(ng.formatDiagnostics(ngOptions, diagnostics)); | ||
console.error(ng.formatDiagnostics(compilerOpts, diagnostics)); | ||
} else { | ||
@@ -225,7 +207,7 @@ if (bazelOpts.tsickleGenerateExterns) { | ||
for (const missing of expectedOuts) { | ||
for (const missing of writtenExpectedOuts) { | ||
originalWriteFile(missing, '', false); | ||
} | ||
return diagnostics.every(d => d.category !== ts.DiagnosticCategory.Error); | ||
return {program, diagnostics}; | ||
} | ||
@@ -232,0 +214,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17508
188
+ Added@angular/compiler@5.0.0-beta.7(transitive)
+ Added@angular/compiler-cli@5.0.0-beta.7(transitive)
+ Added@angular/tsc-wrapped@5.0.0-beta.7(transitive)
+ Added@bazel/typescript@0.1.0(transitive)
+ Addedtsickle@0.24.1(transitive)
+ Addedtypescript@2.4.22.9.2(transitive)
- Removed@angular/compiler@5.0.0-beta.6(transitive)
- Removed@angular/compiler-cli@5.0.0-beta.6(transitive)
- Removed@angular/tsc-wrapped@5.0.0-beta.6(transitive)
- Removed@bazel/typescript@0.0.9(transitive)
- Removedtsickle@0.23.6(transitive)
- Removedtypescript@2.3.4(transitive)
Updated@bazel/typescript@0.1.x