fix(dev-infra): `ng_rollup_bundle` rule should error if import cannot be resolved (#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 #42760
This commit is contained in:
Paul Gschwendtner 2021-07-06 15:29:19 +02:00 committed by Andrew Kushnir
parent 9d58ebfcee
commit b1fa1bf0d5
12 changed files with 39 additions and 19 deletions

View File

@ -36,18 +36,18 @@ filegroup(
srcs = [ srcs = [
# We also declare the unminified AngularJS files since these can be used for # We also declare the unminified AngularJS files since these can be used for
# local debugging (e.g. see: packages/upgrade/test/common/test_helpers.ts) # local debugging (e.g. see: packages/upgrade/test/common/test_helpers.ts)
"@npm//:node_modules/angular/angular.js",
"@npm//:node_modules/angular/angular.min.js",
"@npm//:node_modules/angular-1.5/angular.js", "@npm//:node_modules/angular-1.5/angular.js",
"@npm//:node_modules/angular-1.5/angular.min.js", "@npm//:node_modules/angular-1.5/angular.min.js",
"@npm//:node_modules/angular-1.6/angular.js", "@npm//:node_modules/angular-1.6/angular.js",
"@npm//:node_modules/angular-1.6/angular.min.js", "@npm//:node_modules/angular-1.6/angular.min.js",
"@npm//:node_modules/angular-1.7/angular.js", "@npm//:node_modules/angular-1.7/angular.js",
"@npm//:node_modules/angular-1.7/angular.min.js", "@npm//:node_modules/angular-1.7/angular.min.js",
"@npm//:node_modules/angular-mocks/angular-mocks.js",
"@npm//:node_modules/angular-mocks-1.5/angular-mocks.js", "@npm//:node_modules/angular-mocks-1.5/angular-mocks.js",
"@npm//:node_modules/angular-mocks-1.6/angular-mocks.js", "@npm//:node_modules/angular-mocks-1.6/angular-mocks.js",
"@npm//:node_modules/angular-mocks-1.7/angular-mocks.js", "@npm//:node_modules/angular-mocks-1.7/angular-mocks.js",
"@npm//:node_modules/angular-mocks-1.8/angular-mocks.js",
"@npm//:node_modules/angular-1.8/angular.js",
"@npm//:node_modules/angular-1.8/angular.min.js",
], ],
) )

View File

@ -71,6 +71,10 @@ def ng_rollup_bundle(
silent = True, silent = True,
format = format, format = format,
sourcemap = "true", sourcemap = "true",
# TODO(devversion): consider removing when View Engine is removed. View Engine
# uses Bazel manifest path imports in generated factory files.
# e.g. `import "<workspace_root>/<..>/some_file";`
link_workspace_root = True,
**kwargs **kwargs
) )

View File

