Skip to content

Commit

Permalink
Fixed nextjs pages directives to keep single page
Browse files Browse the repository at this point in the history
files as singletons
  • Loading branch information
ColinHeathman committed Oct 10, 2024
1 parent 063a282 commit 0a7bbf9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
11 changes: 11 additions & 0 deletions gazelle/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type JsConfig struct {
Fix bool
JSRoot string
NextPagesRoot string
NextPagesRootName string
WebAssetSuffixes map[string]bool
Quiet bool
Verbose bool
Expand Down Expand Up @@ -116,6 +117,7 @@ func NewJsConfig() *JsConfig {
Fix: false,
JSRoot: "/",
NextPagesRoot: "",
NextPagesRootName: "pages",
WebAssetSuffixes: make(map[string]bool),
Quiet: false,
Verbose: false,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)

Expand Down
12 changes: 5 additions & 7 deletions gazelle/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions gazelle/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 11 additions & 4 deletions tests/collect_pages/src/pages/BUILD.out
Original file line number Diff line number Diff line change
@@ -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"],
)
1 change: 1 addition & 0 deletions tests/collect_pages/src/pages2/BUILD.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# gazelle:js_nextjs_pages_root
# gazelle:js_nextjs_pages_root_name pages2
1 change: 1 addition & 0 deletions tests/collect_pages/src/pages2/BUILD.out
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 0a7bbf9

Please sign in to comment.