From 6fc5940959c7ba79782243959321263dc4c873af Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Fri, 21 Jul 2017 14:20:34 -0700 Subject: [PATCH] build: Bazel builds ngfactories for packages/core (#18289) PR Close #18289 --- packages/compiler-cli/BUILD.bazel | 17 +++++++++ packages/compiler-cli/src/compiler_host.ts | 6 +-- packages/compiler-cli/src/extractor.ts | 2 +- .../transformers/module_filename_resolver.ts | 6 +-- packages/compiler/BUILD.bazel | 13 +++++++ packages/core/BUILD.bazel | 15 ++++++++ packages/tsc-wrapped/BUILD.bazel | 11 ++++++ packages/tsc-wrapped/tsconfig-build.json | 6 ++- tools/ngc-wrapped/BUILD.bazel | 19 ++++++++++ tools/ngc-wrapped/README.md | 6 +++ tools/ngc-wrapped/index.ts | 37 +++++++++++++++++++ tools/tsconfig.json | 11 ++++-- 12 files changed, 137 insertions(+), 12 deletions(-) create mode 100644 packages/compiler-cli/BUILD.bazel create mode 100644 packages/compiler/BUILD.bazel create mode 100644 packages/tsc-wrapped/BUILD.bazel create mode 100644 tools/ngc-wrapped/BUILD.bazel create mode 100644 tools/ngc-wrapped/README.md create mode 100644 tools/ngc-wrapped/index.ts diff --git a/packages/compiler-cli/BUILD.bazel b/packages/compiler-cli/BUILD.bazel new file mode 100644 index 0000000000..02d37f163f --- /dev/null +++ b/packages/compiler-cli/BUILD.bazel @@ -0,0 +1,17 @@ +package(default_visibility=["//visibility:public"]) +load("@build_bazel_rules_typescript//:defs.bzl", "ts_library") + +ts_library( + name = "compiler-cli", + srcs = glob(["**/*.ts"], exclude=[ + "integrationtest/**", + "test/**", + ]), + module_name = "@angular/compiler-cli", + deps = [ + "//packages/compiler", + "//packages/core", + "//packages/tsc-wrapped", + ], + tsconfig = ":tsconfig-build.json", +) diff --git a/packages/compiler-cli/src/compiler_host.ts b/packages/compiler-cli/src/compiler_host.ts index 03424e9ebc..04d5083770 100644 --- a/packages/compiler-cli/src/compiler_host.ts +++ b/packages/compiler-cli/src/compiler_host.ts @@ -41,8 +41,8 @@ export class CompilerHost implements AotCompilerHost { protected program: ts.Program, protected options: AngularCompilerOptions, protected context: CompilerHostContext, collectorOptions?: CollectorOptions) { // normalize the path so that it never ends with '/'. - this.basePath = path.normalize(path.join(this.options.basePath, '.')).replace(/\\/g, '/'); - this.genDir = path.normalize(path.join(this.options.genDir, '.')).replace(/\\/g, '/'); + this.basePath = path.normalize(path.join(this.options.basePath !, '.')).replace(/\\/g, '/'); + this.genDir = path.normalize(path.join(this.options.genDir !, '.')).replace(/\\/g, '/'); const genPath: string = path.relative(this.basePath, this.genDir); this.isGenDirChildOfRootDir = genPath === '' || !genPath.startsWith('..'); @@ -312,7 +312,7 @@ export class CompilerHost implements AotCompilerHost { relativePath = relativePath.substr(3); } - return path.join(this.options.genDir, relativePath); + return path.join(this.options.genDir !, relativePath); } private hasBundleIndex(filePath: string): boolean { diff --git a/packages/compiler-cli/src/extractor.ts b/packages/compiler-cli/src/extractor.ts index 99a6e8f9f7..e87a370d76 100644 --- a/packages/compiler-cli/src/extractor.ts +++ b/packages/compiler-cli/src/extractor.ts @@ -36,7 +36,7 @@ export class Extractor { return promiseBundle.then(bundle => { const content = this.serialize(bundle, formatName); const dstFile = outFile || `messages.${ext}`; - const dstPath = path.join(this.options.genDir, dstFile); + const dstPath = path.join(this.options.genDir !, dstFile); this.host.writeFile(dstPath, content, false); return [dstPath]; }); diff --git a/packages/compiler-cli/src/transformers/module_filename_resolver.ts b/packages/compiler-cli/src/transformers/module_filename_resolver.ts index 5cf3012e2b..9c224d85de 100644 --- a/packages/compiler-cli/src/transformers/module_filename_resolver.ts +++ b/packages/compiler-cli/src/transformers/module_filename_resolver.ts @@ -27,8 +27,8 @@ class SingleRootDirModuleFilenameResolver implements ModuleFilenameResolver { constructor(private host: ModuleFilenameResolutionHost, private options: CompilerOptions) { // normalize the path so that it never ends with '/'. - this.basePath = path.normalize(path.join(options.basePath, '.')).replace(/\\/g, '/'); - this.genDir = path.normalize(path.join(options.genDir, '.')).replace(/\\/g, '/'); + this.basePath = path.normalize(path.join(options.basePath !, '.')).replace(/\\/g, '/'); + this.genDir = path.normalize(path.join(options.genDir !, '.')).replace(/\\/g, '/'); const genPath: string = path.relative(this.basePath, this.genDir); this.isGenDirChildOfRootDir = genPath === '' || !genPath.startsWith('..'); @@ -153,7 +153,7 @@ class MultipleRootDirModuleFilenameResolver implements ModuleFilenameResolver { constructor(private host: ModuleFilenameResolutionHost, private options: CompilerOptions) { // normalize the path so that it never ends with '/'. - this.basePath = path.normalize(path.join(options.basePath, '.')).replace(/\\/g, '/'); + this.basePath = path.normalize(path.join(options.basePath !, '.')).replace(/\\/g, '/'); } getNgCanonicalFileName(fileName: string): string { diff --git a/packages/compiler/BUILD.bazel b/packages/compiler/BUILD.bazel new file mode 100644 index 0000000000..857fadd6e0 --- /dev/null +++ b/packages/compiler/BUILD.bazel @@ -0,0 +1,13 @@ +package(default_visibility=["//visibility:public"]) +load("@build_bazel_rules_typescript//:defs.bzl", "ts_library") + +ts_library( + name = "compiler", + srcs = glob(["**/*.ts"], exclude=[ + "test/**", + "testing/**", + ]), + module_name = "@angular/compiler", + deps = ["//packages/core"], + tsconfig = ":tsconfig-build.json", +) diff --git a/packages/core/BUILD.bazel b/packages/core/BUILD.bazel index d2c76b4d2a..b0654c1ca6 100644 --- a/packages/core/BUILD.bazel +++ b/packages/core/BUILD.bazel @@ -11,3 +11,18 @@ ts_library( deps = [], tsconfig = ":tsconfig-build.json", ) + +load("@build_bazel_rules_angular//:defs.bzl", "ng_module") + +ng_module( + name = "core_ng_module", + srcs = glob(["**/*.ts"], exclude=[ + "test/**", + "testing/**", + ]), + # Needed to allow (ts_library,ng_module) pair + write_ng_outputs_only = True, + module_name = "@angular/core", + tsconfig = ":tsconfig-build.json", + compiler = "//tools/ngc-wrapped" +) \ No newline at end of file diff --git a/packages/tsc-wrapped/BUILD.bazel b/packages/tsc-wrapped/BUILD.bazel new file mode 100644 index 0000000000..f86cf576c8 --- /dev/null +++ b/packages/tsc-wrapped/BUILD.bazel @@ -0,0 +1,11 @@ +package(default_visibility=["//visibility:public"]) +load("@build_bazel_rules_typescript//:defs.bzl", "ts_library") + +ts_library( + name = "tsc-wrapped", + srcs = glob(["**/*.ts"], exclude=[ + "test/**", + ]), + module_name = "@angular/tsc-wrapped", + tsconfig = ":tsconfig-build.json", +) diff --git a/packages/tsc-wrapped/tsconfig-build.json b/packages/tsc-wrapped/tsconfig-build.json index 16515d1707..6e660bde76 100644 --- a/packages/tsc-wrapped/tsconfig-build.json +++ b/packages/tsc-wrapped/tsconfig-build.json @@ -1,6 +1,10 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "outDir": "../../dist/packages-dist/tsc-wrapped" + "outDir": "../../dist/packages-dist/tsc-wrapped", + "types": [ + "node", + "jasmine" + ] } } \ No newline at end of file diff --git a/tools/ngc-wrapped/BUILD.bazel b/tools/ngc-wrapped/BUILD.bazel new file mode 100644 index 0000000000..fb5d5cf7ac --- /dev/null +++ b/tools/ngc-wrapped/BUILD.bazel @@ -0,0 +1,19 @@ +load("@build_bazel_rules_typescript//:defs.bzl", "ts_library", "nodejs_binary") + +licenses(["notice"]) # Apache 2.0 + +ts_library( + name = "ngc_lib", + srcs = ["index.ts"], + deps = [ + "//packages/compiler-cli", + "@build_bazel_rules_typescript//internal/tsc_wrapped" + ], +) + +nodejs_binary( + name = "ngc-wrapped", + entry_point = "__main__/tools/ngc-wrapped/index.js", + data = [":ngc_lib"], + visibility = ["//visibility:public"], +) \ No newline at end of file diff --git a/tools/ngc-wrapped/README.md b/tools/ngc-wrapped/README.md new file mode 100644 index 0000000000..4f4487e48b --- /dev/null +++ b/tools/ngc-wrapped/README.md @@ -0,0 +1,6 @@ +# ngc-wrapped + +This is a wrapper around @angular/compiler-cli that makes ngc run under Bazel. +It should be identical to https://github.com/bazelbuild/rules_angular/tree/master/internal/ngc +however that is built against Angular packages from npm, while ngc-wrapped is +built using Bazel against Angular at HEAD. diff --git a/tools/ngc-wrapped/index.ts b/tools/ngc-wrapped/index.ts new file mode 100644 index 0000000000..3929ccb05a --- /dev/null +++ b/tools/ngc-wrapped/index.ts @@ -0,0 +1,37 @@ +/** + * @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 + */ + +// TODO(chuckj): Remove the requirment for a fake 'reflect` implementation from +// the compiler +import 'reflect-metadata'; +import {ngc} from '@angular/compiler-cli'; +import * as fs from 'fs'; +// Note, the tsc_wrapped module comes from rules_typescript, not from @angular/tsc-wrapped +import {parseTsconfig} from 'tsc_wrapped'; + +function main(args: string[]) { + const [{options, bazelOpts, files, config}] = parseTsconfig(args[1]); + const ngOptions: {expectedOut: string[]} = (config as any).angularCompilerOptions; + + const result = ngc(args, undefined, files, options, ngOptions); + + if (result === 0) { + // Ensure that expected output files exist. + if (ngOptions && ngOptions.expectedOut) { + for (const out of ngOptions.expectedOut) { + fs.appendFileSync(out, '', 'utf-8'); + } + } + } + + return result; +} + +if (require.main === module) { + process.exitCode = main(process.argv.slice(2)); +} \ No newline at end of file diff --git a/tools/tsconfig.json b/tools/tsconfig.json index f16b62ee1d..58702a2c06 100644 --- a/tools/tsconfig.json +++ b/tools/tsconfig.json @@ -9,19 +9,22 @@ "outDir": "../dist/tools/", "noImplicitAny": true, "noFallthroughCasesInSwitch": true, - "paths": { - }, + "paths": {}, "rootDir": ".", "sourceMap": true, "inlineSources": true, - "lib": ["es6", "dom"], + "lib": [ + "es6", + "dom" + ], "target": "es5", "skipLibCheck": true }, "exclude": [ "node_modules", + "ngc-wrapped", "typings-test", "public_api_guard", "docs" ] -} +} \ No newline at end of file