Skip to content

Commit

Permalink
feat(rollup): support esm configurations to be provided (#3435)
Browse files Browse the repository at this point in the history
Adds support for ESM (.mjs) configurations when using `@bazel/rollup`.
This is useful for using e.g. ESM rollup plugins, or the Angular linker
Babel plugin (as of v13+ Angular packages ship as strict ESM).
  • Loading branch information
devversion authored May 2, 2022
1 parent 43008b9 commit 7bac805
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/Rollup.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ Defaults to `@npm//@bazel/rollup/bin:rollup-worker`

(*Boolean*): Whether to execute the rollup binary with the --silent flag, defaults to False.

Using --silent can cause rollup to [ignore errors/warnings](https://github.com/rollup/rollup/blob/master/docs/999-big-list-of-options.md#onwarn)
Using --silent can cause rollup to [ignore errors/warnings](https://github.com/rollup/rollup/blob/master/docs/999-big-list-of-options.md#onwarn)
which are only surfaced via logging. Since bazel expects printing nothing on success, setting silent to True
is a more Bazel-idiomatic experience, however could cause rollup to drop important warnings.

Expand Down
4 changes: 4 additions & 0 deletions npm_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def npm_deps():
"//tools/npm_packages/bazel_workspaces_consistent:BUILD.bazel",
"//tools/npm_packages/bazel_workspaces_consistent:index.bzl",
"//tools/npm_packages/bazel_workspaces_consistent:package.json",
"//:tools/npm_packages/test_esm_pkg/package.json",
"//:tools/npm_packages/test_esm_pkg/index.mjs",
"//:tools/npm_packages/hello/package.json",
"//:tools/npm_packages/hello/index.js",
"//:tools/npm_packages/node_resolve_index/index.js",
Expand Down Expand Up @@ -91,6 +93,8 @@ js_library(
"//tools/npm_packages/bazel_workspaces_consistent:BUILD.bazel",
"//tools/npm_packages/bazel_workspaces_consistent:index.bzl",
"//tools/npm_packages/bazel_workspaces_consistent:package.json",
"//:tools/npm_packages/test_esm_pkg/package.json",
"//:tools/npm_packages/test_esm_pkg/index.mjs",
"//:tools/npm_packages/hello/package.json",
"//:tools/npm_packages/hello/index.js",
"//:tools/npm_packages/node_resolve_index/index.js",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"source-map": "^0.7.3",
"source-map-support": "0.5.9",
"terser": "4.4.0",
"test_esm_pkg": "file:./tools/npm_packages/test_esm_pkg",
"testy": "file:./tools/npm_packages/testy",
"tmp": "0.1.0",
"ts-lit-plugin": "1.1.9",
Expand Down
5 changes: 3 additions & 2 deletions packages/rollup/rollup_bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Otherwise, the outputs are assumed to be a single file.
"silent": attr.bool(
doc = """Whether to execute the rollup binary with the --silent flag, defaults to False.
Using --silent can cause rollup to [ignore errors/warnings](https://github.com/rollup/rollup/blob/master/docs/999-big-list-of-options.md#onwarn)
Using --silent can cause rollup to [ignore errors/warnings](https://github.com/rollup/rollup/blob/master/docs/999-big-list-of-options.md#onwarn)
which are only surfaced via logging. Since bazel expects printing nothing on success, setting silent to True
is a more Bazel-idiomatic experience, however could cause rollup to drop important warnings.
""",
Expand Down Expand Up @@ -312,7 +312,8 @@ def _rollup_bundle(ctx):

stamp = ctx.attr.stamp[StampSettingInfo].value

config = ctx.actions.declare_file("_%s.rollup_config.js" % ctx.label.name)
config_extension = ctx.file.config_file.extension
config = ctx.actions.declare_file("_%s.rollup_config.%s" % (ctx.label.name, config_extension))
ctx.actions.expand_template(
template = ctx.file.config_file,
output = config,
Expand Down
20 changes: 20 additions & 0 deletions packages/rollup/test/esm_config/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test")
load("//packages/rollup:index.bzl", "rollup_bundle")

rollup_bundle(
name = "esm_config",
config_file = "rollup.config.mjs",
entry_point = "input.js",
sourcemap = "false",
supports_workers = True,
deps = [
# Fake test ESM package from `npm_deps.bzl`.
"@npm//test_esm_pkg",
],
)

generated_file_test(
name = "test",
src = "golden.js_",
generated = ":esm_config.js",
)
1 change: 1 addition & 0 deletions packages/rollup/test/esm_config/golden.js_
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Works - test fixture');
1 change: 1 addition & 0 deletions packages/rollup/test/esm_config/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Works - test fixture');
7 changes: 7 additions & 0 deletions packages/rollup/test/esm_config/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Note: We want to have some actual import statements here,
// validating that ESM is actually used for this config file.
import 'test_esm_pkg';

export default {
plugins: [],
};
1 change: 1 addition & 0 deletions tools/npm_packages/test_esm_pkg/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const greetings = 'Hello';
8 changes: 8 additions & 0 deletions tools/npm_packages/test_esm_pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"version": "0.0.1",
"exports": {
".": {
"import": "./index.mjs"
}
}
}
3 changes: 3 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10367,6 +10367,9 @@ test-exclude@^6.0.0:
glob "^7.1.4"
minimatch "^3.0.4"

"test_esm_pkg@file:./tools/npm_packages/test_esm_pkg":
version "0.0.1"

"testy@file:./tools/npm_packages/testy":
version "0.0.1"

Expand Down

0 comments on commit 7bac805

Please sign in to comment.