build: flatten esm5 sources before rollup (#23131)
this is needed to update to latest rules_nodejs due to breaking change in https://github.com/bazelbuild/rules_nodejs/pull/172 It has the side-effect of correctly marking rxjs packages as side-effect-free PR Close #23131
This commit is contained in:

committed by
Alex Rickabaugh

parent
d284404060
commit
580f05bd9c
@ -126,3 +126,45 @@ esm5_outputs_aspect = aspect(
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
def esm5_root_dir(ctx):
|
||||
return ctx.label.name + ".esm5"
|
||||
|
||||
def flatten_esm5(ctx):
|
||||
"""Merge together the .esm5 folders from the dependencies.
|
||||
|
||||
Two different dependencies A and B may have outputs like
|
||||
`bazel-bin/path/to/A.esm5/path/to/lib.js`
|
||||
`bazel-bin/path/to/B.esm5/path/to/main.js`
|
||||
|
||||
In order to run rollup on this app, in case main.js contains `import from './lib'`
|
||||
they need to be together in the same root directory, so if we depend on both A and B
|
||||
we need the outputs to be
|
||||
`bazel-bin/path/to/my_rule.esm5/path/to/lib.js`
|
||||
`bazel-bin/path/to/my_rule.esm5/path/to/main.js`
|
||||
|
||||
Args:
|
||||
ctx: the skylark rule execution context
|
||||
|
||||
Returns:
|
||||
list of flattened files
|
||||
"""
|
||||
esm5_sources = []
|
||||
result = []
|
||||
for dep in ctx.attr.deps:
|
||||
if ESM5Info in dep:
|
||||
transitive_output = dep[ESM5Info].transitive_output
|
||||
esm5_sources.extend(transitive_output.values())
|
||||
for f in depset(transitive = esm5_sources).to_list():
|
||||
path = f.short_path[f.short_path.find(".esm5") + len(".esm5"):]
|
||||
if (path.startswith("../")):
|
||||
path = "external/" + path[3:]
|
||||
rerooted_file = ctx.actions.declare_file("/".join([esm5_root_dir(ctx), path]))
|
||||
result.append(rerooted_file)
|
||||
# print("copy", f.short_path, "to", rerooted_file.short_path)
|
||||
ctx.actions.expand_template(
|
||||
output = rerooted_file,
|
||||
template = f,
|
||||
substitutions = {},
|
||||
)
|
||||
return result
|
||||
|
@ -16,7 +16,7 @@ load("@build_bazel_rules_nodejs//:internal/npm_package/npm_package.bzl",
|
||||
"NPM_PACKAGE_OUTPUTS",
|
||||
"create_package")
|
||||
load("@build_bazel_rules_nodejs//:internal/node.bzl", "sources_aspect")
|
||||
load("//packages/bazel/src:esm5.bzl", "esm5_outputs_aspect", "ESM5Info")
|
||||
load("//packages/bazel/src:esm5.bzl", "esm5_outputs_aspect", "flatten_esm5", "esm5_root_dir")
|
||||
|
||||
# TODO(alexeagle): this list is incomplete, add more as material ramps up
|
||||
WELL_KNOWN_GLOBALS = {
|
||||
@ -114,19 +114,7 @@ def _ng_package_impl(ctx):
|
||||
npm_package_directory = ctx.actions.declare_directory("%s.ng_pkg" % ctx.label.name)
|
||||
|
||||
esm_2015_files = collect_es6_sources(ctx)
|
||||
|
||||
esm5_sources = depset()
|
||||
root_dirs = []
|
||||
|
||||
for dep in ctx.attr.deps:
|
||||
if ESM5Info in dep:
|
||||
# TODO(alexeagle): we could make the module resolution in the rollup plugin
|
||||
# faster if we kept the files grouped with their root dir. This approach just
|
||||
# passes in both lists and requires multiple lookups (with expensive exception
|
||||
# handling) to locate the files again.
|
||||
transitive_output = dep[ESM5Info].transitive_output
|
||||
root_dirs.extend(transitive_output.keys())
|
||||
esm5_sources = depset(transitive=[esm5_sources] + transitive_output.values())
|
||||
esm5_sources = depset(flatten_esm5(ctx))
|
||||
|
||||
# These accumulators match the directory names where the files live in the
|
||||
# Angular package format.
|
||||
@ -190,7 +178,7 @@ def _ng_package_impl(ctx):
|
||||
umd_output = ctx.outputs.umd
|
||||
min_output = ctx.outputs.umd_min
|
||||
|
||||
config = write_rollup_config(ctx, [], root_dirs)
|
||||
config = write_rollup_config(ctx, [], "/".join([ctx.bin_dir.path, ctx.label.package, esm5_root_dir(ctx)]))
|
||||
|
||||
fesm2015.append(_rollup(ctx, config, es2015_entry_point, esm_2015_files, fesm2015_output))
|
||||
fesm5.append(_rollup(ctx, config, es5_entry_point, esm5_sources, fesm5_output))
|
||||
|
@ -18,11 +18,11 @@ load("@build_bazel_rules_nodejs//internal/rollup:rollup_bundle.bzl",
|
||||
"run_rollup",
|
||||
"run_uglify")
|
||||
load("@build_bazel_rules_nodejs//internal:collect_es6_sources.bzl", collect_es2015_sources = "collect_es6_sources")
|
||||
load(":esm5.bzl", "esm5_outputs_aspect", "ESM5Info")
|
||||
load(":esm5.bzl", "esm5_outputs_aspect", "flatten_esm5", "esm5_root_dir")
|
||||
|
||||
PACKAGES=["core", "common"]
|
||||
PACKAGES=["packages/core/src", "packages/common/src", "external/rxjs"]
|
||||
PLUGIN_CONFIG="{sideEffectFreeModules: [\n%s]}" % ",\n".join(
|
||||
[" 'packages/{0}/{0}.esm5'".format(p) for p in PACKAGES])
|
||||
[" '.esm5/{0}'".format(p) for p in PACKAGES])
|
||||
BO_ROLLUP="angular_devkit/packages/angular_devkit/build_optimizer/src/build-optimizer/rollup-plugin.js"
|
||||
BO_PLUGIN="require('%s').default(%s)" % (BO_ROLLUP, PLUGIN_CONFIG)
|
||||
|
||||
@ -41,21 +41,10 @@ def _ng_rollup_bundle(ctx):
|
||||
esm2015_rollup_config = write_rollup_config(ctx, filename = "_%s.rollup_es6.conf.js")
|
||||
run_rollup(ctx, collect_es2015_sources(ctx), esm2015_rollup_config, ctx.outputs.build_es6)
|
||||
|
||||
esm5_sources = []
|
||||
root_dirs = []
|
||||
esm5_sources = flatten_esm5(ctx)
|
||||
|
||||
for dep in ctx.attr.deps:
|
||||
if ESM5Info in dep:
|
||||
# TODO(alexeagle): we could make the module resolution in the rollup plugin
|
||||
# faster if we kept the files grouped with their root dir. This approach just
|
||||
# passes in both lists and requires multiple lookups (with expensive exception
|
||||
# handling) to locate the files again.
|
||||
transitive_output = dep[ESM5Info].transitive_output
|
||||
root_dirs.extend(transitive_output.keys())
|
||||
esm5_sources.extend(transitive_output.values())
|
||||
|
||||
rollup_config = write_rollup_config(ctx, [BO_PLUGIN], root_dirs)
|
||||
run_rollup(ctx, depset(transitive = esm5_sources).to_list(), rollup_config, ctx.outputs.build_es5)
|
||||
rollup_config = write_rollup_config(ctx, [BO_PLUGIN], "/".join([ctx.bin_dir.path, ctx.label.package, esm5_root_dir(ctx)]))
|
||||
run_rollup(ctx, esm5_sources, rollup_config, ctx.outputs.build_es5)
|
||||
|
||||
run_uglify(ctx, ctx.outputs.build_es5, ctx.outputs.build_es5_min,
|
||||
comments = False)
|
||||
|
Reference in New Issue
Block a user