Skip to content

Commit

Permalink
fix(cli): support js deps via package.json types (#7589)
Browse files Browse the repository at this point in the history
The package.json `types|typings` field should be considered a dependency
just like the `main` field.

---

### Changes are visible to end-users: yes

- Searched for relevant documentation and updated as needed: yes
- Breaking change (forces users to change their own code or config): no
- Suggested release notes appear below: yes

Support `aspect configure` detecting dependencies from `package.json`
`types|typings` fields.

### Test plan

- Covered by existing test cases
- New test cases added

GitOrigin-RevId: f0198476cbbe8f755510e4b0992b020fdcf3059c
  • Loading branch information
jbedard committed Dec 13, 2024
1 parent 599216a commit dbdc5bb
Show file tree
Hide file tree
Showing 35 changed files with 252 additions and 0 deletions.
10 changes: 10 additions & 0 deletions gazelle/js/node/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type npmPackageJSON struct {

// exports: https://nodejs.org/docs/latest-v22.x/api/packages.html#exports
Exports interface{} `json:"exports"`

// types/typings: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html#including-declarations-in-your-npm-package
Types string `json:"types"`
Typings string `json:"typings"`
}

// Extract the various import types from the package.json file such as
Expand All @@ -36,6 +40,12 @@ func ParsePackageJsonImportsFile(rootDir, packageJsonPath string) ([]string, err
if c.Main != "" {
imports = append(imports, path.Clean(c.Main))
}
if c.Types != "" {
imports = append(imports, path.Clean(c.Types))
}
if c.Typings != "" {
imports = append(imports, path.Clean(c.Typings))
}

if c.Exports != nil {
switch exports := c.Exports.(type) {
Expand Down
8 changes: 8 additions & 0 deletions gazelle/js/tests/npm_package_deps/pnpm-lock.yaml

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

6 changes: 6 additions & 0 deletions gazelle/js/tests/npm_package_deps/types-js-gen/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
genrule(
name = "foo",
srcs = [],
outs = ["types.d.ts"],
cmd = "echo \"export const foo: number\" > \"$@\"",
)
20 changes: 20 additions & 0 deletions gazelle/js/tests/npm_package_deps/types-js-gen/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@npm//:defs.bzl", "npm_link_all_packages")

genrule(
name = "foo",
srcs = [],
outs = ["types.d.ts"],
cmd = "echo \"export const foo: number\" > \"$@\"",
)

npm_link_all_packages(name = "node_modules")

npm_package(
name = "pkg",
srcs = [
"package.json",
":types.d.ts",
],
visibility = ["//visibility:public"],
)
5 changes: 5 additions & 0 deletions gazelle/js/tests/npm_package_deps/types-js-gen/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "types-js-gen",
"private": true,
"types": "types.d.ts"
}
Empty file.
22 changes: 22 additions & 0 deletions gazelle/js/tests/npm_package_deps/types-js/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("@aspect_rules_js//js:defs.bzl", "js_library")
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")

js_library(
name = "tsc",
srcs = [
"start.js",
"types.d.ts",
],
)

npm_package(
name = "pkg",
srcs = [
"package.json",
":tsc",
],
visibility = ["//visibility:public"],
)
6 changes: 6 additions & 0 deletions gazelle/js/tests/npm_package_deps/types-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "types-js",
"private": true,
"main": "start.js",
"types": "types.d.ts"
}
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions gazelle/js/tests/npm_package_deps/typings-js-gen/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
genrule(
name = "foo",
srcs = [],
outs = ["types.d.ts"],
cmd = "echo \"export const foo: number\" > \"$@\"",
)
20 changes: 20 additions & 0 deletions gazelle/js/tests/npm_package_deps/typings-js-gen/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@npm//:defs.bzl", "npm_link_all_packages")

genrule(
name = "foo",
srcs = [],
outs = ["types.d.ts"],
cmd = "echo \"export const foo: number\" > \"$@\"",
)

npm_link_all_packages(name = "node_modules")

npm_package(
name = "pkg",
srcs = [
"package.json",
":types.d.ts",
],
visibility = ["//visibility:public"],
)
5 changes: 5 additions & 0 deletions gazelle/js/tests/npm_package_deps/typings-js-gen/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "typings-js-gen",
"private": true,
"typings": "types.d.ts"
}
Empty file.
22 changes: 22 additions & 0 deletions gazelle/js/tests/npm_package_deps/typings-js/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("@aspect_rules_js//js:defs.bzl", "js_library")
load("@aspect_rules_js//npm:defs.bzl", "npm_package")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")

js_library(
name = "tsc",
srcs = [
"start.js",
"types.d.ts",
],
)

npm_package(
name = "pkg",
srcs = [
"package.json",
":tsc",
],
visibility = ["//visibility:public"],
)
6 changes: 6 additions & 0 deletions gazelle/js/tests/npm_package_deps/typings-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "typings-js",
"private": true,
"main": "start.js",
"typings": "types.d.ts"
}
Empty file.
Empty file.
8 changes: 8 additions & 0 deletions gazelle/js/tests/npm_package_deps_lib/pnpm-lock.yaml

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

