Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# @generated
# Input hashes for repository rule npm_translate_lock(name = "npm", pnpm_lock = "@@//:pnpm-lock.yaml").
# This file should be checked into version control along with the pnpm-lock.yaml file.
.npmrc=-1810889885
consumer/package.json=-161392235
lib/package.json=-601767299
package.json=-638192911
pnpm-lock.yaml=-880726515
pnpm-workspace.yaml=-1339876532
3 changes: 3 additions & 0 deletions e2e/publish_package_json/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
consumer/node_modules
lib/node_modules
2 changes: 2 additions & 0 deletions e2e/publish_package_json/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import %workspace%/../../tools/preset.bazelrc
import %workspace%/../e2e.bazelrc
1 change: 1 addition & 0 deletions e2e/publish_package_json/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry=https://registry.npmjs.org/
3 changes: 3 additions & 0 deletions e2e/publish_package_json/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages()
34 changes: 34 additions & 0 deletions e2e/publish_package_json/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
bazel_dep(name = "aspect_rules_js", version = "0.0.0", dev_dependency = True)
local_path_override(
module_name = "aspect_rules_js",
path = "../..",
)

bazel_dep(name = "jq.bzl", version = "0.4.0", dev_dependency = True)

pnpm = use_extension("@aspect_rules_js//npm:extensions.bzl", "pnpm")
pnpm.pnpm(
name = "pnpm",
pnpm_version = "9.15.9",
)
use_repo(pnpm, "pnpm", "pnpm__links")

npm = use_extension(
"@aspect_rules_js//npm:extensions.bzl",
"npm",
dev_dependency = True,
)
npm.npm_translate_lock(
name = "npm",
data = [
"//:package.json",
"//:pnpm-workspace.yaml",
"//lib:package.json",
"//consumer:package.json",
],
npmrc = "//:.npmrc",
pnpm_lock = "//:pnpm-lock.yaml",
update_pnpm_lock = True,
verify_node_modules_ignored = "//:.bazelignore",
)
use_repo(npm, "npm")
12 changes: 12 additions & 0 deletions e2e/publish_package_json/consumer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@aspect_rules_js//js:defs.bzl", "js_test")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages()

js_test(
name = "test",
data = [
":node_modules/@lib/test",
],
entry_point = "test.js",
)
6 changes: 6 additions & 0 deletions e2e/publish_package_json/consumer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "consumer",
"dependencies": {
"@lib/test": "workspace:*"
}
}
10 changes: 10 additions & 0 deletions e2e/publish_package_json/consumer/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const lib = require('@lib/test')

const result = lib.id()
console.log('loaded:', result)

if (!result.includes('dist')) {
throw new Error(
'Expected to load from dist (via publishConfig.main) but got: ' + result
)
}
24 changes: 24 additions & 0 deletions e2e/publish_package_json/lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@aspect_rules_js//js:defs.bzl", "js_library")
load("@aspect_rules_js//npm:defs.bzl", "npm_package", "publish_package_json")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages()

publish_package_json(
name = "package",
)

js_library(
name = "lib",
srcs = [
"dist/index.js",
":package",
],
visibility = ["//visibility:public"],
)

npm_package(
name = "pkg",
srcs = [":lib"],
visibility = ["//visibility:public"],
)
1 change: 1 addition & 0 deletions e2e/publish_package_json/lib/dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { id: () => '@lib/test (dist)' }
8 changes: 8 additions & 0 deletions e2e/publish_package_json/lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@lib/test",
"private": true,
"main": "./src/foo.js",
"publishConfig": {
"main": "./dist/index.js"
}
}
1 change: 1 addition & 0 deletions e2e/publish_package_json/lib/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('should not load source entry point')
6 changes: 6 additions & 0 deletions e2e/publish_package_json/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"pnpm": {
"onlyBuiltDependencies": []
}
}
17 changes: 17 additions & 0 deletions e2e/publish_package_json/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions e2e/publish_package_json/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packages:
- lib
- consumer
31 changes: 31 additions & 0 deletions examples/publish_package_json/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
load("@aspect_rules_js//npm:defs.bzl", "publish_package_json")
load("@jq.bzl//jq:jq.bzl", "jq", "jq_test")

