Skip to content

Commit

Permalink
chore: migrate to copy_file and copy_to_bin
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Dec 13, 2019
1 parent 306a0fd commit a4cd6b8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
13 changes: 6 additions & 7 deletions examples/kotlin/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Add rules here to build your software
# See https://docs.bazel.build/versions/master/build-ref.html#BUILD_files

load("@build_bazel_rules_nodejs//:index.bzl", "pkg_web")
load("@build_bazel_rules_nodejs//:index.bzl", "copy_to_bin", "pkg_web")
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_js_import", "kt_js_library")
load("@npm//http-server:index.bzl", "http_server")
load("@npm_bazel_jasmine//:index.bzl", "jasmine_node_test")
Expand All @@ -22,20 +22,19 @@ kt_js_library(
deps = [":kotlinx-html-js"],
)

# Copy bootstrap.js to the bin folder as _bootstrap.js
# so that relative import to `./hello.js` is valid
genrule(
# Copy bootstrap.js to the output folder, so all files are next to each other at runtime
# Allows the `./hello.js` relative import to work while referencing an output file
copy_to_bin(
name = "bootstrap",
srcs = ["bootstrap.js"],
outs = ["_bootstrap.js"],
cmd = "cp $< $@",
)

rollup_bundle(
name = "bundle",
srcs = ["hello.js"],
config_file = "rollup.config.js",
entry_point = "_bootstrap.js",
# Reference the copy of bootstrap.js in the output folder
entry_point = "bootstrap",
# TODO: make this example work with format = "esm"
format = "cjs",
output_dir = True,
Expand Down
8 changes: 4 additions & 4 deletions packages/create/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
load("@build_bazel_rules_nodejs//:tools/defaults.bzl", "nodejs_test", "npm_package")
load("//third_party/github.com/bazelbuild/bazel-skylib:rules/copy_file.bzl", "copy_file")

# Copy common bazelrc file to be included in this package
genrule(
copy_file(
name = "copy_bazelrc",
srcs = ["//:common.bazelrc"],
outs = ["common.bazelrc"],
cmd = "cp $< $@",
src = "//:common.bazelrc",
out = "common.bazelrc",
)

npm_package(
Expand Down
12 changes: 4 additions & 8 deletions packages/rollup/test/integration/foo/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
load("@npm_bazel_typescript//:index.from_src.bzl", "ts_library")
load("//third_party/github.com/bazelbuild/bazel-skylib:rules/copy_file.bzl", "copy_file")

package(default_visibility = ["//packages/rollup:__subpackages__"])

genrule(
copy_file(
name = "copy_user",
srcs = [
":user.mjs",
],
outs = [
":user.js",
],
cmd = "cp $< $@",
src = ":user.mjs",
out = ":user.js",
)

ts_library(
Expand Down
10 changes: 6 additions & 4 deletions packages/worker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

load("@build_bazel_rules_nodejs//:tools/defaults.bzl", "npm_package")
load("//third_party/github.com/bazelbuild/bazel-skylib:rules/copy_file.bzl", "copy_file")

# We reach inside the @bazel/typescript npm package to grab this one .js file
# This avoids a complex refactoring where we extract that .ts file from tsc_wrapped to a common library
Expand All @@ -22,6 +23,8 @@ _worker_path = "external/build_bazel_rules_typescript/internal/tsc_wrapped/worke
# so the runtime require() statements still work
_worker_proto_dir = "third_party/github.com/bazelbuild/bazel/src/main/protobuf"

# because the package is a tree artifact, there's no label to address the files we want
# and so we must use a custom cp command, not the copy_file rule
genrule(
name = "copy_worker_js",
srcs = ["@build_bazel_rules_typescript//:npm_bazel_typescript_package"],
Expand All @@ -36,11 +39,10 @@ genrule(
cmd = "cp $(location @build_bazel_rules_typescript//:npm_bazel_typescript_package)/%s.d.ts $@" % _worker_path,
)

genrule(
copy_file(
name = "copy_worker_proto",
srcs = ["@build_bazel_rules_typescript//%s:worker_protocol.proto" % _worker_proto_dir],
outs = ["%s/worker_protocol.proto" % _worker_proto_dir],
cmd = "cp $< $@",
src = "@build_bazel_rules_typescript//%s:worker_protocol.proto" % _worker_proto_dir,
out = "%s/worker_protocol.proto" % _worker_proto_dir,
)

npm_package(
Expand Down
8 changes: 4 additions & 4 deletions tools/defaults.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ load(
_npm_package = "npm_package",
)
load("@rules_codeowners//tools:codeowners.bzl", _codeowners = "codeowners")
load("//third_party/github.com/bazelbuild/bazel-skylib:rules/copy_file.bzl", "copy_file")

nodejs_test = _nodejs_test

def npm_package(**kwargs):
"Set some defaults for the npm_package rule"

# Every package should have a copy of the root LICENSE file
native.genrule(
copy_file(
name = "copy_LICENSE",
srcs = ["@build_bazel_rules_nodejs//:LICENSE"],
outs = ["LICENSE"],
cmd = "cp $< $@",
src = "@build_bazel_rules_nodejs//:LICENSE",
out = "LICENSE",
)

deps = [":copy_LICENSE"] + kwargs.pop("deps", [])
Expand Down

0 comments on commit a4cd6b8

Please sign in to comment.