6 changes: 6 additions & 0 deletions gazelle/js/tests/npm_package_deps_lib/types-js-gen/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
genrule(
name = "foo",
srcs = [],
outs = ["types.d.ts"],
cmd = "echo \"export const foo: number\" > \"$@\"",
)
18 changes: 18 additions & 0 deletions gazelle/js/tests/npm_package_deps_lib/types-js-gen/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
load("@aspect_rules_js//js:defs.bzl", "js_library")
load("@npm//:defs.bzl", "npm_link_all_packages")

genrule(
name = "foo",
srcs = [],
outs = ["types.d.ts"],
cmd = "echo \"export const foo: number\" > \"$@\"",
)

npm_link_all_packages(name = "node_modules")

js_library(
name = "pkg",
srcs = ["package.json"],
visibility = ["//visibility:public"],
deps = [":types.d.ts"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "types-js-gen",
"private": true,
"types": "types.d.ts"
}
Empty file.
19 changes: 19 additions & 0 deletions gazelle/js/tests/npm_package_deps_lib/types-js/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@aspect_rules_js//js:defs.bzl", "js_library")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")

js_library(
name = "tsc",
srcs = [
"start.js",
"types.d.ts",
],
)

js_library(
name = "pkg",
srcs = ["package.json"],
visibility = ["//visibility:public"],
deps = [":tsc"],
)
6 changes: 6 additions & 0 deletions gazelle/js/tests/npm_package_deps_lib/types-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "types-js",
"private": true,
"main": "start.js",
"types": "types.d.ts"
}
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions gazelle/js/tests/npm_package_deps_lib/typings-js-gen/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
genrule(
name = "foo",
srcs = [],
outs = ["types.d.ts"],
cmd = "echo \"export const foo: number\" > \"$@\"",
)
18 changes: 18 additions & 0 deletions gazelle/js/tests/npm_package_deps_lib/typings-js-gen/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
load("@aspect_rules_js//js:defs.bzl", "js_library")
load("@npm//:defs.bzl", "npm_link_all_packages")

genrule(
name = "foo",
srcs = [],
outs = ["types.d.ts"],
cmd = "echo \"export const foo: number\" > \"$@\"",
)

npm_link_all_packages(name = "node_modules")

js_library(
name = "pkg",
srcs = ["package.json"],
visibility = ["//visibility:public"],
deps = [":types.d.ts"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "typings-js-gen",
"private": true,
"typings": "types.d.ts"
}
Empty file.
19 changes: 19 additions & 0 deletions gazelle/js/tests/npm_package_deps_lib/typings-js/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@aspect_rules_js//js:defs.bzl", "js_library")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")

js_library(
name = "tsc",
srcs = [
"start.js",
"types.d.ts",
],
)

js_library(
name = "pkg",
srcs = ["package.json"],
visibility = ["//visibility:public"],
deps = [":tsc"],
)
6 changes: 6 additions & 0 deletions gazelle/js/tests/npm_package_deps_lib/typings-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "typings-js",
"private": true,
"main": "start.js",
"typings": "types.d.ts"
}
Empty file.
Empty file.

0 comments on commit dbdc5bb

Please sign in to comment.