publish_package_json(
name = "package",
)

jq(
name = "expected",
srcs = [],
filter = """
{
name: "@example/publish-config",
version: "1.0.0",
main: "./dist/index.js",
types: "./dist/index.d.ts",
exports: {
".": {
types: "./dist/index.d.ts",
import: "./dist/index.js"
}
}
}
""",
)

jq_test(
name = "publish_config_test",
file1 = ":package",
file2 = ":expected",
)
22 changes: 22 additions & 0 deletions examples/publish_package_json/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@example/publish-config",
"version": "1.0.0",
"main": "./src/index.ts",
"types": "./src/index.ts",
"exports": {
".": {
"types": "./src/index.ts",
"import": "./src/index.ts"
}
},
"publishConfig": {
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
}
}
}
2 changes: 2 additions & 0 deletions npm/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ load(
load(
"//npm/private:npm_package.bzl",
_npm_package = "npm_package",
_publish_package_json = "publish_package_json",
_stamped_package_json = "stamped_package_json",
)

npm_package = _npm_package
npm_link_package = _npm_link_package
publish_package_json = _publish_package_json
stamped_package_json = _stamped_package_json
69 changes: 69 additions & 0 deletions npm/private/npm_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,75 @@ def npm_package(
**kwargs
)

_PUBLISH_CONFIG_FIELDS = [
"bin",
"browser",
"cpu",
"engines",
"es2015",
"esnext",
"exports",
"imports",
"libc",
"main",
"module",
"os",
"type",
"types",
"typesVersions",
"typings",
"umd:main",
"unpkg",
]

def publish_package_json(name, stamp_var = None, publish_config_fields = None, **kwargs):
"""Convenience wrapper to apply publishConfig field promotion to package.json.

Mimics `pnpm publish` behavior: fields within the `publishConfig` object are promoted to the
top level of the package.json, then `publishConfig` is removed. This is useful when your source
package.json uses `publishConfig` to override fields like `exports`, `main`, `types`, etc. for
the published artifact.

Optionally stamps the `version` field using a Bazel stamp variable, combining the behavior of
`stamped_package_json`.

For more information on pnpm's publishConfig behavior, see https://pnpm.io/package_json#publishconfig

Args:
name: name of the resulting `jq` target, must be "package"
stamp_var: optional key from the bazel-out/stable-status.txt or bazel-out/volatile-status.txt files.
When set, the version field is stamped just like `stamped_package_json`.
publish_config_fields: optional list of field names to promote from `publishConfig`. Defaults to the
set of fields that pnpm promotes: bin, browser, cpu, exports, imports, libc, main, module, os,
type, types, typings.
**kwargs: additional attributes passed to the jq rule
"""
if name != "package":
fail("""publish_package_json should always be named "package" so that the default output is named "package.json".
This is required since Bazel doesn't allow a predeclared output to have the same name as an input file.""")

fields = publish_config_fields if publish_config_fields != None else _PUBLISH_CONFIG_FIELDS

promote_parts = [
"if .publishConfig[\"{field}\"] then .[\"{field}\"] = .publishConfig[\"{field}\"] else . end".format(field = f)
for f in fields
]

filters = promote_parts + ["del(.publishConfig)"]

if stamp_var:
filters = [
"$ARGS.named.STAMP as $stamp",
".version = ($stamp[0].{} // \"0.0.0\")".format(stamp_var),
] + filters

jq(
name = name,
srcs = ["package.json"],
filter = " | ".join(filters),
**kwargs
)

def stamped_package_json(name, stamp_var, **kwargs):
"""Convenience wrapper to set the "version" property in package.json with the git tag.

Expand Down