build: fix ng_package & ng_rollup_bundle windows issues (#33607)

Fixes ng_package and ng_rollup_bundle rollup issue on Windows where .js file was resolved by bazel resolved instead of .mjs file as there is no sandbox.

Fixes https://github.com/angular/angular/issues/32603 by passing globals and external through templated rollup config as Windows CLI argument limit can be easily exceeded. Also fixes this for ng_rollup_bundle.

PR Close #33607
This commit is contained in:
Greg Magolan 2019-11-05 16:56:25 -08:00 committed by atscott
parent 1c22e464b2
commit cf598f71fd
4 changed files with 44 additions and 25 deletions

View File

@ -197,7 +197,7 @@ def _compute_node_modules_root(ctx):
node_modules_root = "external/npm/node_modules" node_modules_root = "external/npm/node_modules"
return node_modules_root return node_modules_root
def _write_rollup_config(ctx, root_dir, filename = "_%s.rollup.conf.js"): def _write_rollup_config(ctx, root_dir, filename = "_%s.rollup.conf.js", include_tslib = False):
"""Generate a rollup config file. """Generate a rollup config file.
Args: Args:
@ -220,6 +220,15 @@ def _write_rollup_config(ctx, root_dir, filename = "_%s.rollup.conf.js"):
(dep.label, k, mappings[k], v)), "deps") (dep.label, k, mappings[k], v)), "deps")
mappings[k] = v mappings[k] = v
globals = dict(WELL_KNOWN_GLOBALS, **ctx.attr.globals)
external = globals.keys()
if not include_tslib:
external.append("tslib")
# Pass external & globals through a templated config file because on Windows there is
# an argument limit and we there might be a lot of globals which need to be passed to
# rollup.
ctx.actions.expand_template( ctx.actions.expand_template(
output = config, output = config,
template = ctx.file.rollup_config_tmpl, template = ctx.file.rollup_config_tmpl,
@ -230,17 +239,19 @@ def _write_rollup_config(ctx, root_dir, filename = "_%s.rollup.conf.js"):
"TMPL_root_dir": root_dir, "TMPL_root_dir": root_dir,
"TMPL_stamp_data": "\"%s\"" % ctx.version_file.path if ctx.version_file else "undefined", "TMPL_stamp_data": "\"%s\"" % ctx.version_file.path if ctx.version_file else "undefined",
"TMPL_workspace_name": ctx.workspace_name, "TMPL_workspace_name": ctx.workspace_name,
"TMPL_external": ", ".join(["'%s'" % e for e in external]),
"TMPL_globals": ", ".join(["'%s': '%s'" % g for g in globals.items()]),
}, },
) )
return config return config
def _run_rollup(ctx, bundle_name, rollup_config, entry_point, inputs, js_output, format, module_name = "", include_tslib = False): def _run_rollup(ctx, bundle_name, rollup_config, entry_point, inputs, js_output, format, module_name = ""):
map_output = ctx.actions.declare_file(js_output.basename + ".map", sibling = js_output) map_output = ctx.actions.declare_file(js_output.basename + ".map", sibling = js_output)
args = ctx.actions.args() args = ctx.actions.args()
args.add("--config", rollup_config)
args.add("--input", entry_point) args.add("--input", entry_point)
args.add("--config", rollup_config)
args.add("--output.file", js_output) args.add("--output.file", js_output)
args.add("--output.format", format) args.add("--output.format", format)
if module_name: if module_name:
@ -268,18 +279,6 @@ def _run_rollup(ctx, bundle_name, rollup_config, entry_point, inputs, js_output,
args.add("--preserveSymlinks") args.add("--preserveSymlinks")
globals = dict(WELL_KNOWN_GLOBALS, **ctx.attr.globals)
external = globals.keys()
if not include_tslib:
external.append("tslib")
args.add_joined("--external", external, join_with = ",")
args.add_joined(
"--globals",
["%s:%s" % g for g in globals.items()],
join_with = ",",
)
# We will produce errors as needed. Anything else is spammy: a well-behaved # We will produce errors as needed. Anything else is spammy: a well-behaved
# bazel rule prints nothing on success. # bazel rule prints nothing on success.
args.add("--silent") args.add("--silent")
@ -496,6 +495,7 @@ def _ng_package_impl(ctx):
esm2015_config = _write_rollup_config(ctx, ctx.bin_dir.path, filename = "_%s.rollup_esm2015.conf.js") esm2015_config = _write_rollup_config(ctx, ctx.bin_dir.path, filename = "_%s.rollup_esm2015.conf.js")
esm5_config = _write_rollup_config(ctx, "/".join([ctx.bin_dir.path, ctx.label.package, esm5_root_dir(ctx)]), filename = "_%s.rollup_esm5.conf.js") esm5_config = _write_rollup_config(ctx, "/".join([ctx.bin_dir.path, ctx.label.package, esm5_root_dir(ctx)]), filename = "_%s.rollup_esm5.conf.js")
esm5_tslib_config = _write_rollup_config(ctx, "/".join([ctx.bin_dir.path, ctx.label.package, esm5_root_dir(ctx)]), filename = "_%s.rollup_esm5_tslib.conf.js", include_tslib = True)
fesm2015.append( fesm2015.append(
_run_rollup( _run_rollup(
@ -525,13 +525,12 @@ def _ng_package_impl(ctx):
_run_rollup( _run_rollup(
ctx, ctx,
"umd", "umd",
esm5_config, esm5_tslib_config,
es5_entry_point, es5_entry_point,
esm5_rollup_inputs, esm5_rollup_inputs,
umd_output, umd_output,
module_name = module_name, module_name = module_name,
format = "umd", format = "umd",
include_tslib = True,
), ),
) )
terser_sourcemap = _terser( terser_sourcemap = _terser(

View File

@ -113,6 +113,13 @@ function resolveBazel(
} }
if (resolved) { if (resolved) {
if (path.extname(resolved) == '.js') {
// check for .mjs file and prioritize that
const resolved_mjs = resolved.substr(0, resolved.length - 3) + '.mjs';
if (fileExists(resolved_mjs)) {
resolved = resolved_mjs;
}
}
log_verbose(`resolved to ${resolved}`); log_verbose(`resolved to ${resolved}`);
} else { } else {
log_verbose(`allowing rollup to resolve '${importee}' with node module resolution`); log_verbose(`allowing rollup to resolve '${importee}' with node module resolution`);
@ -152,8 +159,10 @@ const plugins = [
const config = { const config = {
plugins, plugins,
external: [TMPL_external],
output: { output: {
banner, globals: {TMPL_globals},
banner,
} }
}; };

View File

@ -255,6 +255,12 @@ def _write_rollup_config(ctx, root_dir, build_optimizer, filename = "_%s.rollup.
(dep.label, k, mappings[k], v)), "deps") (dep.label, k, mappings[k], v)), "deps")
mappings[k] = v mappings[k] = v
globals = {}
external = []
if ctx.attr.globals:
globals = ctx.attr.globals.items()
external = ctx.attr.globals.keys()
ctx.actions.expand_template( ctx.actions.expand_template(
output = config, output = config,
template = ctx.file._rollup_config_tmpl, template = ctx.file._rollup_config_tmpl,
@ -266,6 +272,8 @@ def _write_rollup_config(ctx, root_dir, build_optimizer, filename = "_%s.rollup.
"TMPL_root_dir": root_dir, "TMPL_root_dir": root_dir,
"TMPL_stamp_data": "\"%s\"" % ctx.version_file.path if ctx.version_file else "undefined", "TMPL_stamp_data": "\"%s\"" % ctx.version_file.path if ctx.version_file else "undefined",
"TMPL_workspace_name": ctx.workspace_name, "TMPL_workspace_name": ctx.workspace_name,
"TMPL_external": ", ".join(["'%s'" % e for e in external]),
"TMPL_globals": ", ".join(["'%s': '%s'" % g for g in globals]),
}, },
) )
@ -295,12 +303,6 @@ def _run_rollup(ctx, entry_point_path, sources, config):
args.add("--preserveSymlinks") args.add("--preserveSymlinks")
if ctx.attr.globals:
args.add("--external")
args.add_joined(ctx.attr.globals.keys(), join_with = ",")
args.add("--globals")
args.add_joined(["%s:%s" % g for g in ctx.attr.globals.items()], join_with = ",")
direct_inputs = [config] direct_inputs = [config]
# Also include files from npm fine grained deps as inputs. # Also include files from npm fine grained deps as inputs.

View File

@ -117,6 +117,13 @@ function resolveBazel(
} }
if (resolved) { if (resolved) {
if (path.extname(resolved) == '.js') {
// check for .mjs file and prioritize that
const resolved_mjs = resolved.substr(0, resolved.length - 3) + '.mjs';
if (fileExists(resolved_mjs)) {
resolved = resolved_mjs;
}
}
log_verbose(`resolved to ${resolved}`); log_verbose(`resolved to ${resolved}`);
} else { } else {
log_verbose(`allowing rollup to resolve '${importee}' with node module resolution`); log_verbose(`allowing rollup to resolve '${importee}' with node module resolution`);
@ -174,8 +181,10 @@ if (bannerFile) {
const config = { const config = {
plugins, plugins,
external: [TMPL_external],
output: { output: {
banner, globals: {TMPL_globals},
banner,
} }
}; };