diff --git a/gazelle/configure.go b/gazelle/configure.go index 1d1b03e..6b1f451 100644 --- a/gazelle/configure.go +++ b/gazelle/configure.go @@ -76,6 +76,7 @@ type JsConfig struct { Fix bool JSRoot string NextPagesRoot string + NextPagesRootName string WebAssetSuffixes map[string]bool Quiet bool Verbose bool @@ -116,6 +117,7 @@ func NewJsConfig() *JsConfig { Fix: false, JSRoot: "/", NextPagesRoot: "", + NextPagesRootName: "pages", WebAssetSuffixes: make(map[string]bool), Quiet: false, Verbose: false, @@ -170,6 +172,7 @@ func (parent *JsConfig) NewChild() *JsConfig { child.CollectedAssets = parent.CollectedAssets // Reinitialized on change to JSRoot child.NextPagesRoot = parent.NextPagesRoot + child.NextPagesRootName = parent.NextPagesRootName if parent.CollectPages { // If the parent is collecting pages, the child should collect page files child.CollectPages = false child.CollectPageFiles = true @@ -246,6 +249,7 @@ func (*JS) KnownDirectives() []string { "js_extension", "js_root", "js_nextjs_pages_root", + "js_nextjs_pages_root_name", "js_lookup_types", "js_fix", "js_package_file", @@ -401,6 +405,13 @@ func (*JS) Configure(c *config.Config, rel string, f *rule.File) { jsConfig.CollectPageFiles = false } + case "js_nextjs_pages_root_name": + if directive.Value == "" { + jsConfig.NextPagesRootName = "pages" + } else { + jsConfig.NextPagesRootName = directive.Value + } + case "js_collect_barrels": jsConfig.CollectBarrels = readBoolDirective(directive) diff --git a/gazelle/generate.go b/gazelle/generate.go index 8111ab0..1db0fce 100644 --- a/gazelle/generate.go +++ b/gazelle/generate.go @@ -246,11 +246,9 @@ func (lang *JS) GenerateRules(args language.GenerateArgs) language.GenerateResul generatedRules = append(generatedRules, generatedAWARules...) generatedImports = append(generatedImports, generatedAWAImports...) - generatedTSRules, generatedTSImports := lang.genEmptyPageRule( + generatedTSRules, generatedTSImports := lang.genPageRule( args, jsConfig, - pkgName, - append(sources.tsSources, sources.jsSources...), ) generatedRules = append(generatedRules, generatedTSRules...) generatedImports = append(generatedImports, generatedTSImports...) @@ -521,7 +519,7 @@ func (lang *JS) genRules(args language.GenerateArgs, jsConfig *JsConfig, isBarre if appendTSExt { name = name + "_ts" } - if jsConfig.CollectAll || jsConfig.CollectPages || jsConfig.CollectPageFiles { + if jsConfig.CollectAll || jsConfig.CollectPageFiles { // add as a folder for _, existingRule := range lang.readExistingRules(args, false) { // Look for existing rules with the same name, but different kind @@ -843,14 +841,14 @@ func (lang *JS) genAllAssets(args language.GenerateArgs, isJSRoot bool, jsConfig return generatedRules, generatedImports } -func (lang *JS) genEmptyPageRule(args language.GenerateArgs, jsConfig *JsConfig, pkgName string, sources []string) ([]*rule.Rule, []interface{}) { +func (lang *JS) genPageRule(args language.GenerateArgs, jsConfig *JsConfig) ([]*rule.Rule, []interface{}) { generatedRules := make([]*rule.Rule, 0) generatedImports := make([]interface{}, 0) - if len(sources) == 0 && jsConfig.CollectPages { + if jsConfig.CollectPages { // Add an empty `js_library` rule. This will be given `deps` later in resolve.go - r := rule.NewRule(getKind(args.Config, "js_library"), pkgName) + r := rule.NewRule(getKind(args.Config, "js_library"), jsConfig.NextPagesRootName) generatedRules = append(generatedRules, r) generatedImports = append(generatedImports, &noImports) diff --git a/gazelle/resolve.go b/gazelle/resolve.go index c1d762b..7ab6b1a 100644 --- a/gazelle/resolve.go +++ b/gazelle/resolve.go @@ -120,7 +120,7 @@ func (lang *JS) Imports(c *config.Config, r *rule.Rule, f *rule.File) []resolve. } // Any subfolders could be used to depend on this rule - folderImports := jsConfig.CollectAll && (r.Kind() == getKind(c, "ts_project") || r.Kind() == getKind(c, "js_library")) + folderImports := (jsConfig.CollectAll || jsConfig.CollectPageFiles) && (r.Kind() == getKind(c, "ts_project") || r.Kind() == getKind(c, "js_library")) if folderImports { base := filepath.Dir(f.Path) subDirectories := make(map[string]bool) @@ -283,7 +283,7 @@ func (lang *JS) Resolve(c *config.Config, ix *resolve.RuleIndex, rc *repo.Remote } // Add in page dependencies if they exist - if jsConfig.CollectPages && len(jsConfig.CollectedPages) > 0 { + if jsConfig.CollectPages && len(jsConfig.CollectedPages) > 0 && r.Name() == jsConfig.NextPagesRootName { for fqName := range jsConfig.CollectedPages { depSet[fqName] = true } diff --git a/tests/collect_pages/src/pages/BUILD.out b/tests/collect_pages/src/pages/BUILD.out index 380892b..ab3b873 100644 --- a/tests/collect_pages/src/pages/BUILD.out +++ b/tests/collect_pages/src/pages/BUILD.out @@ -1,12 +1,19 @@ +load("@aspect_rules_js//js:defs.bzl", "js_library") load("@aspect_rules_ts//ts:defs.bzl", "ts_project") # gazelle:js_nextjs_pages_root ts_project( + name = "404", + srcs = ["404.ts"], +) + +ts_project( + name = "index", + srcs = ["index.ts"], +) + +js_library( name = "pages", - srcs = [ - "404.ts", - "index.ts", - ], deps = ["//src/pages/my_page"], ) diff --git a/tests/collect_pages/src/pages2/BUILD.in b/tests/collect_pages/src/pages2/BUILD.in index e79a20f..76c3022 100644 --- a/tests/collect_pages/src/pages2/BUILD.in +++ b/tests/collect_pages/src/pages2/BUILD.in @@ -1 +1,2 @@ # gazelle:js_nextjs_pages_root +# gazelle:js_nextjs_pages_root_name pages2 diff --git a/tests/collect_pages/src/pages2/BUILD.out b/tests/collect_pages/src/pages2/BUILD.out index 05368eb..b5291e8 100644 --- a/tests/collect_pages/src/pages2/BUILD.out +++ b/tests/collect_pages/src/pages2/BUILD.out @@ -1,6 +1,7 @@ load("@aspect_rules_js//js:defs.bzl", "js_library") # gazelle:js_nextjs_pages_root +# gazelle:js_nextjs_pages_root_name pages2 js_library( name = "pages2",