diff --git a/package.json b/package.json index 4622278830..24a257abd8 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "node_resolve_nested_main": "file:./tools/npm_packages/node_resolve_nested_main", "protobufjs": "6.8.8", "protractor": "^5.4.2", - "rollup": "1.0.0", + "rollup": "1.3.1", "rollup-plugin-amd": "^3.0.0", "rollup-plugin-commonjs": "^9.2.1", "rollup-plugin-json": "4.0.0", @@ -53,7 +53,7 @@ "source-map": "^0.7.3", "source-map-support": "0.5.9", "stylus": "~0.54.5", - "terser": "3.17.0", + "terser": "^4.2.1", "testy": "file:./tools/npm_packages/testy", "tmp": "0.1.0", "tsickle": "0.33.1", diff --git a/packages/index.bzl b/packages/index.bzl index 659b0bce97..0084636064 100644 --- a/packages/index.bzl +++ b/packages/index.bzl @@ -22,6 +22,7 @@ NESTED_PACKAGES = [ "labs", "less", "protractor", + "rollup", "stylus", "terser", "typescript", diff --git a/packages/rollup/BUILD.bazel b/packages/rollup/BUILD.bazel new file mode 100644 index 0000000000..4908386265 --- /dev/null +++ b/packages/rollup/BUILD.bazel @@ -0,0 +1,43 @@ +# Copyright 2017 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@build_bazel_rules_nodejs//:tools/defaults.bzl", "npm_package") + +# Ugly genrule depending on local linux environment to build the README out of skylark doc generation. +# Only referenced when we do a release. +# TODO: This ought to be possible with stardoc alone. Need to coordinate with Chris Parsons. +genrule( + name = "generate_README", + srcs = [ + "//packages/rollup/docs:index.md", + "//packages/rollup/docs:install.md", + ], + outs = ["README.md"], + cmd = """cat $(location //packages/rollup/docs:install.md) $(location //packages/rollup/docs:index.md) | sed 's/^##/\\\n##/' > $@""", + visibility = ["//docs:__pkg__"], +) + +npm_package( + name = "npm_package", + srcs = [ + "@npm_bazel_rollup//:package_contents", + ], + tags = ["do-not-publish"], + vendor_external = [ + "npm_bazel_rollup", + ], + deps = [ + ":generate_README", + ], +) diff --git a/packages/rollup/docs/BUILD.bazel b/packages/rollup/docs/BUILD.bazel new file mode 100644 index 0000000000..c141917f5f --- /dev/null +++ b/packages/rollup/docs/BUILD.bazel @@ -0,0 +1,24 @@ +load("//tools/stardoc:index.bzl", "stardoc") + +package(default_visibility = ["//visibility:public"]) + +exports_files(["install.md"]) + +stardoc( + name = "docs", + testonly = True, + out = "index.md", + input = "@npm_bazel_rollup//:index.bzl", + deps = [ + "@npm_bazel_rollup//:bzl", + # We need to restate local workspace dependencies here in `//foo:bzl` + # format to work-around a bug in stardoc where .bzl files from + # `@build_bazel_rules_nodejs//foo:bzl` style deps are not found + # by the doc generator: + # ``` + # Exception in thread "main" java.lang.IllegalStateException: File external/npm_bazel_karma/karma_node_test.bzl imported '@build_bazel_rules_nodejs//internal/common:devmode_js_sources.bzl', yet internal/common/devmode_js_sources.bzl was not found, even at roots [.]. + # ``` + "//internal/common:bzl", + "//internal/linker:bzl", + ], +) diff --git a/packages/rollup/docs/install.md b/packages/rollup/docs/install.md new file mode 100644 index 0000000000..e422bc0d27 --- /dev/null +++ b/packages/rollup/docs/install.md @@ -0,0 +1,33 @@ +# rollup rules for Bazel + +**WARNING: this is beta-quality software. Breaking changes are likely. Not recommended for production use without expert support.** + +The rollup rules run the rollup JS bundler with Bazel. + +Wraps the rollup CLI documented at https://rollupjs.org/guide/en/#command-line-reference + +## Installation + +Add the `@bazel/rollup` npm package to your `devDependencies` in `package.json`. + +Your `WORKSPACE` should declare a `yarn_install` or `npm_install` rule named `npm`. +It should then install the rules found in the npm packages using the `install_bazel_dependencies` function. +See https://github.com/bazelbuild/rules_nodejs/#quickstart + +This causes the `@bazel/rollup` package to be installed as a Bazel workspace named `npm_bazel_rollup`. + +## Installing with self-managed dependencies + +If you didn't use the `yarn_install` or `npm_install` rule to create an `npm` workspace, you'll have to declare a rule in your root `BUILD.bazel` file to execute rollup: + +```python +# Create a rollup rule to use in rollup_bundle#rollup_bin +# attribute when using self-managed dependencies +nodejs_binary( + name = "rollup_bin", + entry_point = "//:node_modules/rollup/bin/rollup", + # Point bazel to your node_modules to find the entry point + node_modules = ["//:node_modules"], +) +``` + diff --git a/packages/rollup/src/BUILD.bazel b/packages/rollup/src/BUILD.bazel new file mode 100644 index 0000000000..708531e0f9 --- /dev/null +++ b/packages/rollup/src/BUILD.bazel @@ -0,0 +1,40 @@ +# Copyright 2019 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@bazel_skylib//:bzl_library.bzl", "bzl_library") + +package(default_visibility = ["//visibility:public"]) + +exports_files(["rollup.config.js"]) + +bzl_library( + name = "bzl", + srcs = glob(["*.bzl"]), + deps = [ + "@build_bazel_rules_nodejs//:bzl", + "@build_bazel_rules_nodejs//internal/common:bzl", + "@build_bazel_rules_nodejs//internal/linker:bzl", + ], +) + +filegroup( + name = "package_contents", + srcs = [ + "BUILD.bazel", + "index.bzl", + "package.json", + "rollup.config.js", + "rollup_bundle.bzl", + ], +) diff --git a/packages/rollup/src/WORKSPACE b/packages/rollup/src/WORKSPACE new file mode 100644 index 0000000000..836d1efbe3 --- /dev/null +++ b/packages/rollup/src/WORKSPACE @@ -0,0 +1 @@ +workspace(name = "npm_bazel_rollup") diff --git a/packages/rollup/src/index.bzl b/packages/rollup/src/index.bzl new file mode 100644 index 0000000000..bb894187f8 --- /dev/null +++ b/packages/rollup/src/index.bzl @@ -0,0 +1,19 @@ +# Copyright 2019 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"Public API for terser rules" + +load(":rollup_bundle.bzl", _rollup_bundle = "rollup_bundle") + +rollup_bundle = _rollup_bundle diff --git a/packages/rollup/src/index.from_src.bzl b/packages/rollup/src/index.from_src.bzl new file mode 100644 index 0000000000..fb051d67ba --- /dev/null +++ b/packages/rollup/src/index.from_src.bzl @@ -0,0 +1,28 @@ +# Copyright 2019 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" Defaults for usage without @npm//@bazel/rollup +""" + +load( + ":index.bzl", + _rollup_bundle = "rollup_bundle", +) + +def rollup_bundle(**kwargs): + _rollup_bundle( + # Override to point to the one installed by build_bazel_rules_nodejs in the root + rollup_bin = "@npm//rollup/bin:rollup", + **kwargs + ) diff --git a/packages/rollup/src/package.json b/packages/rollup/src/package.json new file mode 100644 index 0000000000..7480415b27 --- /dev/null +++ b/packages/rollup/src/package.json @@ -0,0 +1,29 @@ +{ + "name": "@bazel/rollup", + "peerDependencies": { + "rollup": ">1.0.0 <2.0.0" + }, + "description": "Run rollup.js bundler under Bazel", + "license": "Apache-2.0", + "version": "0.0.0-PLACEHOLDER", + "repository": { + "type" : "git", + "url" : "https://github.com/bazelbuild/rules_nodejs.git", + "directory": "packages/rollup" + }, + "bugs": { + "url": "https://github.com/bazelbuild/rules_nodejs/issues" + }, + "keywords": [ + "rollup", + "bazel" + ], + "bin": { + "rollup": "index.js" + }, + "bazelWorkspaces": { + "npm_bazel_rollup": { + "rootPath": "." + } + } +} \ No newline at end of file diff --git a/packages/rollup/src/rollup.config.js b/packages/rollup/src/rollup.config.js new file mode 100644 index 0000000000..37e1bece8b --- /dev/null +++ b/packages/rollup/src/rollup.config.js @@ -0,0 +1,8 @@ +// Note: we cannot require() any plugins here because the user may not have any installed + +module.exports = { + output: { + name: 'bundle', + }, + plugins: [], +}; diff --git a/packages/rollup/src/rollup_bundle.bzl b/packages/rollup/src/rollup_bundle.bzl new file mode 100644 index 0000000000..01d58ec11c --- /dev/null +++ b/packages/rollup/src/rollup_bundle.bzl @@ -0,0 +1,181 @@ +"Rules for running Rollup under Bazel" + +load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "module_mappings_aspect", "register_node_modules_linker") + +_ROLLUP_ATTRS = { + "srcs": attr.label_list( + doc = """JavaScript source files from the workspace. + +The file passed to entry_point is automatically added. +""", + allow_files = [".js", ".json"], + ), + "config_file": attr.label( + doc = """A rollup.config.js file + +Passed to the --config +See https://rollupjs.org/guide/en/#configuration-files + +If not set, a default basic Rollup config is used. +""", + allow_single_file = True, + default = "@npm_bazel_rollup//:rollup.config.js", + ), + "entry_point": attr.label( + doc = """The bundle's entry point(s) (e.g. your main.js or app.js or index.js). + +Passed to the [`--input` option](https://github.com/rollup/rollup/blob/master/docs/999-big-list-of-options.md#input) in Rollup. +If you provide an array of entry points or an object mapping names to entry points, they will be bundled to separate output chunks. +""", + mandatory = True, + allow_single_file = True, + ), + "format": attr.string( + doc = """"Specifies the format of the generated bundle. One of the following: + +- `amd`: Asynchronous Module Definition, used with module loaders like RequireJS +- `cjs`: CommonJS, suitable for Node and other bundlers +- `esm`: Keep the bundle as an ES module file, suitable for other bundlers and inclusion as a `