Skip to content

Commit b1fa1bf

Browse files
devversionAndrewKushnir
authored andcommitted
fix(dev-infra): ng_rollup_bundle rule should error if import cannot be resolved (angular#42760)
Rollup just prints a warning if an import cannot be resolved and ends up being treated as an external dependency. This in combination with the `silent = True` attribute for `rollup_bundle` means that bundles might end up being extremely small without people noticing that it misses actual imports. To improve this situation, the warning is replaced by an error if an import cannot be resolved. This unveiles an issue with the `ng_rollup_bundle` macro from dev-infra where imports in View Engine were not resolved but ended up being treated as external. This did not prevent benchmarks using this macro from working because the ConcatJS devserver had builtin resolution for workspace manifest paths. Though given the new check for no unresolved imports, this will now cause errors within Rollup, and we need to fix the resolution. We can fix the issue by temporarily enabling workspace linking. This does not have any performance downsides. To enable workspace linking (which we might need more often in the future given the linker taking over patched module resolution), we had to rename the `angular` dependency to a more specific one so that the Angular linker could link into `node_modules/angular`. PR Close angular#42760
1 parent 9d58ebf commit b1fa1bf

File tree

12 files changed

+39
-19
lines changed

12 files changed

+39
-19
lines changed

BUILD.bazel

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ filegroup(
3636
srcs = [
3737
# We also declare the unminified AngularJS files since these can be used for
3838
# local debugging (e.g. see: packages/upgrade/test/common/test_helpers.ts)
39-
"@npm//:node_modules/angular/angular.js",
40-
"@npm//:node_modules/angular/angular.min.js",
4139
"@npm//:node_modules/angular-1.5/angular.js",
4240
"@npm//:node_modules/angular-1.5/angular.min.js",
4341
"@npm//:node_modules/angular-1.6/angular.js",
4442
"@npm//:node_modules/angular-1.6/angular.min.js",
4543
"@npm//:node_modules/angular-1.7/angular.js",
4644
"@npm//:node_modules/angular-1.7/angular.min.js",
47-
"@npm//:node_modules/angular-mocks/angular-mocks.js",
4845
"@npm//:node_modules/angular-mocks-1.5/angular-mocks.js",
4946
"@npm//:node_modules/angular-mocks-1.6/angular-mocks.js",
5047
"@npm//:node_modules/angular-mocks-1.7/angular-mocks.js",
48+
"@npm//:node_modules/angular-mocks-1.8/angular-mocks.js",
49+
"@npm//:node_modules/angular-1.8/angular.js",
50+
"@npm//:node_modules/angular-1.8/angular.min.js",
5151
],
5252
)
5353

dev-infra/benchmark/ng_rollup_bundle/ng_rollup_bundle.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def ng_rollup_bundle(
7171
silent = True,
7272
format = format,
7373
sourcemap = "true",
74+
# TODO(devversion): consider removing when View Engine is removed. View Engine
75+
# uses Bazel manifest path imports in generated factory files.
76+
# e.g. `import "<workspace_root>/<..>/some_file";`
77+
link_workspace_root = True,
7478
**kwargs
7579
)
7680

dev-infra/benchmark/ng_rollup_bundle/rollup.config-tmpl.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const plugins = [
4949
// https://github.com/angular/angular-cli/blob/1a1ceb609b9a87c4021cce3a6f0fc6d167cd09d2/packages/ngtools/webpack/src/angular_compiler_plugin.ts#L918-L920
5050
mainFields: ivyEnabled ? ['module_ivy_ngcc', 'main_ivy_ngcc', 'module', 'main'] :
5151
['module', 'main'],
52+
preferBuiltins: true,
5253
}),
5354
commonjs({ignoreGlobal: true}),
5455
sourcemaps(),
@@ -62,13 +63,26 @@ if (useBuildOptimizer) {
6263

6364
module.exports = {
6465
plugins,
66+
onwarn: customWarningHandler,
6567
external: [TMPL_external],
6668
output: {
6769
globals: {TMPL_globals},
6870
banner: extractBannerIfConfigured(),
6971
}
7072
};
7173

74+
/** Custom warning handler for Rollup. */
75+
function customWarningHandler(warning, defaultHandler) {
76+
// If rollup is unable to resolve an import, we want to throw an error
77+
// instead of silently treating the import as external dependency.
78+
// https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
79+
if (warning.code === 'UNRESOLVED_IMPORT') {
80+
throw Error(`Unresolved import: ${warning.message}`);
81+
}
82+
83+
defaultHandler(warning);
84+
}
85+
7286
/** Extracts the top-level bundle banner if specified. */
7387
function extractBannerIfConfigured() {
7488
if (!bannerFile) {

karma-js.conf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ module.exports = function(config) {
3636
{pattern: 'node_modules/angular-mocks-1.6/angular-mocks.js', included: false, watched: false},
3737
{pattern: 'node_modules/angular-1.7/angular?(.min).js', included: false, watched: false},
3838
{pattern: 'node_modules/angular-mocks-1.7/angular-mocks.js', included: false, watched: false},
39-
{pattern: 'node_modules/angular/angular?(.min).js', included: false, watched: false},
40-
{pattern: 'node_modules/angular-mocks/angular-mocks.js', included: false, watched: false},
39+
{pattern: 'node_modules/angular-1.8/angular?(.min).js', included: false, watched: false},
40+
{pattern: 'node_modules/angular-mocks-1.8/angular-mocks.js', included: false, watched: false},
4141

4242
'node_modules/core-js-bundle/index.js',
4343
'node_modules/jasmine-ajax/lib/mock-ajax.js',

modules/benchmarks/src/tree/ng1/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ ts_library(
1616

1717
ts_devserver(
1818
name = "devserver",
19-
bootstrap = ["@npm//:node_modules/angular/angular.js"],
19+
bootstrap = ["@npm//:node_modules/angular-1.8/angular.js"],
2020
entry_module = "angular/modules/benchmarks/src/tree/ng1/index",
2121
port = 4200,
2222
static_files = ["index.html"],

modules/playground/src/upgrade/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ts_devserver(
2020
bootstrap = [
2121
"//packages/zone.js/bundles:zone.umd.js",
2222
"@npm//:node_modules/reflect-metadata/Reflect.js",
23-
"@npm//:node_modules/angular/angular.js",
23+
"@npm//:node_modules/angular-1.8/angular.js",
2424
],
2525
entry_module = "angular/modules/playground/src/upgrade/index",
2626
port = 4200,

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@
9494
"@types/yaml": "^1.9.7",
9595
"@types/yargs": "^16.0.1",
9696
"@webcomponents/custom-elements": "^1.1.0",
97-
"angular": "npm:angular@1.8",
9897
"angular-1.5": "npm:angular@1.5",
9998
"angular-1.6": "npm:angular@1.6",
10099
"angular-1.7": "npm:angular@1.7",
101-
"angular-mocks": "npm:angular-mocks@1.8",
100+
"angular-1.8": "npm:angular@1.8",
102101
"angular-mocks-1.5": "npm:angular-mocks@1.5",
103102
"angular-mocks-1.6": "npm:angular-mocks@1.6",
104103
"angular-mocks-1.7": "npm:angular-mocks@1.7",
104+
"angular-mocks-1.8": "npm:angular-mocks@1.8",
105105
"base64-js": "1.5.1",
106106
"bluebird": "^3.7.2",
107107
"brotli": "^1.3.2",

packages/examples/upgrade/upgrade_example.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def create_upgrade_example_targets(name, srcs, e2e_srcs, entry_module, assets =
4545
additional_root_paths = ["angular/packages/examples"],
4646
bootstrap = [
4747
"//packages/zone.js/bundles:zone.umd.js",
48-
"@npm//:node_modules/angular/angular.js",
48+
"@npm//:node_modules/angular-1.8/angular.js",
4949
"@npm//:node_modules/reflect-metadata/Reflect.js",
5050
],
5151
static_files = [

packages/language-service/bundles/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ng_rollup_bundle(
66
entry_point = "//packages/language-service:src/ts_plugin.ts",
77
format = "amd",
88
globals = {
9+
"os": "os",
910
"fs": "fs",
1011
"path": "path",
1112
"typescript": "ts",
@@ -26,6 +27,7 @@ ng_rollup_bundle(
2627
entry_point = "//packages/language-service/ivy:ts_plugin.ts",
2728
format = "amd",
2829
globals = {
30+
"os": "os",
2931
"fs": "fs",
3032
"path": "path",
3133
"typescript": "ts",

packages/upgrade/src/common/test/helpers/common_test_helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const ng1Versions = [
2727
},
2828
{
2929
label: '1.8',
30-
files: [`angular/${ANGULARJS_FILENAME}`, 'angular-mocks/angular-mocks.js'],
30+
files: [`angular-1.8/${ANGULARJS_FILENAME}`, 'angular-mocks-1.8/angular-mocks.js'],
3131
},
3232
];
3333

0 commit comments

Comments
 (0)