@ -49,6 +49,7 @@ const plugins = [
// https://github.com/angular/angular-cli/blob/1a1ceb609b9a87c4021cce3a6f0fc6d167cd09d2/packages/ngtools/webpack/src/angular_compiler_plugin.ts#L918-L920 // https://github.com/angular/angular-cli/blob/1a1ceb609b9a87c4021cce3a6f0fc6d167cd09d2/packages/ngtools/webpack/src/angular_compiler_plugin.ts#L918-L920
mainFields: ivyEnabled ? ['module_ivy_ngcc', 'main_ivy_ngcc', 'module', 'main'] : mainFields: ivyEnabled ? ['module_ivy_ngcc', 'main_ivy_ngcc', 'module', 'main'] :
['module', 'main'], ['module', 'main'],
preferBuiltins: true,
}), }),
commonjs({ignoreGlobal: true}), commonjs({ignoreGlobal: true}),
sourcemaps(), sourcemaps(),
@ -62,6 +63,7 @@ if (useBuildOptimizer) {
module.exports = { module.exports = {
plugins, plugins,
onwarn: customWarningHandler,
external: [TMPL_external], external: [TMPL_external],
output: { output: {
globals: {TMPL_globals}, globals: {TMPL_globals},
@ -69,6 +71,18 @@ module.exports = {
} }
}; };
/** Custom warning handler for Rollup. */
function customWarningHandler(warning, defaultHandler) {
// If rollup is unable to resolve an import, we want to throw an error
// instead of silently treating the import as external dependency.
// https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
if (warning.code === 'UNRESOLVED_IMPORT') {
throw Error(`Unresolved import: ${warning.message}`);
}
defaultHandler(warning);
}
/** Extracts the top-level bundle banner if specified. */ /** Extracts the top-level bundle banner if specified. */
function extractBannerIfConfigured() { function extractBannerIfConfigured() {
if (!bannerFile) { if (!bannerFile) {

View File

@ -36,8 +36,8 @@ module.exports = function(config) {
{pattern: 'node_modules/angular-mocks-1.6/angular-mocks.js', included: false, watched: false}, {pattern: 'node_modules/angular-mocks-1.6/angular-mocks.js', included: false, watched: false},
{pattern: 'node_modules/angular-1.7/angular?(.min).js', included: false, watched: false}, {pattern: 'node_modules/angular-1.7/angular?(.min).js', included: false, watched: false},
{pattern: 'node_modules/angular-mocks-1.7/angular-mocks.js', included: false, watched: false}, {pattern: 'node_modules/angular-mocks-1.7/angular-mocks.js', included: false, watched: false},
{pattern: 'node_modules/angular/angular?(.min).js', included: false, watched: false}, {pattern: 'node_modules/angular-1.8/angular?(.min).js', included: false, watched: false},
{pattern: 'node_modules/angular-mocks/angular-mocks.js', included: false, watched: false}, {pattern: 'node_modules/angular-mocks-1.8/angular-mocks.js', included: false, watched: false},
'node_modules/core-js-bundle/index.js', 'node_modules/core-js-bundle/index.js',
'node_modules/jasmine-ajax/lib/mock-ajax.js', 'node_modules/jasmine-ajax/lib/mock-ajax.js',

View File

@ -16,7 +16,7 @@ ts_library(
ts_devserver( ts_devserver(
name = "devserver", name = "devserver",
bootstrap = ["@npm//:node_modules/angular/angular.js"], bootstrap = ["@npm//:node_modules/angular-1.8/angular.js"],
entry_module = "angular/modules/benchmarks/src/tree/ng1/index", entry_module = "angular/modules/benchmarks/src/tree/ng1/index",
port = 4200, port = 4200,
static_files = ["index.html"], static_files = ["index.html"],

View File

@ -20,7 +20,7 @@ ts_devserver(
bootstrap = [ bootstrap = [
"//packages/zone.js/bundles:zone.umd.js", "//packages/zone.js/bundles:zone.umd.js",
"@npm//:node_modules/reflect-metadata/Reflect.js", "@npm//:node_modules/reflect-metadata/Reflect.js",
"@npm//:node_modules/angular/angular.js", "@npm//:node_modules/angular-1.8/angular.js",
], ],
entry_module = "angular/modules/playground/src/upgrade/index", entry_module = "angular/modules/playground/src/upgrade/index",
port = 4200, port = 4200,

View File

@ -94,14 +94,14 @@
"@types/yaml": "^1.9.7", "@types/yaml": "^1.9.7",
"@types/yargs": "^16.0.1", "@types/yargs": "^16.0.1",
"@webcomponents/custom-elements": "^1.1.0", "@webcomponents/custom-elements": "^1.1.0",
"angular": "npm:angular@1.8",
"angular-1.5": "npm:angular@1.5", "angular-1.5": "npm:angular@1.5",
"angular-1.6": "npm:angular@1.6", "angular-1.6": "npm:angular@1.6",
"angular-1.7": "npm:angular@1.7", "angular-1.7": "npm:angular@1.7",
"angular-mocks": "npm:angular-mocks@1.8", "angular-1.8": "npm:angular@1.8",
"angular-mocks-1.5": "npm:angular-mocks@1.5", "angular-mocks-1.5": "npm:angular-mocks@1.5",
"angular-mocks-1.6": "npm:angular-mocks@1.6", "angular-mocks-1.6": "npm:angular-mocks@1.6",
"angular-mocks-1.7": "npm:angular-mocks@1.7", "angular-mocks-1.7": "npm:angular-mocks@1.7",
"angular-mocks-1.8": "npm:angular-mocks@1.8",
"base64-js": "1.5.1", "base64-js": "1.5.1",
"bluebird": "^3.7.2", "bluebird": "^3.7.2",
"brotli": "^1.3.2", "brotli": "^1.3.2",

View File

@ -45,7 +45,7 @@ def create_upgrade_example_targets(name, srcs, e2e_srcs, entry_module, assets =
additional_root_paths = ["angular/packages/examples"], additional_root_paths = ["angular/packages/examples"],
bootstrap = [ bootstrap = [
"//packages/zone.js/bundles:zone.umd.js", "//packages/zone.js/bundles:zone.umd.js",
"@npm//:node_modules/angular/angular.js", "@npm//:node_modules/angular-1.8/angular.js",
"@npm//:node_modules/reflect-metadata/Reflect.js", "@npm//:node_modules/reflect-metadata/Reflect.js",
], ],
static_files = [ static_files = [

View File

@ -6,6 +6,7 @@ ng_rollup_bundle(
entry_point = "//packages/language-service:src/ts_plugin.ts", entry_point = "//packages/language-service:src/ts_plugin.ts",
format = "amd", format = "amd",
globals = { globals = {
"os": "os",
"fs": "fs", "fs": "fs",
"path": "path", "path": "path",
"typescript": "ts", "typescript": "ts",
@ -26,6 +27,7 @@ ng_rollup_bundle(
entry_point = "//packages/language-service/ivy:ts_plugin.ts", entry_point = "//packages/language-service/ivy:ts_plugin.ts",
format = "amd", format = "amd",
globals = { globals = {
"os": "os",
"fs": "fs", "fs": "fs",
"path": "path", "path": "path",
"typescript": "ts", "typescript": "ts",

View File

@ -27,7 +27,7 @@ const ng1Versions = [
}, },
{ {
label: '1.8', label: '1.8',
files: [`angular/${ANGULARJS_FILENAME}`, 'angular-mocks/angular-mocks.js'], files: [`angular-1.8/${ANGULARJS_FILENAME}`, 'angular-mocks-1.8/angular-mocks.js'],
}, },
]; ];

View File

@ -35,14 +35,14 @@
"@types/babel__traverse", "@types/babel__traverse",
"@types/node", "@types/node",
"@types/selenium-webdriver", "@types/selenium-webdriver",
"angular",
"angular-1.5", "angular-1.5",
"angular-1.6", "angular-1.6",
"angular-1.7", "angular-1.7",
"angular-mocks", "angular-1.8",
"angular-mocks-1.5", "angular-mocks-1.5",
"angular-mocks-1.6", "angular-mocks-1.6",
"angular-mocks-1.7", "angular-mocks-1.7",
"angular-mocks-1.8",
"puppeteer", "puppeteer",
"rollup", "rollup",
"selenium-webdriver", "selenium-webdriver",

View File

@ -2291,6 +2291,11 @@ alphanum-sort@^1.0.2:
resolved "https://registry.yarnpkg.com/angular/-/angular-1.7.9.tgz#e52616e8701c17724c3c238cfe4f9446fd570bc4" resolved "https://registry.yarnpkg.com/angular/-/angular-1.7.9.tgz#e52616e8701c17724c3c238cfe4f9446fd570bc4"
integrity sha512-5se7ZpcOtu0MBFlzGv5dsM1quQDoDeUTwZrWjGtTNA7O88cD8TEk5IEKCTDa3uECV9XnvKREVUr7du1ACiWGFQ== integrity sha512-5se7ZpcOtu0MBFlzGv5dsM1quQDoDeUTwZrWjGtTNA7O88cD8TEk5IEKCTDa3uECV9XnvKREVUr7du1ACiWGFQ==
"angular-1.8@npm:angular@1.8":
version "1.8.2"
resolved "https://registry.yarnpkg.com/angular/-/angular-1.8.2.tgz#5983bbb5a9fa63e213cb7749199e0d352de3a2f1"
integrity sha512-IauMOej2xEe7/7Ennahkbb5qd/HFADiNuLSESz9Q27inmi32zB0lnAsFeLEWcox3Gd1F6YhNd1CP7/9IukJ0Gw==
"angular-mocks-1.5@npm:angular-mocks@1.5": "angular-mocks-1.5@npm:angular-mocks@1.5":
version "1.5.11" version "1.5.11"
resolved "https://registry.yarnpkg.com/angular-mocks/-/angular-mocks-1.5.11.tgz#a0e1dd0ea55fd77ee7a757d75536c5e964c86f81" resolved "https://registry.yarnpkg.com/angular-mocks/-/angular-mocks-1.5.11.tgz#a0e1dd0ea55fd77ee7a757d75536c5e964c86f81"
@ -2306,16 +2311,11 @@ alphanum-sort@^1.0.2:
resolved "https://registry.yarnpkg.com/angular-mocks/-/angular-mocks-1.7.9.tgz#0a3b7e28b9a493b4e3010ed2b0f69a68e9b4f79b" resolved "https://registry.yarnpkg.com/angular-mocks/-/angular-mocks-1.7.9.tgz#0a3b7e28b9a493b4e3010ed2b0f69a68e9b4f79b"
integrity sha512-LQRqqiV3sZ7NTHBnNmLT0bXtE5e81t97+hkJ56oU0k3dqKv1s6F+nBWRlOVzqHWPGFOiPS8ZJVdrS8DFzHyNIA== integrity sha512-LQRqqiV3sZ7NTHBnNmLT0bXtE5e81t97+hkJ56oU0k3dqKv1s6F+nBWRlOVzqHWPGFOiPS8ZJVdrS8DFzHyNIA==
"angular-mocks@npm:angular-mocks@1.8": "angular-mocks-1.8@npm:angular-mocks@1.8":
version "1.8.2" version "1.8.2"
resolved "https://registry.yarnpkg.com/angular-mocks/-/angular-mocks-1.8.2.tgz#dc022420b86978cf317a8447c116c0be73a853bf" resolved "https://registry.yarnpkg.com/angular-mocks/-/angular-mocks-1.8.2.tgz#dc022420b86978cf317a8447c116c0be73a853bf"
integrity sha512-I5L3P0l21HPdVsP4A4qWmENt4ePjjbkDFdAzOaM7QiibFySbt14DptPbt2IjeG4vFBr4vSLbhIz8Fk03DISl8Q== integrity sha512-I5L3P0l21HPdVsP4A4qWmENt4ePjjbkDFdAzOaM7QiibFySbt14DptPbt2IjeG4vFBr4vSLbhIz8Fk03DISl8Q==
"angular@npm:angular@1.8":
version "1.8.2"
resolved "https://registry.yarnpkg.com/angular/-/angular-1.8.2.tgz#5983bbb5a9fa63e213cb7749199e0d352de3a2f1"
integrity sha512-IauMOej2xEe7/7Ennahkbb5qd/HFADiNuLSESz9Q27inmi32zB0lnAsFeLEWcox3Gd1F6YhNd1CP7/9IukJ0Gw==
ansi-align@^3.0.0: ansi-align@^3.0.0:
version "3.0.0" version "3.0.0"
resolved "https://registry.yarnpkg.com/ansi-align/-/ansi-align-3.0.0.tgz#b536b371cf687caaef236c18d3e21fe3797467cb" resolved "https://registry.yarnpkg.com/ansi-align/-/ansi-align-3.0.0.tgz#b536b371cf687caaef236c18d3e21fe3797467cb"