Skip to content

Commit

Permalink
Merge pull request #6 from player-ui/npm-publish
Browse files Browse the repository at this point in the history
Add better support for publishing npm packages
  • Loading branch information
adierkens authored Jul 15, 2022
2 parents 3ae2eca + 3975e48 commit 229dfc4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"name": "@test/module-a",
"version": "0.0.0-PLACEHOLDER",
"private": false,
"publishConfig": {
"registry": "https://registry.npmjs.org"
},
"peerDependencies": {
"typescript": "^4.5.2"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"name": "@test/module-b",
"version": "0.0.0-PLACEHOLDER",
"private": false,
"publishConfig": {
"registry": "https://registry.npmjs.org"
},
"peerDependencies": {},
"dependencies": {
"@test/module-a": "0.0.0-PLACEHOLDER"
Expand Down
24 changes: 18 additions & 6 deletions javascript/js_library_pipeline.bzl
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
load("//javascript/package_json:package_json.bzl", "create_package_json")
load("//javascript/rollup:rollup_build.bzl", "rollup_bin_build", "rollup_build")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm")
load("@npm//jest-cli:index.bzl", "jest_test")
load("@npm//eslint:index.bzl", "eslint_test")
load(":npm.bzl", "publish_npm")
load(":utils.bzl", "filter_empty", "without_tests", "remove_duplicates", "include_exts")

BUILD_DATA = [
Expand Down Expand Up @@ -34,9 +33,11 @@ def js_library_pipeline(
js_library_data = [],
test_env = {},
placeholder_version = PLACEHOLDER_VERSION,
registry = "https://registry.npmjs.org",
version_file = "//:VERSION",
root_package_json = "//:package.json",
typings = [],
private = False,
test_file_pattern = [
"_tests_",
".test.",
Expand All @@ -52,9 +53,11 @@ def js_library_pipeline(
name = create_package_json_name,
package_name = name,
entry = entry,
private = private,
bin_entry = bin_entry,
bin_name = bin_name,
out_dir = out_dir,
registry = registry,
placeholder_version = placeholder_version,
dependencies = dependencies,
peer_dependencies = peer_dependencies,
Expand Down Expand Up @@ -125,8 +128,17 @@ def js_library_pipeline(
data = remove_duplicates(filter_empty([jest_config] + data + test_data + dependencies + peer_dependencies + srcs)),
)

publish_npm(
name = "%s-publish",
version = version_file,
bundle = [":%s" % name],
pkg_npm(
name = "pkg_npm",
package_name = name,
deps = [":%s" % name],
tags = filter_empty([
"do-not-publish" if private else None
]),
substitutions = {
"__VERSION__": "{STABLE_VERSION}",
"0.0.0-PLACEHOLDER": "{STABLE_VERSION}",
"__GIT_COMMIT__": "{STABLE_GIT_COMMIT}",
},
validate = False,
)
15 changes: 0 additions & 15 deletions javascript/npm.bzl

This file was deleted.

15 changes: 14 additions & 1 deletion javascript/package_json/create_package_json.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ const main = ([config]) => {
root_package_json,
base_package_json,
additional_properties,
out_dir
out_dir,
registry,
private
} = JSON.parse(config);

const rootPackageJson = JSON.parse(fs.readFileSync(root_package_json, 'utf-8'));
Expand Down Expand Up @@ -48,8 +50,19 @@ const main = ([config]) => {
}
}

let publishConfig = undefined;

if (registry) {
publishConfig = {
"registry": registry,
}
}

fs.writeFileSync(output_file, JSON.stringify({
name,
version: placeholder_version,
private,
publishConfig,
peerDependencies: createDependencyObject(peer_dependencies),
dependencies: createDependencyObject(dependencies),
main: path.join(out_dir, 'index.cjs.js'),
Expand Down
8 changes: 8 additions & 0 deletions javascript/package_json/package_json.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ PACKGE_JSON_ATTRS = {
# The name for a bin entry
"bin_name": attr.string(),

# If the package should skipped being published to npm
"private": attr.bool(default = False),

# The npm registry to publish to
"registry": attr.string(),

# A .json file to use to add additional properties to the generated package.
# This can often be a 'package.json' and the entries/outputs/dependencies will be filled in later on
"base_package_json": attr.label(allow_single_file = ["*.json"]),
Expand Down Expand Up @@ -96,6 +102,8 @@ def _create_package_json_impl(ctx):
"peer_dependencies": [_get_pkg_name(dep) for dep in ctx.attr.peer_dependencies],
"dependencies": [_get_pkg_name(dep) for dep in ctx.attr.dependencies],
"local_deps": local_deps,
"private": ctx.attr.private,
"registry": ctx.attr.registry,
})],
executable = "_create_pkg_json",
)
Expand Down

0 comments on commit 229dfc4

Please sign in to comment.