Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added directive js_collect_targets #61

Merged
merged 5 commits into from
Oct 17, 2024
Merged
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ The following directives are recognized by this plugin:
<td colspan="2"><p dir="auto">Generates a <code>web_assets</code> rule in the configured <code>js_root</code> that refers to all of the <code>web_assets</code> rules in child packages</p></td>
</tr>

<tr>
<td><code># gazelle:js_collect_targets</code></td>
<td><code>""</code></td>
</tr>
<tr>
<td colspan="2"><p dir="auto">Similar to <code>js_collect_all_assets</code> this directive collects all sibling and child package targets in a target with the given name. See <code>tests/collect_targets</code> for usage.</p></td>
</tr>

<tr>
<td><code># gazelle:js_web_asset .json,.css,.scss</code></td>
<td><code>none</code></td>
Expand Down
1 change: 1 addition & 0 deletions examples/nextjs/.bazelignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules
apps/alpha/node_modules
apps/alpha/pages/__snapshots__
packages/one/node_modules
app/.next
bazel-out
Expand Down
12 changes: 11 additions & 1 deletion examples/nextjs/apps/alpha/pages/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
load("@aspect_rules_js//js:defs.bzl", "js_library")
load("//bazel:jest_test.bzl", "jest_test")
load("//bazel:ts_project.bzl", "ts_project")

# gazelle:js_collect_all
# gazelle:js_collect_targets next_pages

