create-plasmic-app 0.0.33 → 0.0.34

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/dist/index.js CHANGED
File without changes
package/dist/lib.js CHANGED
@@ -85,8 +85,10 @@ function create(args) {
85
85
  useTypescript,
86
86
  template,
87
87
  });
88
- // Ensure that
89
- if (useTypescript) {
88
+ // Ensure that we have a empty tsconfig and @types packages
89
+ // Gatsby by default supports typescript handling internally
90
+ // tsconfig so we don't have to ensure it
91
+ if (useTypescript && platform !== "gatsby") {
90
92
  yield file_utils_1.ensureTsconfig(resolvedProjectPath);
91
93
  }
92
94
  // Install dependency
@@ -127,7 +129,10 @@ function create(args) {
127
129
  });
128
130
  }
129
131
  if (scheme === "loader") {
130
- yield cpaStrategy.overwriteFiles({ projectPath: resolvedProjectPath });
132
+ yield cpaStrategy.overwriteFiles({
133
+ projectPath: resolvedProjectPath,
134
+ useTypescript,
135
+ });
131
136
  }
132
137
  else if (scheme === "codegen") {
133
138
  // The loader files to be overwritten are handled by cpaStrategy
@@ -50,13 +50,11 @@ const gatsbyStrategy = {
50
50
  const { projectPath, template } = args;
51
51
  const createCommand = `npx -p gatsby gatsby new ${projectPath}`;
52
52
  const templateArg = template ? ` ${template}` : "";
53
- // Default Gatsby starter already supports Typescript
54
- // See where we `touch tsconfig.json` later on
55
53
  yield cmd_utils_1.spawnOrFail(`${createCommand}${templateArg}`);
56
54
  }),
