From aa7f2c8dc7b8e4e5da62d113b200af5ad7265c83 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 26 Dec 2018 19:12:29 +0100 Subject: [PATCH] fix(bazel): flat module misses AMD module name on windows (#27839) * Fixes that the flat module out files do not have a proper AMD module name on Windows. This is currently blocking serving a `ng_module` using the Bazel TypeScript `devserver` on Windows. PR Close #27839 --- packages/bazel/src/ngc-wrapped/index.ts | 14 ++++--- packages/bazel/test/ngc-wrapped/BUILD.bazel | 16 ++++++++ .../test/ngc-wrapped/flat_module/BUILD.bazel | 16 ++++++++ .../test/ngc-wrapped/flat_module/export.ts | 9 +++++ .../test/ngc-wrapped/flat_module/index.ts | 9 +++++ .../ngc-wrapped/flat_module/tsconfig.json | 6 +++ .../test/ngc-wrapped/flat_module_test.ts | 40 +++++++++++++++++++ 7 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 packages/bazel/test/ngc-wrapped/flat_module/BUILD.bazel create mode 100644 packages/bazel/test/ngc-wrapped/flat_module/export.ts create mode 100644 packages/bazel/test/ngc-wrapped/flat_module/index.ts create mode 100644 packages/bazel/test/ngc-wrapped/flat_module/tsconfig.json create mode 100644 packages/bazel/test/ngc-wrapped/flat_module_test.ts diff --git a/packages/bazel/src/ngc-wrapped/index.ts b/packages/bazel/src/ngc-wrapped/index.ts index d875cd0bbe..d2cd4ead4f 100644 --- a/packages/bazel/src/ngc-wrapped/index.ts +++ b/packages/bazel/src/ngc-wrapped/index.ts @@ -199,25 +199,27 @@ export function compile({allDepsCompiledWithBazel = true, compilerOpts, tsHost, }; const origBazelHostShouldNameModule = bazelHost.shouldNameModule.bind(bazelHost); bazelHost.shouldNameModule = (fileName: string) => { + const flatModuleOutPath = + path.posix.join(bazelOpts.package, compilerOpts.flatModuleOutFile + '.ts'); + // The bundle index file is synthesized in bundle_index_host so it's not in the // compilationTargetSrc. // However we still want to give it an AMD module name for devmode. // We can't easily tell which file is the synthetic one, so we build up the path we expect // it to have and compare against that. - if (fileName === - path.join(compilerOpts.baseUrl, bazelOpts.package, compilerOpts.flatModuleOutFile + '.ts')) - return true; + if (fileName === path.posix.join(compilerOpts.baseUrl, flatModuleOutPath)) return true; + // Also handle the case the target is in an external repository. // Pull the workspace name from the target which is formatted as `@wksp//package:target` // if it the target is from an external workspace. If the target is from the local // workspace then it will be formatted as `//package:target`. const targetWorkspace = bazelOpts.target.split('/')[0].replace(/^@/, ''); + if (targetWorkspace && fileName === - path.join( - compilerOpts.baseUrl, 'external', targetWorkspace, bazelOpts.package, - compilerOpts.flatModuleOutFile + '.ts')) + path.posix.join(compilerOpts.baseUrl, 'external', targetWorkspace, flatModuleOutPath)) return true; + return origBazelHostShouldNameModule(fileName) || NGC_GEN_FILES.test(fileName); }; diff --git a/packages/bazel/test/ngc-wrapped/BUILD.bazel b/packages/bazel/test/ngc-wrapped/BUILD.bazel index aaa5aa9cd8..afec3725fe 100644 --- a/packages/bazel/test/ngc-wrapped/BUILD.bazel +++ b/packages/bazel/test/ngc-wrapped/BUILD.bazel @@ -35,3 +35,19 @@ jasmine_node_test( "@build_bazel_rules_typescript//third_party/github.com/bazelbuild/bazel/src/main/protobuf:worker_protocol.proto", ], ) + +ts_library( + name = "flat_module_test_lib", + testonly = True, + srcs = ["flat_module_test.ts"], + tsconfig = ":tsconfig.json", + deps = [ + "//packages/private/testing", + ], +) + +jasmine_node_test( + name = "flat_module_test", + srcs = [":flat_module_test_lib"], + data = ["//packages/bazel/test/ngc-wrapped/flat_module"], +) diff --git a/packages/bazel/test/ngc-wrapped/flat_module/BUILD.bazel b/packages/bazel/test/ngc-wrapped/flat_module/BUILD.bazel new file mode 100644 index 0000000000..9dc3f8f2f6 --- /dev/null +++ b/packages/bazel/test/ngc-wrapped/flat_module/BUILD.bazel @@ -0,0 +1,16 @@ +load("//tools:defaults.bzl", "ng_module") + +package(default_visibility = ["//packages/bazel/test:__subpackages__"]) + +ng_module( + name = "flat_module", + srcs = [ + "export.ts", + "index.ts", + ], + module_name = "flat_module", + tsconfig = ":tsconfig.json", + deps = [ + "//packages/core", + ], +) diff --git a/packages/bazel/test/ngc-wrapped/flat_module/export.ts b/packages/bazel/test/ngc-wrapped/flat_module/export.ts new file mode 100644 index 0000000000..22c8f6fd65 --- /dev/null +++ b/packages/bazel/test/ngc-wrapped/flat_module/export.ts @@ -0,0 +1,9 @@ +/** + * @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 + */ + +export const Test = 'This is a test export'; diff --git a/packages/bazel/test/ngc-wrapped/flat_module/index.ts b/packages/bazel/test/ngc-wrapped/flat_module/index.ts new file mode 100644 index 0000000000..9eec5fecb2 --- /dev/null +++ b/packages/bazel/test/ngc-wrapped/flat_module/index.ts @@ -0,0 +1,9 @@ +/** + * @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 + */ + +export * from './export'; diff --git a/packages/bazel/test/ngc-wrapped/flat_module/tsconfig.json b/packages/bazel/test/ngc-wrapped/flat_module/tsconfig.json new file mode 100644 index 0000000000..dff8f94228 --- /dev/null +++ b/packages/bazel/test/ngc-wrapped/flat_module/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "skipLibCheck": true, + "types": [] + } +} diff --git a/packages/bazel/test/ngc-wrapped/flat_module_test.ts b/packages/bazel/test/ngc-wrapped/flat_module_test.ts new file mode 100644 index 0000000000..b7fdec9b79 --- /dev/null +++ b/packages/bazel/test/ngc-wrapped/flat_module_test.ts @@ -0,0 +1,40 @@ +/** + * @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 {obsoleteInIvy, onlyInIvy} from '@angular/private/testing'; +import {existsSync, readFileSync} from 'fs'; +import {dirname, join} from 'path'; + +describe('flat_module ng_module', () => { + + let packageOutput: string; + let flatModuleOutFile: string; + + beforeAll(() => { + packageOutput = + dirname(require.resolve('angular/packages/bazel/test/ngc-wrapped/flat_module/index.js')); + flatModuleOutFile = join(packageOutput, 'flat_module.js'); + }); + + it('should have a flat module out file', + () => { expect(existsSync(flatModuleOutFile)).toBe(true); }); + + describe('flat module out file', () => { + + obsoleteInIvy('Ngtsc computes the AMD module name differently than NGC') + .it('should have a proper AMD module name', () => { + expect(readFileSync(flatModuleOutFile, 'utf8')) + .toContain(`define("flat_module/flat_module"`); + }); + + onlyInIvy('Ngtsc computes the AMD module name differently than NGC') + .it('should have a proper AMD module name', () => { + expect(readFileSync(flatModuleOutFile, 'utf8')).toContain(`define("flat_module"`); + }); + }); +});