Skip to content

Commit

Permalink
feat: introduce "core" package at /nodejs
Browse files Browse the repository at this point in the history
Motivation is described by #2892

Some advantages of this approach:
- We now follow the style guide at https://docs.bazel.build/versions/main/skylark/deploying.html
- Gives us a 'clean room' to eventually reduce the scope of the project.
- Allows others to develop their own competing nodejs rules without having to start from scratch.
- Allows the user to declare two different node versions since they control the name of
  the external repository we create
  • Loading branch information
alexeagle committed Sep 11, 2021
1 parent d0f184f commit a32cf5c
Show file tree
Hide file tree
Showing 23 changed files with 866 additions and 723 deletions.
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import %workspace%/common.bazelrc
# This lets us glob() up all the files inside the examples to make them inputs to tests
# To update these lines, just run `yarn bazel:update-deleted-packages`
# (Note, we cannot use common --deleted_packages because the bazel version command doesn't support it)
build --deleted_packages=e2e/bazel_managed_deps,e2e/concatjs_devserver,e2e/concatjs_devserver/genrule,e2e/concatjs_devserver/subpackage,e2e/concatjs_devserver_directory_artifacts,e2e/concatjs_devserver_directory_artifacts/genrule,e2e/concatjs_devserver_directory_artifacts/subpackage,e2e/coverage,e2e/fine_grained_symlinks,e2e/jasmine,e2e/node_loader_no_preserve_symlinks,e2e/node_loader_preserve_symlinks,e2e/nodejs_image,e2e/nodejs_image/foolib,e2e/nodejs_repository,e2e/packages,e2e/symlinked_node_modules_npm,e2e/symlinked_node_modules_yarn,e2e/typescript,e2e/webapp,examples/angular,examples/angular/e2e,examples/angular/src,examples/angular/src/app,examples/angular/src/app/hello-world,examples/angular/src/app/home,examples/angular/src/app/todos,examples/angular/src/app/todos/reducers,examples/angular/src/assets,examples/angular/src/lib/shorten,examples/angular/src/shared/material,examples/angular/tools,examples/angular_bazel_architect,examples/angular_bazel_architect/projects/frontend-lib,examples/angular_view_engine,examples/angular_view_engine/e2e,examples/angular_view_engine/src,examples/angular_view_engine/src/app,examples/angular_view_engine/src/app/hello-world,examples/angular_view_engine/src/app/home,examples/angular_view_engine/src/app/todos,examples/angular_view_engine/src/app/todos/reducers,examples/angular_view_engine/src/assets,examples/angular_view_engine/src/lib/shorten,examples/angular_view_engine/src/lib/typography,examples/angular_view_engine/src/shared/material,examples/app,examples/app/styles,examples/app/test,examples/closure,examples/create-react-app,examples/cypress,examples/esbuild,examples/esbuild/src,examples/from_source,examples/jest,examples/jest/ts,examples/jest/ts/src,examples/jest/ts/test,examples/kotlin,examples/nestjs,examples/nestjs/src,examples/parcel,examples/protobufjs,examples/protocol_buffers,examples/react_webpack,examples/user_managed_deps,examples/vendored_node,examples/vendored_node_and_yarn,examples/vue,examples/vue/src,examples/vue/src/components/HelloWorld,examples/web_testing,examples/webapp,examples/worker
query --deleted_packages=e2e/bazel_managed_deps,e2e/concatjs_devserver,e2e/concatjs_devserver/genrule,e2e/concatjs_devserver/subpackage,e2e/concatjs_devserver_directory_artifacts,e2e/concatjs_devserver_directory_artifacts/genrule,e2e/concatjs_devserver_directory_artifacts/subpackage,e2e/coverage,e2e/fine_grained_symlinks,e2e/jasmine,e2e/node_loader_no_preserve_symlinks,e2e/node_loader_preserve_symlinks,e2e/nodejs_image,e2e/nodejs_image/foolib,e2e/nodejs_repository,e2e/packages,e2e/symlinked_node_modules_npm,e2e/symlinked_node_modules_yarn,e2e/typescript,e2e/webapp,examples/angular,examples/angular/e2e,examples/angular/src,examples/angular/src/app,examples/angular/src/app/hello-world,examples/angular/src/app/home,examples/angular/src/app/todos,examples/angular/src/app/todos/reducers,examples/angular/src/assets,examples/angular/src/lib/shorten,examples/angular/src/shared/material,examples/angular/tools,examples/angular_bazel_architect,examples/angular_bazel_architect/projects/frontend-lib,examples/angular_view_engine,examples/angular_view_engine/e2e,examples/angular_view_engine/src,examples/angular_view_engine/src/app,examples/angular_view_engine/src/app/hello-world,examples/angular_view_engine/src/app/home,examples/angular_view_engine/src/app/todos,examples/angular_view_engine/src/app/todos/reducers,examples/angular_view_engine/src/assets,examples/angular_view_engine/src/lib/shorten,examples/angular_view_engine/src/lib/typography,examples/angular_view_engine/src/shared/material,examples/app,examples/app/styles,examples/app/test,examples/closure,examples/create-react-app,examples/cypress,examples/esbuild,examples/esbuild/src,examples/from_source,examples/jest,examples/jest/ts,examples/jest/ts/src,examples/jest/ts/test,examples/kotlin,examples/nestjs,examples/nestjs/src,examples/parcel,examples/protobufjs,examples/protocol_buffers,examples/react_webpack,examples/user_managed_deps,examples/vendored_node,examples/vendored_node_and_yarn,examples/vue,examples/vue/src,examples/vue/src/components/HelloWorld,examples/web_testing,examples/webapp,examples/worker
build --deleted_packages=e2e/bazel_managed_deps,e2e/concatjs_devserver,e2e/concatjs_devserver/genrule,e2e/concatjs_devserver/subpackage,e2e/concatjs_devserver_directory_artifacts,e2e/concatjs_devserver_directory_artifacts/genrule,e2e/concatjs_devserver_directory_artifacts/subpackage,e2e/core,e2e/coverage,e2e/fine_grained_symlinks,e2e/jasmine,e2e/node_loader_no_preserve_symlinks,e2e/node_loader_preserve_symlinks,e2e/nodejs_image,e2e/nodejs_image/foolib,e2e/nodejs_repository,e2e/packages,e2e/symlinked_node_modules_npm,e2e/symlinked_node_modules_yarn,e2e/typescript,e2e/webapp,examples/angular,examples/angular/e2e,examples/angular/src,examples/angular/src/app,examples/angular/src/app/hello-world,examples/angular/src/app/home,examples/angular/src/app/todos,examples/angular/src/app/todos/reducers,examples/angular/src/assets,examples/angular/src/lib/shorten,examples/angular/src/shared/material,examples/angular/tools,examples/angular_bazel_architect,examples/angular_bazel_architect/projects/frontend-lib,examples/angular_view_engine,examples/angular_view_engine/e2e,examples/angular_view_engine/src,examples/angular_view_engine/src/app,examples/angular_view_engine/src/app/hello-world,examples/angular_view_engine/src/app/home,examples/angular_view_engine/src/app/todos,examples/angular_view_engine/src/app/todos/reducers,examples/angular_view_engine/src/assets,examples/angular_view_engine/src/lib/shorten,examples/angular_view_engine/src/lib/typography,examples/angular_view_engine/src/shared/material,examples/app,examples/app/styles,examples/app/test,examples/closure,examples/create-react-app,examples/cypress,examples/esbuild,examples/esbuild/src,examples/from_source,examples/jest,examples/jest/ts,examples/jest/ts/src,examples/jest/ts/test,examples/kotlin,examples/nestjs,examples/nestjs/src,examples/parcel,examples/protobufjs,examples/protocol_buffers,examples/react_webpack,examples/user_managed_deps,examples/vendored_node,examples/vendored_node_and_yarn,examples/vue,examples/vue/src,examples/vue/src/components/HelloWorld,examples/web_testing,examples/webapp,examples/worker
query --deleted_packages=e2e/bazel_managed_deps,e2e/concatjs_devserver,e2e/concatjs_devserver/genrule,e2e/concatjs_devserver/subpackage,e2e/concatjs_devserver_directory_artifacts,e2e/concatjs_devserver_directory_artifacts/genrule,e2e/concatjs_devserver_directory_artifacts/subpackage,e2e/core,e2e/coverage,e2e/fine_grained_symlinks,e2e/jasmine,e2e/node_loader_no_preserve_symlinks,e2e/node_loader_preserve_symlinks,e2e/nodejs_image,e2e/nodejs_image/foolib,e2e/nodejs_repository,e2e/packages,e2e/symlinked_node_modules_npm,e2e/symlinked_node_modules_yarn,e2e/typescript,e2e/webapp,examples/angular,examples/angular/e2e,examples/angular/src,examples/angular/src/app,examples/angular/src/app/hello-world,examples/angular/src/app/home,examples/angular/src/app/todos,examples/angular/src/app/todos/reducers,examples/angular/src/assets,examples/angular/src/lib/shorten,examples/angular/src/shared/material,examples/angular/tools,examples/angular_bazel_architect,examples/angular_bazel_architect/projects/frontend-lib,examples/angular_view_engine,examples/angular_view_engine/e2e,examples/angular_view_engine/src,examples/angular_view_engine/src/app,examples/angular_view_engine/src/app/hello-world,examples/angular_view_engine/src/app/home,examples/angular_view_engine/src/app/todos,examples/angular_view_engine/src/app/todos/reducers,examples/angular_view_engine/src/assets,examples/angular_view_engine/src/lib/shorten,examples/angular_view_engine/src/lib/typography,examples/angular_view_engine/src/shared/material,examples/app,examples/app/styles,examples/app/test,examples/closure,examples/create-react-app,examples/cypress,examples/esbuild,examples/esbuild/src,examples/from_source,examples/jest,examples/jest/ts,examples/jest/ts/src,examples/jest/ts/test,examples/kotlin,examples/nestjs,examples/nestjs/src,examples/parcel,examples/protobufjs,examples/protocol_buffers,examples/react_webpack,examples/user_managed_deps,examples/vendored_node,examples/vendored_node_and_yarn,examples/vue,examples/vue/src,examples/vue/src/components/HelloWorld,examples/web_testing,examples/webapp,examples/worker

