Skip to content

Commit

Permalink
Added directive js_collect_pages which can be used in a NextJS
Browse files Browse the repository at this point in the history
style page root to mark each direct leaf as a page
  • Loading branch information
ColinHeathman committed Oct 9, 2024
1 parent 6542264 commit 063a282
Show file tree
Hide file tree
Showing 29 changed files with 324 additions and 101 deletions.
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
57 changes: 29 additions & 28 deletions examples/nextjs/apps/alpha/pages/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,35 +1,10 @@
load("//bazel:jest_test.bzl", "jest_test")
load("//bazel:ts_project.bzl", "ts_project")

# gazelle:js_collect_all

ts_project(
name = "pages",
srcs = [
"_app.tsx",
"api/hello.ts",
"index.tsx",
],
data = [
"//:node_modules/@nextjs-example/one",
"//:node_modules/next",
"//:node_modules/react",
"//apps/alpha:node_modules/is-even",
"//apps/alpha/styles:assets",
],
visibility = ["//apps/alpha:__subpackages__"],
deps = [
"//:node_modules/@nextjs-example/one",
"//:node_modules/@types/react",
"//:node_modules/next",
"//:node_modules/react",
"//apps/alpha:node_modules/@types/is-even",
"//apps/alpha:node_modules/is-even",
],
)
# gazelle:js_nextjs_pages_root

jest_test(
name = "pages_test",
name = "index.test",
srcs = ["index.test.tsx"],
config = "//apps/alpha:jest.config",
data = [
Expand All @@ -42,7 +17,7 @@ jest_test(
"//:node_modules/react",
"//apps/alpha:package_json",
],
snapshots = ["__snapshots__"],
snapshots = ["__snapshots__/index.test.tsx.snap"],
visibility = ["//apps/alpha:__subpackages__"],
deps = [
":pages",
Expand All @@ -52,5 +27,31 @@ jest_test(
"//:node_modules/jest-environment-jsdom",
"//:node_modules/jest-transform-stub",
"//:node_modules/react",
"//apps/alpha/pages/api",
],
)

ts_project(
name = "pages",
srcs = [
"_app.tsx",
"index.tsx",
],
data = [
"//:node_modules/@nextjs-example/one",
"//:node_modules/next",
"//:node_modules/react",
"//apps/alpha:node_modules/is-even",
"//apps/alpha/styles:assets",
],
visibility = ["//apps/alpha:__subpackages__"],
deps = [
"//:node_modules/@nextjs-example/one",
"//:node_modules/@types/react",
"//:node_modules/next",
"//:node_modules/react",
"//apps/alpha:node_modules/@types/is-even",
"//apps/alpha:node_modules/is-even",
"//apps/alpha/pages/api",
],
)
9 changes: 9 additions & 0 deletions examples/nextjs/apps/alpha/pages/api/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("//bazel:ts_project.bzl", "ts_project")

ts_project(
name = "api",
srcs = ["hello.ts"],
data = ["//:node_modules/next"],
visibility = ["//apps/alpha:__subpackages__"],
deps = ["//:node_modules/next"],
)
89 changes: 62 additions & 27 deletions gazelle/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,16 @@ type JsConfig struct {
CollectWebAssets bool
CollectAllAssets bool
CollectedAssets map[string]bool
CollectPages bool
CollectedPages map[string]bool
CollectPageFiles bool
CollectedPageFiles map[string]bool
CollectAll bool
CollectAllRoot string
CollectAllSources map[string]bool
Fix bool
JSRoot string
NextPagesRoot string
WebAssetSuffixes map[string]bool
Quiet bool
Verbose bool
Expand All @@ -97,21 +102,26 @@ func NewJsConfig() *JsConfig {
Visibility: Visibility{
Labels: []string{},
},
CollectBarrels: false,
CollectWebAssets: false,
CollectAllAssets: false,
CollectedAssets: make(map[string]bool),
CollectAll: false,
CollectAllRoot: "",
CollectAllSources: make(map[string]bool),
Fix: false,
JSRoot: "/",
WebAssetSuffixes: make(map[string]bool),
Quiet: false,
Verbose: false,
DefaultNpmLabel: "//:node_modules/",
JestTestsPerShard: -1,
JestConfig: "",
CollectBarrels: false,
CollectWebAssets: false,
CollectAllAssets: false,
CollectedAssets: make(map[string]bool),
CollectPages: false,
CollectedPages: make(map[string]bool),
CollectPageFiles: false,
CollectedPageFiles: make(map[string]bool),
CollectAll: false,
CollectAllRoot: "",
CollectAllSources: make(map[string]bool),
Fix: false,
JSRoot: "/",
NextPagesRoot: "",
WebAssetSuffixes: make(map[string]bool),
Quiet: false,
Verbose: false,
DefaultNpmLabel: "//:node_modules/",
JestTestsPerShard: -1,
JestConfig: "",
}
}

Expand Down Expand Up @@ -159,6 +169,18 @@ func (parent *JsConfig) NewChild() *JsConfig {
child.CollectAllAssets = parent.CollectAllAssets
child.CollectedAssets = parent.CollectedAssets // Reinitialized on change to JSRoot

child.NextPagesRoot = parent.NextPagesRoot
if parent.CollectPages { // If the parent is collecting pages, the child should collect page files
child.CollectPages = false
child.CollectPageFiles = true
child.CollectedPages = parent.CollectedPages
child.CollectedPageFiles = make(map[string]bool)
} else {
child.CollectPageFiles = parent.CollectPageFiles
child.CollectedPages = parent.CollectedPages
child.CollectedPageFiles = parent.CollectedPageFiles
}

child.CollectAll = parent.CollectAll
child.CollectAllRoot = parent.CollectAllRoot
child.CollectAllSources = parent.CollectAllSources // Copy reference, reinitialized on change to CollectAll
Expand Down Expand Up @@ -223,6 +245,7 @@ func (*JS) KnownDirectives() []string {
return []string{
"js_extension",
"js_root",
"js_nextjs_pages_root",
"js_lookup_types",
"js_fix",
"js_package_file",
Expand Down Expand Up @@ -286,7 +309,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 +321,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 +334,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 +346,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 +369,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 +383,24 @@ 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_nextjs_pages_root":
nextPagesRoot, err := filepath.Rel(".", f.Pkg)
if err != nil {
log.Fatal(Err("failed to read directive %s: %v", directive.Key, err))
} else {
jsConfig.NextPagesRoot = nextPagesRoot
jsConfig.CollectAll = false
jsConfig.CollectPages = true
jsConfig.CollectedPages = make(map[string]bool)
jsConfig.CollectPageFiles = false
}

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

Expand All @@ -387,7 +422,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 +445,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 +551,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 +563,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

0 comments on commit 063a282

Please sign in to comment.