Skip to content

Commit

Permalink
build: flatten esm5 sources before rollup
Browse files Browse the repository at this point in the history
this is needed to update to latest rules_nodejs due to breaking change in
bazelbuild/rules_nodejs#172
It has the side-effect of correctly marking rxjs packages as side-effect-free
  • Loading branch information
alexeagle committed Apr 3, 2018
1 parent 47ce6b9 commit 6d7b0be
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 710 deletions.
9 changes: 9 additions & 0 deletions .circleci/bazel.rc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ build --noshow_progress
# Don't run manual tests
test --test_tag_filters=-manual

# Print all the options that apply to the build.
# This helps us diagnose which options override others
# (e.g. /etc/bazel.bazelrc vs. tools/bazel.rc)
build --announce_rc

# Create dist/bin symlink to $(bazel info bazel-bin)
# We use this when uploading artifacts after the build finishes
build --symlink_prefix=dist/

# Enable experimental CircleCI bazel remote cache proxy
# See remote cache documentation in /docs/BAZEL.md
build --experimental_remote_spawn_cache --remote_rest_cache=http://localhost:7643
Expand Down
6 changes: 6 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,15 @@ jobs:
- store_artifacts:
path: dist/bin/packages/core/test/bundling/hello_world/bundle.min.js
destination: packages/core/test/bundling/hello_world/bundle.min.js
- store_artifacts:
path: dist/bin/packages/core/test/bundling/todo/bundle.min.js
destination: packages/core/test/bundling/todo/bundle.min.js
- store_artifacts:
path: dist/bin/packages/core/test/bundling/hello_world/bundle.min.js.brotli
destination: packages/core/test/bundling/hello_world/bundle.min.js.brotli
- store_artifacts:
path: dist/bin/packages/core/test/bundling/todo/bundle.min.js.brotli
destination: packages/core/test/bundling/todo/bundle.min.js.brotli

- save_cache:
key: *cache_key
Expand Down
6 changes: 3 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ workspace(name = "angular")

http_archive(
name = "build_bazel_rules_nodejs",
url = "https://github.com/bazelbuild/rules_nodejs/archive/0.6.0.zip",
strip_prefix = "rules_nodejs-0.6.0",
sha256 = "e8a2bb5ca51fbafb244bc507bcebcae33a63d969f47413b319a8dcce032845bf",
url = "https://github.com/bazelbuild/rules_nodejs/archive/cd368bd71a4b04fae0eafb5c5e2c906a93772584.zip",
strip_prefix = "rules_nodejs-cd368bd71a4b04fae0eafb5c5e2c906a93772584",
sha256 = "db74c61dd8bf73cc50aed56e78b7a8ad383f5869206901506cf8d3ee27f9277f",
)

load("@build_bazel_rules_nodejs//:defs.bzl", "check_bazel_version", "node_repositories", "yarn_install")
Expand Down
42 changes: 42 additions & 0 deletions packages/bazel/src/esm5.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 3 additions & 15 deletions packages/bazel/src/ng_package/ng_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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))
Expand Down
23 changes: 6 additions & 17 deletions packages/bazel/src/ng_rollup_bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down

0 comments on commit 6d7b0be

Please sign in to comment.