diff --git a/examples/kotlin/BUILD.bazel b/examples/kotlin/BUILD.bazel index dcf78d3f0f..4c26be4134 100644 --- a/examples/kotlin/BUILD.bazel +++ b/examples/kotlin/BUILD.bazel @@ -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") @@ -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, diff --git a/packages/create/BUILD.bazel b/packages/create/BUILD.bazel index 82468d0644..c0d126cf3a 100644 --- a/packages/create/BUILD.bazel +++ b/packages/create/BUILD.bazel @@ -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( diff --git a/packages/rollup/test/integration/foo/BUILD.bazel b/packages/rollup/test/integration/foo/BUILD.bazel index 4fb9dbee90..14b86b648f 100644 --- a/packages/rollup/test/integration/foo/BUILD.bazel +++ b/packages/rollup/test/integration/foo/BUILD.bazel @@ -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( diff --git a/packages/worker/BUILD.bazel b/packages/worker/BUILD.bazel index 448356a675..0a8676324f 100644 --- a/packages/worker/BUILD.bazel +++ b/packages/worker/BUILD.bazel @@ -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 @@ -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"], @@ -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( diff --git a/tools/defaults.bzl b/tools/defaults.bzl index 66488f9305..feed4843d7 100644 --- a/tools/defaults.bzl +++ b/tools/defaults.bzl @@ -10,6 +10,7 @@ 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 @@ -17,11 +18,10 @@ 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", [])