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
This commit is contained in:
Paul Gschwendtner 2018-12-26 19:12:29 +01:00 committed by Andrew Kushnir
parent 31fdff7121
commit 935ce63b73
7 changed files with 104 additions and 6 deletions

View File

@ -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);
};

View File

@ -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"],
)

View File

@ -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",
],
)

View File

@ -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';

View File

@ -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';

View File

@ -0,0 +1,6 @@
{
"compilerOptions": {
"skipLibCheck": true,
"types": []
}
}

View File

@ -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"`);
});
});
});