@valbuild/init 0.62.3 → 0.62.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/init.ts CHANGED
@@ -14,6 +14,7 @@ import {
14
14
  VAL_APP_PAGE,
15
15
  VAL_CLIENT,
16
16
  VAL_CONFIG,
17
+ VAL_MODULES,
17
18
  VAL_RSC,
18
19
  VAL_SERVER,
19
20
  } from "./templates";
@@ -288,6 +289,7 @@ type Plan = Partial<{
288
289
  repo: string;
289
290
  };
290
291
  includeExample: false | FileOp;
292
+ includeModules: false | FileOp;
291
293
  updateVSCodeSettings: false | FileOp;
292
294
  }>;
293
295
 
@@ -486,19 +488,76 @@ async function plan(
486
488
  source: VAL_CONFIG(!!analysis.isTypeScript, {}),
487
489
  };
488
490
 
491
+ {
492
+ const answer = await confirm({
493
+ message: "Include example Val files?",
494
+ default: true,
495
+ });
496
+ if (answer) {
497
+ const exampleDir = path.join(analysis.srcDir, "examples", "val");
498
+ const examplePath = path.join(
499
+ exampleDir,
500
+ "example.val." + (analysis.isJavaScript ? "js" : "ts")
501
+ );
502
+ const exampleImport = path
503
+ .relative(exampleDir, valConfigPath)
504
+ .replace(".js", "")
505
+ .replace(".ts", "");
506
+ if (!analysis.packageJsonDir) {
507
+ throw Error(
508
+ "Could not detect package.json directory! This is a Val bug."
509
+ );
510
+ }
511
+ const exampleModuleFilePath = `/${path.relative(
512
+ analysis.packageJsonDir,
513
+ examplePath
514
+ )}`;
515
+
516
+ plan.includeExample = {
517
+ path: examplePath,
518
+ source: BASIC_EXAMPLE(
519
+ exampleModuleFilePath,
520
+ exampleImport,
521
+ !!analysis.isJavaScript
522
+ ),
523
+ };
524
+ }
525
+ }
526
+
527
+ const valModulesDir = analysis.root;
528
+ const valModulesImport = path
529
+ .relative(valModulesDir, valConfigPath)
530
+ .replace(".js", "")
531
+ .replace(".ts", "");
532
+ const exampleModuleFilePath = plan.includeExample
533
+ ? plan.includeExample.path
534
+ : undefined;
535
+ const exampleModuleImport =
536
+ exampleModuleFilePath &&
537
+ path
538
+ .relative(valModulesDir, exampleModuleFilePath)
539
+ .replace(".js", "")
540
+ .replace(".ts", "");
541
+ plan.includeModules = {
542
+ path: path.join(valModulesDir, "val.modules.ts"),
543
+ source: VAL_MODULES(valModulesImport, exampleModuleImport),
544
+ };
489
545
  const valUtilsDir = path.join(analysis.srcDir, "val");
546
+ const valModulesServerImport = path
547
+ .relative(valUtilsDir, plan.includeModules.path)
548
+ .replace(".js", "")
549
+ .replace(".ts", "");
490
550
  const valUtilsImportPath = path
491
551
  .relative(valUtilsDir, valConfigPath)
492
552
  .replace(".js", "")
493
553
  .replace(".ts", "");
494
-
495
554
  const valServerPath = path.join(
496
555
  valUtilsDir,
497
556
  analysis.isTypeScript ? "val.server.ts" : "val.server.js"
498
557
  );
499
558
  plan.createValServer = {
500
559
  path: valServerPath,
501
- source: VAL_SERVER(valUtilsImportPath),
560
+ source: VAL_SERVER(valUtilsImportPath, valModulesServerImport),
502
561
  };
503
562
 
504
563
  if (!analysis.appRouterPath) {
@@ -764,43 +823,6 @@ async function plan(
764
823
  }
765
824
  }
766
825
 
767
- {
768
- const answer = await confirm({
769
- message: "Include example Val files?",
770
- default: true,
771
- });
772
- if (answer) {
773
- const exampleDir = path.join(analysis.srcDir, "examples", "val");
774
- const examplePath = path.join(
775
- exampleDir,
776
- "example.val." + (analysis.isJavaScript ? "js" : "ts")
777
- );
778
- const exampleImport = path
779
- .relative(exampleDir, valConfigPath)
780
- .replace(".js", "")
781
- .replace(".ts", "");
782
- if (!analysis.packageJsonDir) {
783
- throw Error(
784
- "Could not detect package.json directory! This is a Val bug."
785
- );
786
- }
787
- const exampleModuleId = `/${path
788
- .relative(analysis.packageJsonDir, examplePath)
789
- .replace(".val", "")
790
- .replace(".js", "")
791
- .replace(".ts", "")}`;
792
-
793
- plan.includeExample = {
794
- path: examplePath,
795
- source: BASIC_EXAMPLE(
796
- exampleModuleId,
797
- exampleImport,
798
- !!analysis.isJavaScript
799
- ),
800
- };
801
- }
802
- }
803
-
804
826
  return plan;
805
827
  }
806
828
 
package/src/templates.ts CHANGED
@@ -21,12 +21,17 @@ const { fetchValStega: fetchVal } = initValRsc(config, {
21
21
  export { fetchVal };
22
22
  `;
23
23
 
24
- export const VAL_SERVER = (configImportPath: string) => `import "server-only";
24
+ export const VAL_SERVER = (
25
+ configImportPath: string,
26
+ valModulesImportPath: string
27
+ ) => `import "server-only";
25
28
  import { initValServer } from "@valbuild/next/server";
26
29
  import { config } from "${configImportPath}";
27
30
  import { draftMode } from "next/headers";
31
+ import valModules from "${valModulesImportPath}";
28
32
 
29
33
  const { valNextAppRouter } = initValServer(
34
+ valModules,
30
35
  { ...config },
31
36
  {
32
37
  draftMode,
@@ -76,8 +81,25 @@ export default function Val() {
76
81
  }
77
82
  `;
78
83
 
84
+ export const VAL_MODULES = (
85
+ configImportPath: string,
86
+ exampleModuleImport?: string
87
+ ) => `import { modules } from "@valbuild/next";
88
+ import { config } from "./${configImportPath}";
89
+
90
+ export default modules(config, [
91
+ // Add your modules here${
92
+ exampleModuleImport
93
+ ? `
94
+ { def: () => import("./${exampleModuleImport}") },`
95
+ : ""
96
+ }
97
+ ]);
98
+
99
+ `;
100
+
79
101
  export const BASIC_EXAMPLE = (
80
- moduleId: string,
102
+ moduleFilePath: string,
81
103
  configImportPath: string,
82
104
  isJavaScript: boolean
83
105
  ) => `${isJavaScript ? "// @ts-check\n" : ""}/**
@@ -187,7 +209,7 @@ export type TestContent = t.inferSchema<typeof testSchema>;
187
209
  *
188
210
  * NOTE: the first argument, module id, must match the path of the file.
189
211
  */
190
- export default c.define("${moduleId}", testSchema, {
212
+ export default c.define("${moduleFilePath}", testSchema, {
191
213
  text: "Basic text content",
192
214
  optionals: null,
193
215
  arrays: ["A string"],