Skip to content

Commit

Permalink
feat(rollup): new implementation of rollup_bundle in @bazel/rollup pa…
Browse files Browse the repository at this point in the history
…ckage

Fixes #532
Fixes #724
  • Loading branch information
alexeagle committed Sep 10, 2019
1 parent 65d8a36 commit 3873715
Show file tree
Hide file tree
Showing 13 changed files with 432 additions and 20 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ NESTED_PACKAGES = [
"labs",
"less",
"protractor",
"rollup",
"stylus",
"terser",
"typescript",
Expand Down
43 changes: 43 additions & 0 deletions packages/rollup/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
],
)
24 changes: 24 additions & 0 deletions packages/rollup/docs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
],
)
33 changes: 33 additions & 0 deletions packages/rollup/docs/install.md
Original file line number Diff line number Diff line change
@@ -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"],
)
```

40 changes: 40 additions & 0 deletions packages/rollup/src/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
],
)
1 change: 1 addition & 0 deletions packages/rollup/src/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
workspace(name = "npm_bazel_rollup")
19 changes: 19 additions & 0 deletions packages/rollup/src/index.bzl
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions packages/rollup/src/index.from_src.bzl
Original file line number Diff line number Diff line change
@@ -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
)
29 changes: 29 additions & 0 deletions packages/rollup/src/package.json
Original file line number Diff line number Diff line change
@@ -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": "."
}
}
}
8 changes: 8 additions & 0 deletions packages/rollup/src/rollup.config.js
Original file line number Diff line number Diff line change
@@ -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: [],
};
Loading

0 comments on commit 3873715

Please sign in to comment.