57
55
  configLoader: (args) => __awaiter(void 0, void 0, void 0, function* () {
58
56
  var e_1, _a;
59
- const { projectId, projectPath, projectApiToken } = args;
57
+ const { projectId, projectPath, projectApiToken, useTypescript } = args;
60
58
  const installResult = yield npm_utils_1.installUpgrade("@plasmicapp/loader-gatsby", {
61
59
  workingDir: projectPath,
62
60
  });
@@ -76,7 +74,7 @@ const gatsbyStrategy = {
76
74
  result += line + "\n";
77
75
  // Prepend PlasmicLoader to list of plugins
78
76
  if (line.includes("plugins:")) {
79
- result += gatsby_1.GATSBY_PLUGIN_CONFIG(projectId, projectApiToken);
77
+ result += gatsby_1.GATSBY_PLUGIN_CONFIG(projectId, projectApiToken, useTypescript);
80
78
  }
81
79
  }
82
80
  }
@@ -92,20 +90,24 @@ const gatsbyStrategy = {
92
90
  overwriteFiles: (args) => __awaiter(void 0, void 0, void 0, function* () {
93
91
  // in gatsby we can delete all existing pages/components, since all pages are going
94
92
  // to be handled by templates/defaultPlasmicPage
95
- const { projectPath } = args;
93
+ const { projectPath, useTypescript } = args;
94
+ const extension = useTypescript ? "ts" : "js";
96
95
  file_utils_1.deleteGlob(path_1.default.join(projectPath, "src/@(pages|components|templates)/*.*"));
97
96
  // Create a very basic 404 page - `gatsby build` fails without it.
98
- // We've deleted the components that the default 404 page depended
99
- // on, so
100
- yield fs_1.promises.writeFile(path_1.default.join(projectPath, "src/pages/404.js"), gatsby_1.GATSBY_404);
97
+ yield fs_1.promises.writeFile(path_1.default.join(projectPath, `src/pages/404.js`), gatsby_1.GATSBY_404);
98
+ yield fs_1.promises.writeFile(path_1.default.join(projectPath, `src/plasmic-init.${extension}`), gatsby_1.makeGatsbyPlasmicInit(extension));
99
+ // Add plasmic-host page
100
+ yield fs_1.promises.writeFile(path_1.default.join(projectPath, `src/pages/plasmic-host.${extension}x`), gatsby_1.makeGatsbyHostPage(extension));
101
101
  // Start with an empty gatsby-node.js
102
102
  yield fs_1.promises.writeFile(path_1.default.join(projectPath, "gatsby-node.js"), "");
103
+ // Updates `gatsby-ssr` to include script tag for preamble
104
+ yield fs_1.promises.writeFile(path_1.default.join(projectPath, "gatsby-ssr.js"), gatsby_1.GATSBY_SSR_CONFIG);
103
105
  const templatesFolder = path_1.default.join(projectPath, "src/templates");
104
- const defaultPagePath = path_1.default.join(templatesFolder, "defaultPlasmicPage.js");
106
+ const defaultPagePath = path_1.default.join(templatesFolder, `defaultPlasmicPage.${extension}x`);
105
107
  if (!fs_1.existsSync(templatesFolder)) {
106
108
  yield fs_1.promises.mkdir(templatesFolder);
107
109
  }
108
- yield fs_1.promises.writeFile(defaultPagePath, gatsby_1.GATSBY_DEFAULT_PAGE);
110
+ yield fs_1.promises.writeFile(defaultPagePath, gatsby_1.makeGatsbyDefaultPage(extension));
109
111
  }),
110
112
  build: (args) => __awaiter(void 0, void 0, void 0, function* () {
111
113
  const { npmRunCmd, projectPath } = args;
@@ -11,6 +11,7 @@ interface ConfigArgs {
11
11
  }
12
12
  interface OverwriteFilesArgs {
13
13
  projectPath: string;
14
+ useTypescript: boolean;
14
15
  }
15
16
  interface BuildArgs {
16
17
  projectPath: string;
@@ -1,3 +1,6 @@
1
- export declare const GATSBY_DEFAULT_PAGE: string;
1
+ export declare const makeGatsbyDefaultPage: (format: "ts" | "js") => string;
2
2
  export declare const GATSBY_404: string;
3
- export declare const GATSBY_PLUGIN_CONFIG: (projectId: string, projectApiToken: string) => string;
3
+ export declare const GATSBY_PLUGIN_CONFIG: (projectId: string, projectApiToken: string, useTypescript: boolean) => string;
4
+ export declare const makeGatsbyHostPage: (format: "ts" | "js") => string;
5
+ export declare const GATSBY_SSR_CONFIG: string;
6
+ export declare const makeGatsbyPlasmicInit: (format: "ts" | "js") => string;
@@ -1,15 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GATSBY_PLUGIN_CONFIG = exports.GATSBY_404 = exports.GATSBY_DEFAULT_PAGE = void 0;
4
- exports.GATSBY_DEFAULT_PAGE = `
3
+ exports.makeGatsbyPlasmicInit = exports.GATSBY_SSR_CONFIG = exports.makeGatsbyHostPage = exports.GATSBY_PLUGIN_CONFIG = exports.GATSBY_404 = exports.makeGatsbyDefaultPage = void 0;
4
+ const file_utils_1 = require("../utils/file-utils");
5
+ const makeGatsbyDefaultPage = (format) => {
6
+ const ts = format === "ts";
7
+ return `
5
8
  import React from "react";
6
9
  import Helmet from "react-helmet";
7
10
  import {
8
- initPlasmicLoader,
9
11
  PlasmicComponent,
10
- PlasmicRootProvider,
12
+ PlasmicRootProvider,${file_utils_1.ifTs(ts, `
13
+ InitOptions,
14
+ ComponentRenderData,`)}
11
15
  } from "@plasmicapp/loader-gatsby";
12
16
  import { graphql } from "gatsby";
17
+ import { initPlasmicLoaderWithRegistrations } from "../plasmic-init";
13
18
 
14
19
  export const query = graphql\`
15
20
  query ($path: String) {
@@ -18,7 +23,14 @@ export const query = graphql\`
18
23
  }
19
24
  \`;
20
25
 
21
- const PlasmicGatsbyPage = ({ data, location }) => {
26
+ ${file_utils_1.ifTs(ts, `interface PlasmicGatsbyPageProps {
27
+ data: {
28
+ plasmicOptions: InitOptions
29
+ plasmicComponents: ComponentRenderData
30
+ }
31
+ }
32
+ `)}
33
+ const PlasmicGatsbyPage = ({ data }${file_utils_1.ifTs(ts, ": PlasmicGatsbyPageProps")}) => {
22
34
  const {
23
35
  plasmicComponents,
24
36
  plasmicOptions,
@@ -27,7 +39,7 @@ const PlasmicGatsbyPage = ({ data, location }) => {
27
39
  const pageMetadata = pageMeta.pageMetadata;
28
40
  return (
29
41
  <PlasmicRootProvider
30
- loader={initPlasmicLoader(plasmicOptions)}
42
+ loader={initPlasmicLoaderWithRegistrations(plasmicOptions)}
31
43
  prefetchedData={plasmicComponents}
32
44
  >
33
45
  <Helmet>
@@ -43,13 +55,15 @@ const PlasmicGatsbyPage = ({ data, location }) => {
43
55
 
44
56
  export default PlasmicGatsbyPage;
45
57
  `.trim();
58
+ };
59
+ exports.makeGatsbyDefaultPage = makeGatsbyDefaultPage;
46
60
  exports.GATSBY_404 = `
47
61
  const NotFound = () => {
48
62
  return "Not Found";
49
63
  };
50
64
  export default NotFound;
51
65
  `.trim();
52
- const GATSBY_PLUGIN_CONFIG = (projectId, projectApiToken) => `
66
+ const GATSBY_PLUGIN_CONFIG = (projectId, projectApiToken, useTypescript) => `
53
67
  {
54
68
  resolve: "@plasmicapp/loader-gatsby",
55
69
  options: {
@@ -59,8 +73,98 @@ const GATSBY_PLUGIN_CONFIG = (projectId, projectApiToken) => `
59
73
  token: "${projectApiToken}",
60
74
  },
61
75
  ], // An array of project ids.
62
- defaultPlasmicPage: require.resolve("./src/templates/defaultPlasmicPage.js"),
76
+ defaultPlasmicPage: require.resolve("./src/templates/defaultPlasmicPage.${useTypescript ? "tsx" : "jsx"}"),
63
77
  },
64
78
  },
65
79
  `;
66
80
  exports.GATSBY_PLUGIN_CONFIG = GATSBY_PLUGIN_CONFIG;
81
+ const makeGatsbyHostPage = (format) => {
82
+ const ts = format === "ts";
83
+ return `
84
+ import * as React from "react"
85
+ import {
86
+ PlasmicCanvasHost,${file_utils_1.ifTs(ts, `
87
+ InitOptions,`)}
88
+ } from "@plasmicapp/loader-gatsby"
89
+ import { graphql } from "gatsby"
90
+ import { initPlasmicLoaderWithRegistrations } from "../plasmic-init"
91
+
92
+ export const query = graphql\`
93
+ query {
94
+ plasmicOptions
95
+ }
96
+ \`
97
+
98
+ ${file_utils_1.ifTs(ts, `interface HostProps {
99
+ data: {
100
+ plasmicOptions: InitOptions;
101
+ }
102
+ }
103
+ `)}
104
+ export default function Host({ data }${file_utils_1.ifTs(ts, ": HostProps")}) {
105
+ const { plasmicOptions } = data
106
+ initPlasmicLoaderWithRegistrations(plasmicOptions)
107
+ return <PlasmicCanvasHost />
108
+ }
109
+ `.trim();
110
+ };
111
+ exports.makeGatsbyHostPage = makeGatsbyHostPage;
112
+ exports.GATSBY_SSR_CONFIG = `
113
+ /**
114
+ * Implement Gatsby's SSR (Server Side Rendering) APIs in this file.
115
+ *
116
+ * See: https://www.gatsbyjs.com/docs/ssr-apis/
117
+ */
118
+
119
+ const React = require("react")
120
+
121
+ /**
122
+ * Add preamble to allow functional code components in studio.
123
+ *
124
+ * See: https://docs.plasmic.app/learn/functional-code-components/
125
+ */
126
+ const HeadComponents = [
127
+ <script
128
+ key="plasmic-preamble"
129
+ src="https://static1.plasmic.app/preamble.js"
130
+ />,
131
+ ]
132
+
133
+ const isProduction = process.env.NODE_ENV === "production"
134
+
135
+ exports.onRenderBody = ({ pathname, setHeadComponents }) => {
136
+ /**
137
+ * We add the preamble tag script to all pages during development mode
138
+ * because during development all pages are dynamically rendered based
139
+ * on \`/\` route, during production we add it only in \`/plasmic-host/\`
140
+ */
141
+ if (!isProduction || pathname === "/plasmic-host/") {
142
+ setHeadComponents(HeadComponents)
143
+ }
144
+ }
145
+ `.trim();
146
+ const makeGatsbyPlasmicInit = (format) => {
147
+ const ts = format === "ts";
148
+ return `
149
+ import {
150
+ initPlasmicLoader,${file_utils_1.ifTs(ts, `
151
+ InitOptions,`)}
152
+ } from "@plasmicapp/loader-gatsby";
153
+
154
+ export function initPlasmicLoaderWithRegistrations(plasmicOptions${file_utils_1.ifTs(ts, ": InitOptions")}) {
155
+ const PLASMIC = initPlasmicLoader(plasmicOptions);
156
+
157
+ // You can register any code components that you want to use here; see
158
+ // https://docs.plasmic.app/learn/code-components-ref/
159
+ // And configure your Plasmic project to use the host url pointing at
160
+ // the /plasmic-host page of your nextjs app (for example,
161
+ // http://localhost:8000/plasmic-host). See
162
+ // https://docs.plasmic.app/learn/app-hosting/#set-a-plasmic-project-to-use-your-app-host
163
+
164
+ // PLASMIC.registerComponent(...);
165
+
166
+ return PLASMIC;
167
+ }
168
+ `.trim();
169
+ };
170
+ exports.makeGatsbyPlasmicInit = makeGatsbyPlasmicInit;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.makeNextjsHostPage = exports.makeNextjsCatchallPage = exports.makeNextjsInitPage = void 0;
4
+ const file_utils_1 = require("../utils/file-utils");
4
5
  const makeNextjsInitPage = (projectId, projectApiToken) => `
5
6
  import { initPlasmicLoader } from "@plasmicapp/loader-nextjs";
6
7
 
@@ -29,15 +30,12 @@ export const PLASMIC = initPlasmicLoader({
29
30
  // PLASMIC.registerComponent(...);
30
31
  `.trim();
31
32
  exports.makeNextjsInitPage = makeNextjsInitPage;
32
- function ifTs(ts, str) {
33
- return ts ? str : "";
34
- }
35
33
  function makeNextjsCatchallPage(format) {
36
34
  const ts = format === "ts";
37
35
  return `
38
36
  import * as React from "react";
39
37
  import { PlasmicComponent } from "@plasmicapp/loader-nextjs";
40
- ${ifTs(ts, `import { GetStaticPaths, GetStaticProps } from "next";\n`)}
38
+ ${file_utils_1.ifTs(ts, `import { GetStaticPaths, GetStaticProps } from "next";\n`)}
41
39
  import {
42
40
  ComponentRenderData,
43
41
  PlasmicRootProvider,
@@ -45,7 +43,7 @@ import {
45
43
  import Error from "next/error";
46
44
  import { PLASMIC } from "../plasmic-init";
47
45
 
48
- export default function PlasmicLoaderPage(props${ifTs(ts, `: {
46
+ export default function PlasmicLoaderPage(props${file_utils_1.ifTs(ts, `: {
49
47
  plasmicData?: ComponentRenderData;
50
48
  }`)}) {
51
49
  const { plasmicData } = props;
@@ -62,7 +60,7 @@ export default function PlasmicLoaderPage(props${ifTs(ts, `: {
62
60
  );
63
61
  }
64
62
 
65
- export const getStaticProps${ifTs(ts, `: GetStaticProps`)} = async (context) => {
63
+ export const getStaticProps${file_utils_1.ifTs(ts, `: GetStaticProps`)} = async (context) => {
66
64
  const { catchall } = context.params ?? {};
67
65
  const plasmicPath = typeof catchall === 'string' ? catchall : Array.isArray(catchall) ? \`/\${catchall.join('/')}\` : '/';
68
66
  const plasmicData = await PLASMIC.maybeFetchComponentData(plasmicPath);
@@ -80,7 +78,7 @@ export const getStaticProps${ifTs(ts, `: GetStaticProps`)} = async (context) =>
80
78
  };
81
79
  }
82
80
 
83
- export const getStaticPaths${ifTs(ts, `: GetStaticPaths`)} = async () => {
81
+ export const getStaticPaths${file_utils_1.ifTs(ts, `: GetStaticPaths`)} = async () => {
84
82
  const pageModules = await PLASMIC.fetchPages();
85
83
  return {
86
84
  paths: pageModules.map((mod) => ({
@@ -33,3 +33,4 @@ export declare function overwriteIndex(projectPath: string, platform: string, sc
33
33
  export declare function overwriteReadme(projectPath: string, platform: PlatformType, buildCommand: string): Promise<void>;
34
34
  export declare function ensureTsconfig(projectPath: string): Promise<void>;
35
35
  export declare function wrapAppRoot(projectPath: string, platform: string, scheme: string): Promise<void>;
36
+ export declare function ifTs(ts: boolean, str: string): string;
@@ -31,7 +31,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
31
31
  return (mod && mod.__esModule) ? mod : { "default": mod };
32
32
  };
33
33
  Object.defineProperty(exports, "__esModule", { value: true });
34
- exports.wrapAppRoot = exports.ensureTsconfig = exports.overwriteReadme = exports.overwriteIndex = exports.writePlasmicLoaderJson = exports.writeDefaultNextjsConfig = exports.stripExtension = exports.deleteGlob = void 0;
34
+ exports.ifTs = exports.wrapAppRoot = exports.ensureTsconfig = exports.overwriteReadme = exports.overwriteIndex = exports.writePlasmicLoaderJson = exports.writeDefaultNextjsConfig = exports.stripExtension = exports.deleteGlob = void 0;
35
35
  const fs_1 = require("fs");
36
36
  const glob_1 = __importDefault(require("glob"));
37
37
  const lodash_1 = __importDefault(require("lodash"));
@@ -326,3 +326,7 @@ export const wrapRootElement = ({ element }) => {
326
326
  });
327
327
  }
328
328
  exports.wrapAppRoot = wrapAppRoot;
329
+ function ifTs(ts, str) {
330
+ return ts ? str : "";
331
+ }
332
+ exports.ifTs = ifTs;
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-plasmic-app",
3
- "version": "0.0.33",
3
+ "version": "0.0.34",
4
4
  "description": "Create Plasmic-powered React apps",
5
5
  "main": "./dist/lib.js",
6
6
  "types": "./dist/lib.d.ts",
package/src/lib.ts CHANGED
@@ -84,8 +84,10 @@ export async function create(args: CreatePlasmicAppArgs): Promise<void> {
84
84
  template,
85
85
  });
86
86
 
87
- // Ensure that
88
- if (useTypescript) {
87
+ // Ensure that we have a empty tsconfig and @types packages
88
+ // Gatsby by default supports typescript handling internally
89
+ // tsconfig so we don't have to ensure it
90
+ if (useTypescript && platform !== "gatsby") {
89
91
  await ensureTsconfig(resolvedProjectPath);
90
92
  }
91
93
 
@@ -136,7 +138,10 @@ export async function create(args: CreatePlasmicAppArgs): Promise<void> {
136
138
  }
137
139
 
138
140
  if (scheme === "loader") {
139
- await cpaStrategy.overwriteFiles({ projectPath: resolvedProjectPath });
141
+ await cpaStrategy.overwriteFiles({
142
+ projectPath: resolvedProjectPath,
143
+ useTypescript,
144
+ });
140
145
  } else if (scheme === "codegen") {
141
146
  // The loader files to be overwritten are handled by cpaStrategy
142
147
  // but for codegen we still have to run it
@@ -3,8 +3,11 @@ import path from "path";
3
3
  import * as readline from "readline";
4
4
  import {
5
5
  GATSBY_404,
6
- GATSBY_DEFAULT_PAGE,
7
6
  GATSBY_PLUGIN_CONFIG,
7
+ GATSBY_SSR_CONFIG,
8
+ makeGatsbyDefaultPage,
9
+ makeGatsbyHostPage,
10
+ makeGatsbyPlasmicInit,
8
11
  } from "../templates/gatsby";
9
12
  import { spawnOrFail } from "../utils/cmd-utils";
10
13
  import { deleteGlob } from "../utils/file-utils";
@@ -17,12 +20,10 @@ const gatsbyStrategy: CPAStrategy = {
17
20
  const createCommand = `npx -p gatsby gatsby new ${projectPath}`;
18
21
  const templateArg = template ? ` ${template}` : "";
19
22
 
20
- // Default Gatsby starter already supports Typescript
21
- // See where we `touch tsconfig.json` later on
22
23
  await spawnOrFail(`${createCommand}${templateArg}`);
23
24
  },
24
25
  configLoader: async (args) => {
25
- const { projectId, projectPath, projectApiToken } = args;
26
+ const { projectId, projectPath, projectApiToken, useTypescript } = args;
26
27
 
27
28
  const installResult = await installUpgrade("@plasmicapp/loader-gatsby", {
28
29
  workingDir: projectPath,
@@ -43,7 +44,11 @@ const gatsbyStrategy: CPAStrategy = {
43
44
  result += line + "\n";
44
45
  // Prepend PlasmicLoader to list of plugins
45
46
  if (line.includes("plugins:")) {
46
- result += GATSBY_PLUGIN_CONFIG(projectId, projectApiToken);
47
+ result += GATSBY_PLUGIN_CONFIG(
48
+ projectId,
49
+ projectApiToken,
50
+ useTypescript
51
+ );
47
52
  }
48
53
  }
49
54
  await fs.writeFile(gatsbyConfigFile, result);
@@ -51,25 +56,45 @@ const gatsbyStrategy: CPAStrategy = {
51
56
  overwriteFiles: async (args) => {
52
57
  // in gatsby we can delete all existing pages/components, since all pages are going
53
58
  // to be handled by templates/defaultPlasmicPage
54
- const { projectPath } = args;
59
+ const { projectPath, useTypescript } = args;
60
+
61
+ const extension = useTypescript ? "ts" : "js";
55
62
 
56
63
  deleteGlob(path.join(projectPath, "src/@(pages|components|templates)/*.*"));
57
64
 
58
65
  // Create a very basic 404 page - `gatsby build` fails without it.
59
- // We've deleted the components that the default 404 page depended
60
- // on, so
61
- await fs.writeFile(path.join(projectPath, "src/pages/404.js"), GATSBY_404);
66
+ await fs.writeFile(path.join(projectPath, `src/pages/404.js`), GATSBY_404);
67
+
68
+ await fs.writeFile(
69
+ path.join(projectPath, `src/plasmic-init.${extension}`),
70
+ makeGatsbyPlasmicInit(extension)
71
+ );
72
+
73
+ // Add plasmic-host page
74
+ await fs.writeFile(
75
+ path.join(projectPath, `src/pages/plasmic-host.${extension}x`),
76
+ makeGatsbyHostPage(extension)
77
+ );
62
78
 
63
79
  // Start with an empty gatsby-node.js
64
80
  await fs.writeFile(path.join(projectPath, "gatsby-node.js"), "");
65
81
 
82
+ // Updates `gatsby-ssr` to include script tag for preamble
83
+ await fs.writeFile(
84
+ path.join(projectPath, "gatsby-ssr.js"),
85
+ GATSBY_SSR_CONFIG
86
+ );
87
+
66
88
  const templatesFolder = path.join(projectPath, "src/templates");
67
- const defaultPagePath = path.join(templatesFolder, "defaultPlasmicPage.js");
89
+ const defaultPagePath = path.join(
90
+ templatesFolder,
91
+ `defaultPlasmicPage.${extension}x`
92
+ );
68
93
 
69
94
  if (!existsSync(templatesFolder)) {
70
95
  await fs.mkdir(templatesFolder);
71
96
  }
72
- await fs.writeFile(defaultPagePath, GATSBY_DEFAULT_PAGE);
97
+ await fs.writeFile(defaultPagePath, makeGatsbyDefaultPage(extension));
73
98
  },
74
99
  build: async (args) => {
75
100
  const { npmRunCmd, projectPath } = args;
@@ -13,6 +13,7 @@ interface ConfigArgs {
13
13
 
14
14
  interface OverwriteFilesArgs {
15
15
  projectPath: string;
16
+ useTypescript: boolean;
16
17
  }
17
18
 
18
19
  interface BuildArgs {
@@ -1,12 +1,21 @@
1
- export const GATSBY_DEFAULT_PAGE = `
1
+ import { ifTs } from "../utils/file-utils";
2
+
3
+ export const makeGatsbyDefaultPage = (format: "ts" | "js"): string => {
4
+ const ts = format === "ts";
5
+ return `
2
6
  import React from "react";
3
7
  import Helmet from "react-helmet";
4
8
  import {
5
- initPlasmicLoader,
6
9
  PlasmicComponent,
7
- PlasmicRootProvider,
10
+ PlasmicRootProvider,${ifTs(
11
+ ts,
12
+ `
13
+ InitOptions,
14
+ ComponentRenderData,`
15
+ )}
8
16
  } from "@plasmicapp/loader-gatsby";
9
17
  import { graphql } from "gatsby";
18
+ import { initPlasmicLoaderWithRegistrations } from "../plasmic-init";
10
19
 
11
20
  export const query = graphql\`
12
21
  query ($path: String) {
@@ -15,7 +24,17 @@ export const query = graphql\`
15
24
  }
16
25
  \`;
17
26
 
18
- const PlasmicGatsbyPage = ({ data, location }) => {
27
+ ${ifTs(
28
+ ts,
29
+ `interface PlasmicGatsbyPageProps {
30
+ data: {
31
+ plasmicOptions: InitOptions
32
+ plasmicComponents: ComponentRenderData
33
+ }
34
+ }
35
+ `
36
+ )}
37
+ const PlasmicGatsbyPage = ({ data }${ifTs(ts, ": PlasmicGatsbyPageProps")}) => {
19
38
  const {
20
39
  plasmicComponents,
21
40
  plasmicOptions,
@@ -24,7 +43,7 @@ const PlasmicGatsbyPage = ({ data, location }) => {
24
43
  const pageMetadata = pageMeta.pageMetadata;
25
44
  return (
26
45
  <PlasmicRootProvider
27
- loader={initPlasmicLoader(plasmicOptions)}
46
+ loader={initPlasmicLoaderWithRegistrations(plasmicOptions)}
28
47
  prefetchedData={plasmicComponents}
29
48
  >
30
49
  <Helmet>
@@ -40,6 +59,7 @@ const PlasmicGatsbyPage = ({ data, location }) => {
40
59
 
41
60
  export default PlasmicGatsbyPage;
42
61
  `.trim();
62
+ };
43
63
 
44
64
  export const GATSBY_404 = `
45
65
  const NotFound = () => {
@@ -50,7 +70,8 @@ export default NotFound;
50
70
 
51
71
  export const GATSBY_PLUGIN_CONFIG = (
52
72
  projectId: string,
53
- projectApiToken: string
73
+ projectApiToken: string,
74
+ useTypescript: boolean
54
75
  ): string => `
55
76
  {
56
77
  resolve: "@plasmicapp/loader-gatsby",
@@ -61,7 +82,112 @@ export const GATSBY_PLUGIN_CONFIG = (
61
82
  token: "${projectApiToken}",
62
83
  },
63
84
  ], // An array of project ids.
64
- defaultPlasmicPage: require.resolve("./src/templates/defaultPlasmicPage.js"),
85
+ defaultPlasmicPage: require.resolve("./src/templates/defaultPlasmicPage.${
86
+ useTypescript ? "tsx" : "jsx"
87
+ }"),
65
88
  },
66
89
  },
67
90
  `;
91
+
92
+ export const makeGatsbyHostPage = (format: "ts" | "js"): string => {
93
+ const ts = format === "ts";
94
+ return `
95
+ import * as React from "react"
96
+ import {
97
+ PlasmicCanvasHost,${ifTs(
98
+ ts,
99
+ `
100
+ InitOptions,`
101
+ )}
102
+ } from "@plasmicapp/loader-gatsby"
103
+ import { graphql } from "gatsby"
104
+ import { initPlasmicLoaderWithRegistrations } from "../plasmic-init"
105
+
106
+ export const query = graphql\`
107
+ query {
108
+ plasmicOptions
109
+ }
110
+ \`
111
+
112
+ ${ifTs(
113
+ ts,
114
+ `interface HostProps {
115
+ data: {
116
+ plasmicOptions: InitOptions;
117
+ }
118
+ }
119
+ `
120
+ )}
121
+ export default function Host({ data }${ifTs(ts, ": HostProps")}) {
122
+ const { plasmicOptions } = data
123
+ initPlasmicLoaderWithRegistrations(plasmicOptions)
124
+ return <PlasmicCanvasHost />
125
+ }
126
+ `.trim();
127
+ };
128
+
129
+ export const GATSBY_SSR_CONFIG = `
130
+ /**
131
+ * Implement Gatsby's SSR (Server Side Rendering) APIs in this file.
132
+ *
133
+ * See: https://www.gatsbyjs.com/docs/ssr-apis/
134
+ */
135
+
136
+ const React = require("react")
137
+
138
+ /**
139
+ * Add preamble to allow functional code components in studio.
140
+ *
141
+ * See: https://docs.plasmic.app/learn/functional-code-components/
142
+ */
143
+ const HeadComponents = [
144
+ <script
145
+ key="plasmic-preamble"
146
+ src="https://static1.plasmic.app/preamble.js"
147
+ />,
148
+ ]
149
+
150
+ const isProduction = process.env.NODE_ENV === "production"
151
+
152
+ exports.onRenderBody = ({ pathname, setHeadComponents }) => {
153
+ /**
154
+ * We add the preamble tag script to all pages during development mode
155
+ * because during development all pages are dynamically rendered based
156
+ * on \`/\` route, during production we add it only in \`/plasmic-host/\`
157
+ */
158
+ if (!isProduction || pathname === "/plasmic-host/") {
159
+ setHeadComponents(HeadComponents)
160
+ }
161
+ }
162
+ `.trim();
163
+
164
+ export const makeGatsbyPlasmicInit = (format: "ts" | "js"): string => {
165
+ const ts = format === "ts";
166
+ return `
167
+ import {
168
+ initPlasmicLoader,${ifTs(
169
+ ts,
170
+ `
171
+ InitOptions,`
172
+ )}
173
+ } from "@plasmicapp/loader-gatsby";
174
+
175
+ export function initPlasmicLoaderWithRegistrations(plasmicOptions${ifTs(
176
+ ts,
177
+ ": InitOptions"
178
+ )}) {
179
+ const PLASMIC = initPlasmicLoader(plasmicOptions);
180
+
181
+ // You can register any code components that you want to use here; see
182
+ // https://docs.plasmic.app/learn/code-components-ref/
183
+ // And configure your Plasmic project to use the host url pointing at
184
+ // the /plasmic-host page of your nextjs app (for example,
185
+ // http://localhost:8000/plasmic-host). See
186
+ // https://docs.plasmic.app/learn/app-hosting/#set-a-plasmic-project-to-use-your-app-host
187
+
188
+ // PLASMIC.registerComponent(...);
189
+
190
+ return PLASMIC;
191
+ }
192
+ `.trim();
193
+ };
@@ -1,3 +1,5 @@
1
+ import { ifTs } from "../utils/file-utils";
2
+
1
3
  export const makeNextjsInitPage = (
2
4
  projectId: string,
3
5
  projectApiToken: string
@@ -30,10 +32,6 @@ export const PLASMIC = initPlasmicLoader({
30
32
  // PLASMIC.registerComponent(...);
31
33
  `.trim();
32
34
 
33
- function ifTs(ts: boolean, str: string) {
34
- return ts ? str : "";
35
- }
36
-
37
35
  export function makeNextjsCatchallPage(format: "ts" | "js"): string {
38
36
  const ts = format === "ts";
39
37
  return `
@@ -384,3 +384,7 @@ export const wrapRootElement = ({ element }) => {
384
384
  await fs.writeFile(ssrFilePath, wrapperContent);
385
385
  }
386
386
  }
387
+
388
+ export function ifTs(ts: boolean, str: string) {
389
+ return ts ? str : "";
390
+ }