# Mock versioning command to test the --stamp behavior
build --workspace_status_command="echo BUILD_SCM_VERSION 1.2.3"
Expand Down
14 changes: 14 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pkg_npm(
"//internal/pkg_web:package_contents",
"//internal/providers:package_contents",
"//internal/runfiles:package_contents",
"//nodejs:package_contents",
"//third_party/github.com/bazelbuild/bazel:package_contents",
"//third_party/github.com/bazelbuild/bazel-skylib:package_contents",
"//third_party/github.com/bazelbuild/bazel/tools/bash/runfiles:package_contents",
Expand Down Expand Up @@ -122,4 +123,17 @@ pkg_tar(
strip_prefix = "./rules_nodejs_package",
tags = ["manual"],
)

pkg_tar(
name = "release-core",
srcs = [
"LICENSE",
"README.md",
"//nodejs:package_contents",
# TODO(5.0) remove this and depend on real skylib
"//third_party/github.com/bazelbuild/bazel-skylib:package_contents",
],
extension = "tar.gz",
strip_prefix = ".",
)
# END-INTERNAL
8 changes: 7 additions & 1 deletion docs/Built-ins.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This is necessary to bootstrap Bazel to run the package manager to download othe

<pre>
node_repositories(<a href="#node_repositories-name">name</a>, <a href="#node_repositories-node_download_auth">node_download_auth</a>, <a href="#node_repositories-node_repositories">node_repositories</a>, <a href="#node_repositories-node_urls">node_urls</a>, <a href="#node_repositories-node_version">node_version</a>,
<a href="#node_repositories-package_json">package_json</a>, <a href="#node_repositories-preserve_symlinks">preserve_symlinks</a>, <a href="#node_repositories-repo_mapping">repo_mapping</a>, <a href="#node_repositories-use_nvmrc">use_nvmrc</a>, <a href="#node_repositories-vendored_node">vendored_node</a>,
<a href="#node_repositories-package_json">package_json</a>, <a href="#node_repositories-platform">platform</a>, <a href="#node_repositories-preserve_symlinks">preserve_symlinks</a>, <a href="#node_repositories-repo_mapping">repo_mapping</a>, <a href="#node_repositories-use_nvmrc">use_nvmrc</a>, <a href="#node_repositories-vendored_node">vendored_node</a>,
<a href="#node_repositories-vendored_yarn">vendored_yarn</a>, <a href="#node_repositories-yarn_download_auth">yarn_download_auth</a>, <a href="#node_repositories-yarn_repositories">yarn_repositories</a>, <a href="#node_repositories-yarn_urls">yarn_urls</a>, <a href="#node_repositories-yarn_version">yarn_version</a>)
</pre>

Expand Down Expand Up @@ -178,6 +178,12 @@ Defaults to `"14.17.5"`

Defaults to `[]`

<h4 id="node_repositories-platform">platform</h4>

(*String*): Internal use only. Which platform to install as a toolchain. If unset, we assume the repository is named nodejs_[platform]

Defaults to `""`

<h4 id="node_repositories-preserve_symlinks">preserve_symlinks</h4>

(*Boolean*): Turn on --node_options=--preserve-symlinks for nodejs_binary and nodejs_test rules.
Expand Down
5 changes: 5 additions & 0 deletions e2e/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ e2e_integration_test(
name = "e2e_node_loader_preserve_symlinks",
)

e2e_integration_test(
name = "e2e_core",
repositories = {"//:release-core": "rules_nodejs"},
)

e2e_integration_test(
name = "e2e_nodejs_image",
bazel_commands = [
Expand Down
43 changes: 43 additions & 0 deletions e2e/core/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")

write_file(
name = "js",
out = "some.js",
content = ["require('fs').writeFileSync(process.argv[2], 'stuff')"],
)

# Temporary fixture until we have toolchains hooked up in the core package
alias(
name = "node_bin",
actual = select({
"@bazel_tools//src/conditions:darwin_arm64": "@node16_darwin_arm64//:node_bin",
"@bazel_tools//src/conditions:darwin_x86_64": "@node16_darwin_amd64//:node_bin",
"@bazel_tools//src/conditions:linux_aarch64": "@node16_linux_arm64//:node_bin",
"@bazel_tools//src/conditions:linux_s390x": "@node16_linux_s390x//:node_bin",
"@bazel_tools//src/conditions:linux_x86_64": "@node16_linux_amd64//:node_bin",
"@bazel_tools//src/conditions:linux_ppc64le": "@node16_linux_ppc64le//:node_bin",
"@bazel_tools//src/conditions:windows": "@node16_windows_amd64//:node_bin",
"//conditions:default": "@node16_linux_amd64//:node_bin",
}),
)

genrule(
name = "try",
srcs = ["some.js"],
outs = ["thing"],
cmd = "$(execpath :node_bin) $(execpath some.js) $@",
tools = [":node_bin"],
)

write_file(
name = "write_expected",
out = "expected",
content = ["stuff"],
)

diff_test(
name = "test",
file1 = "expected",
file2 = "thing",
)
19 changes: 19 additions & 0 deletions e2e/core/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
workspace(name = "e2e_core")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "rules_nodejs",
sha256 = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/4.1.0/rules_nodejs-core-4.1.0.tar.gz"],
)

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains", "rules_nodejs_dependencies")

# This just gives us bazel-skylib
rules_nodejs_dependencies()

nodejs_register_toolchains(
name = "node16",
node_version = "16.9.0",
)
2 changes: 1 addition & 1 deletion index.for_docs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ load("//internal/common:params_file.bzl", _params_file = "params_file")
load("//internal/generated_file_test:generated_file_test.bzl", _generated_file_test = "generated_file_test")
load("//internal/js_library:js_library.bzl", _js_library = "js_library")
load("//internal/node:node.bzl", _nodejs_binary = "nodejs_binary", _nodejs_test = "nodejs_test")
load("//internal/node:node_repositories.bzl", _node_repositories = "node_repositories_rule")
load("//nodejs:repositories.bzl", _node_repositories = "node_repositories")
load("//internal/node:npm_package_bin.bzl", _npm_bin = "npm_package_bin")
load("//internal/npm_install:npm_install.bzl", _npm_install = "npm_install", _yarn_install = "yarn_install")
load("//internal/pkg_npm:pkg_npm.bzl", _pkg_npm = "pkg_npm")
Expand Down
3 changes: 1 addition & 2 deletions internal/bazel_integration_test/bazel_integration_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,7 @@ def rules_nodejs_integration_test(name, **kwargs):
]

# replace the following repositories with the generated archives
repositories = kwargs.pop("repositories", {})
repositories["//:release"] = "build_bazel_rules_nodejs"
repositories = kwargs.pop("repositories", {"//:release": "build_bazel_rules_nodejs"})

# convert the npm packages into the tar output
npm_packages = kwargs.pop("npm_packages", {})
Expand Down
2 changes: 1 addition & 1 deletion internal/copy_repository/copy_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""Custom copy_repository rule used by npm_install and yarn_install.
"""

load("@build_bazel_rules_nodejs//internal/common:os_name.bzl", "is_windows_os")
load("@build_bazel_rules_nodejs//nodejs/private:os_name.bzl", "is_windows_os")

def _copy_file(rctx, src):
rctx.template(src.basename, src)
Expand Down
1 change: 1 addition & 0 deletions internal/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bzl_library(
visibility = ["//visibility:public"],
deps = [
"//internal/npm_install:bzl",
"//nodejs:bzl",
"//third_party/github.com/bazelbuild/bazel-skylib:bzl",
"//toolchains/node:bzl",
],
Expand Down
2 changes: 1 addition & 1 deletion internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ load("//internal/common:path_utils.bzl", "strip_external")
load("//internal/common:preserve_legacy_templated_args.bzl", "preserve_legacy_templated_args")
load("//internal/common:windows_utils.bzl", "create_windows_native_launcher_script", "is_windows")
load("//internal/linker:link_node_modules.bzl", "MODULE_MAPPINGS_ASPECT_RESULTS_NAME", "module_mappings_aspect", "write_node_modules_manifest")
load("//internal/node:node_repositories.bzl", "BUILT_IN_NODE_PLATFORMS")
load("//nodejs:repositories.bzl", "BUILT_IN_NODE_PLATFORMS")

def _trim_package_node_modules(package_name):
# trim a package name down to its path prior to a node_modules
Expand Down
2 changes: 1 addition & 1 deletion internal/node/node_labels.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Labels are different on windows and linux/OSX.
"""

load("//internal/common:os_name.bzl", "is_windows_os", "os_name")
load("//nodejs/private:os_name.bzl", "is_windows_os", "os_name")

def get_node_label(rctx):
if is_windows_os(rctx):
Expand Down
Loading

0 comments on commit a32cf5c

Please sign in to comment.