ts_project(
name = "pages",
srcs = [
"_app.tsx",
"api/hello.ts",
"index.tsx",
],
data = [
Expand Down Expand Up @@ -54,3 +55,12 @@ jest_test(
"//:node_modules/react",
],
)

js_library(
name = "next_pages",
visibility = ["//apps/alpha:__subpackages__"],
deps = [
":pages",
"//apps/alpha/pages/api",
],
)
11 changes: 11 additions & 0 deletions examples/nextjs/apps/alpha/pages/api/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("//bazel:ts_project.bzl", "ts_project")

# gazelle:js_collect_all

ts_project(
name = "api",
srcs = ["hello.ts"],
data = ["//:node_modules/next"],
visibility = ["//apps/alpha:__subpackages__"],
deps = ["//:node_modules/next"],
)
41 changes: 29 additions & 12 deletions gazelle/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ type JsConfig struct {
CollectWebAssets bool
CollectAllAssets bool
CollectedAssets map[string]bool
CollectTargets string
CollectedTargets map[string]bool
CollectAll bool
CollectAllRoot string
CollectAllSources map[string]bool
Expand Down Expand Up @@ -101,6 +103,8 @@ func NewJsConfig() *JsConfig {
CollectWebAssets: false,
CollectAllAssets: false,
CollectedAssets: make(map[string]bool),
CollectTargets: "",
CollectedTargets: make(map[string]bool),
CollectAll: false,
CollectAllRoot: "",
CollectAllSources: make(map[string]bool),
Expand Down Expand Up @@ -159,6 +163,9 @@ func (parent *JsConfig) NewChild() *JsConfig {
child.CollectAllAssets = parent.CollectAllAssets
child.CollectedAssets = parent.CollectedAssets // Reinitialized on change to JSRoot

child.CollectTargets = ""
child.CollectedTargets = parent.CollectedTargets

child.CollectAll = parent.CollectAll
child.CollectAllRoot = parent.CollectAllRoot
child.CollectAllSources = parent.CollectAllSources // Copy reference, reinitialized on change to CollectAll
Expand Down Expand Up @@ -235,6 +242,7 @@ func (*JS) KnownDirectives() []string {
"js_collect_all_assets",
"js_aggregate_all_assets",
"js_collect_all",
"js_collect_targets",
"js_jest_test_per_shard",
"js_jest_size",
"js_jest_config",
Expand Down Expand Up @@ -286,7 +294,7 @@ func (*JS) Configure(c *config.Config, rel string, f *rule.File) {
case "disabled":
jsConfig.Enabled = false
default:
log.Fatalf(Err("failed to read directive %s: %s, only \"enabled\", and \"disabled\" are valid", directive.Key, directive.Value))
log.Fatal(Err("failed to read directive %s: %s, only \"enabled\", and \"disabled\" are valid", directive.Key, directive.Value))
}

case "js_lookup_types":
Expand All @@ -298,7 +306,7 @@ func (*JS) Configure(c *config.Config, rel string, f *rule.File) {
case "js_package_file":
values := strings.Split(directive.Value, " ")
if len(values) != 2 {
log.Fatalf(Err("failed to read directive %s: %s, expected 2 values", directive.Key, directive.Value))
log.Fatal(Err("failed to read directive %s: %s, expected 2 values", directive.Key, directive.Value))
}
jsConfig.PackageFile = values[0]
npmLabel := values[1]
Expand All @@ -311,7 +319,7 @@ func (*JS) Configure(c *config.Config, rel string, f *rule.File) {

data, err := os.ReadFile(path.Join(c.RepoRoot, f.Pkg, jsConfig.PackageFile))
if err != nil {
log.Fatalf(Err("failed to open %s: %v", directive.Value, err))
log.Fatal(Err("failed to open %s: %v", directive.Value, err))
}

// Read dependencies from file
Expand All @@ -323,14 +331,14 @@ func (*JS) Configure(c *config.Config, rel string, f *rule.File) {
DevDependencies: make(map[string]string),
}
if err := json.Unmarshal(data, &newDeps); err != nil {
log.Fatalf(Err("failed to parse %s: %v", directive.Value, err))
log.Fatal(Err("failed to parse %s: %v", directive.Value, err))
}

// Store npmLabel in dependencies
for k, _ := range newDeps.Dependencies {
for k := range newDeps.Dependencies {
jsConfig.NpmDependencies.Dependencies[k] = npmLabel
}
for k, _ := range newDeps.DevDependencies {
for k := range newDeps.DevDependencies {
jsConfig.NpmDependencies.DevDependencies[k] = npmLabel
}

Expand All @@ -346,7 +354,7 @@ func (*JS) Configure(c *config.Config, rel string, f *rule.File) {

var err error
if jsConfig.ImportAliasPattern, err = regexp.Compile(strings.Join(keyPatterns, "|")); err != nil {
log.Fatalf(Err("failed to parse %s: %v", directive.Value, err))
log.Fatal(Err("failed to parse %s: %v", directive.Value, err))
}

case "js_visibility":
Expand All @@ -360,12 +368,21 @@ func (*JS) Configure(c *config.Config, rel string, f *rule.File) {
case "js_root":
jSRoot, err := filepath.Rel(".", f.Pkg)
if err != nil {
log.Fatalf(Err("failed to read directive %s: %v", directive.Key, err))
log.Fatal(Err("failed to read directive %s: %v", directive.Key, err))
} else {
jsConfig.JSRoot = jSRoot
jsConfig.CollectedAssets = make(map[string]bool)
}

case "js_collect_targets":
if directive.Value == "" {
jsConfig.CollectTargets = ""
jsConfig.CollectedTargets = nil
} else {
jsConfig.CollectTargets = directive.Value
jsConfig.CollectedTargets = make(map[string]bool)
}

case "js_collect_barrels":
jsConfig.CollectBarrels = readBoolDirective(directive)

Expand All @@ -387,7 +404,7 @@ func (*JS) Configure(c *config.Config, rel string, f *rule.File) {
case "js_collect_all":
collectRoot, err := filepath.Rel(".", f.Pkg)
if err != nil {
log.Fatalf(Err("failed to read directive %s: %v", directive.Key, err))
log.Fatal(Err("failed to read directive %s: %v", directive.Key, err))
} else {
jsConfig.CollectAllRoot = collectRoot
jsConfig.CollectAll = true
Expand All @@ -410,7 +427,7 @@ func (*JS) Configure(c *config.Config, rel string, f *rule.File) {
if len(vals) > 1 {
val, err := strconv.ParseBool(directive.Value)
if err != nil {
log.Fatalf(Err("failed to read directive %s: %v", directive.Key, err))
log.Fatal(Err("failed to read directive %s: %v", directive.Key, err))
}
status = val
}
Expand Down Expand Up @@ -516,7 +533,7 @@ func readBoolDirective(directive rule.Directive) bool {
} else {
val, err := strconv.ParseBool(directive.Value)
if err != nil {
log.Fatalf(Err("failed to read directive %s: %v", directive.Key, err))
log.Fatal(Err("failed to read directive %s: %v", directive.Key, err))
}
return val
}
Expand All @@ -528,7 +545,7 @@ func readIntDirective(directive rule.Directive) int {
} else {
val, err := strconv.ParseInt(directive.Value, 10, 32)
if err != nil {
log.Fatalf(Err("failed to read directive %s: %v", directive.Key, err))
log.Fatal(Err("failed to read directive %s: %v", directive.Key, err))
}
return int(val)
}
Expand Down
Loading
Loading