build: fix for ng_rollup_bundle outside of a sandbox (#33867)

Fixes issue introduced in https://github.com/angular/angular/pull/33808 for ng_rollup_bundle with `-spawn_strategy=standalone`. Without the sandbox (if --spawn_strategy=standalone is set) rollup can resolve to the non-esm .js file generated by ts_library instead of the desired .mjs. This fixes the problem by prioritizing .mjs.

Issue observed is of the flavor:

```
ERROR: modules/benchmarks/src/views/BUILD.bazel:20:1: Bundling JavaScript modules/benchmarks/src/views/bundle.es2015.js
[rollup] failed (Exit 1)
[!] Error: 'enableProdMode' is not exported by bazel-out/darwin-fastbuild/bin/packages/core/index.js
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
bazel-out/darwin-fastbuild/bin/modules/benchmarks/src/views/index.mjs (12:9)
10:  * found in the LICENSE file at https://angular.io/license
11:  */
12: import { enableProdMode } from '@angular/core';
             ^
13: import { platformBrowser } from '@angular/platform-browser';
14: import { ViewsBenchmarkModuleNgFactory } from './views-benchmark.ngfactory';
Error: 'enableProdMode' is not exported by bazel-out/darwin-fastbuild/bin/packages/core/index.js
```

PR Close #33867
This commit is contained in:
Greg Magolan 2019-11-15 21:46:22 -08:00 committed by Alex Rickabaugh
parent cec60b792c
commit 91a2506c82
1 changed files with 7 additions and 4 deletions

View File

@ -65,16 +65,19 @@ function resolveBazel(
return undefined; return undefined;
} }
} }
// Attempt to resovle first as specified, then in the following order // Attempt in the following order {importee}.mjs, {importee}/index.mjs,
// {importee}.mjs, {importee}/index.mjs, {importee}.js, {importee}/index.js. // {importee}, {importee}.js, {importee}/index.js. If an .mjs file is
// available it should be resolved as rollup cannot handle the .js files
// generated by ts_library as they are not esm. When rolling up esm5 files
// these are re-rooted so it is not an issue.
// TODO(gregmagolan): clean this up in the future as the .mjs es2015 outputs // TODO(gregmagolan): clean this up in the future as the .mjs es2015 outputs
// along side the .js es5 outputs from ts_library creates this unusual situation for // along side the .js es5 outputs from ts_library creates this unusual situation for
// which we can't rely on standard node module resolution to do the right thing. // which we can't rely on standard node module resolution to do the right thing.
// In the future ts_library (or equivalent) should only produce a single flavor of // In the future ts_library (or equivalent) should only produce a single flavor of
// output and ng_rollup_bundle should also just use the use the vanilla rollup_bundle // output and ng_rollup_bundle should also just use the use the vanilla rollup_bundle
// rule without the need for a custom bazel resolver. // rule without the need for a custom bazel resolver.
return tryImportee(importee) || tryImportee(`${importee}.mjs`) || return tryImportee(`${importee}.mjs`) || tryImportee(`${importee}/index.mjs`) ||
tryImportee(`${importee}/index.mjs`) || tryImportee(`${importee}.js`) || tryImportee(importee) || tryImportee(`${importee}.js`) ||
tryImportee(`${importee}/index.js`); tryImportee(`${importee}/index.js`);
} }