@slicemachine/adapter-sveltekit 0.3.78-beta.9 → 0.3.79-alpha.coderabbitai-chat-dniAA6r9GseKm4ouqNyJ8dl.1

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.
Files changed (70) hide show
  1. package/dist/AlternateGrid/javascript.4.svelte +227 -0
  2. package/dist/AlternateGrid/javascript.svelte +6 -5
  3. package/dist/AlternateGrid/typescript.4.svelte +224 -0
  4. package/dist/AlternateGrid/typescript.svelte +9 -4
  5. package/dist/CallToAction/javascript.4.svelte +127 -0
  6. package/dist/CallToAction/javascript.svelte +6 -5
  7. package/dist/CallToAction/typescript.4.svelte +124 -0
  8. package/dist/CallToAction/typescript.svelte +4 -2
  9. package/dist/CustomerLogos/javascript.4.svelte +124 -0
  10. package/dist/CustomerLogos/javascript.svelte +6 -5
  11. package/dist/CustomerLogos/typescript.4.svelte +121 -0
  12. package/dist/CustomerLogos/typescript.svelte +4 -2
  13. package/dist/Hero/javascript.4.svelte +190 -0
  14. package/dist/Hero/javascript.svelte +5 -4
  15. package/dist/Hero/typescript.4.svelte +187 -0
  16. package/dist/Hero/typescript.svelte +4 -2
  17. package/dist/hooks/documentation-read.cjs +14 -51
  18. package/dist/hooks/documentation-read.cjs.map +1 -1
  19. package/dist/hooks/documentation-read.js +14 -51
  20. package/dist/hooks/documentation-read.js.map +1 -1
  21. package/dist/hooks/documentation-read.templates.cjs +114 -0
  22. package/dist/hooks/documentation-read.templates.cjs.map +1 -0
  23. package/dist/hooks/documentation-read.templates.d.ts +9 -0
  24. package/dist/hooks/documentation-read.templates.js +114 -0
  25. package/dist/hooks/documentation-read.templates.js.map +1 -0
  26. package/dist/hooks/project-init.cjs +44 -143
  27. package/dist/hooks/project-init.cjs.map +1 -1
  28. package/dist/hooks/project-init.js +44 -143
  29. package/dist/hooks/project-init.js.map +1 -1
  30. package/dist/hooks/project-init.templates.cjs +181 -0
  31. package/dist/hooks/project-init.templates.cjs.map +1 -0
  32. package/dist/hooks/project-init.templates.d.ts +12 -0
  33. package/dist/hooks/project-init.templates.js +181 -0
  34. package/dist/hooks/project-init.templates.js.map +1 -0
  35. package/dist/hooks/slice-create.cjs +5 -52
  36. package/dist/hooks/slice-create.cjs.map +1 -1
  37. package/dist/hooks/slice-create.js +5 -52
  38. package/dist/hooks/slice-create.js.map +1 -1
  39. package/dist/hooks/slice-create.templates.cjs +61 -0
  40. package/dist/hooks/slice-create.templates.cjs.map +1 -0
  41. package/dist/hooks/slice-create.templates.d.ts +6 -0
  42. package/dist/hooks/slice-create.templates.js +61 -0
  43. package/dist/hooks/slice-create.templates.js.map +1 -0
  44. package/dist/lib/getSvelteMajor.cjs +38 -0
  45. package/dist/lib/getSvelteMajor.cjs.map +1 -0
  46. package/dist/lib/getSvelteMajor.d.ts +1 -0
  47. package/dist/lib/getSvelteMajor.js +16 -0
  48. package/dist/lib/getSvelteMajor.js.map +1 -0
  49. package/dist/lib/requireResolve.cjs +16 -0
  50. package/dist/lib/requireResolve.cjs.map +1 -0
  51. package/dist/lib/requireResolve.d.ts +10 -0
  52. package/dist/lib/requireResolve.js +16 -0
  53. package/dist/lib/requireResolve.js.map +1 -0
  54. package/dist/plugin.cjs +4 -2
  55. package/dist/plugin.cjs.map +1 -1
  56. package/dist/plugin.js +4 -2
  57. package/dist/plugin.js.map +1 -1
  58. package/dist/simulator/SliceSimulator.svelte +3 -2
  59. package/dist/simulator/SliceSimulator.svelte.d.ts +7 -19
  60. package/package.json +9 -8
  61. package/src/hooks/documentation-read.templates.ts +170 -0
  62. package/src/hooks/documentation-read.ts +21 -59
  63. package/src/hooks/project-init.templates.ts +208 -0
  64. package/src/hooks/project-init.ts +57 -147
  65. package/src/hooks/slice-create.templates.ts +103 -0
  66. package/src/hooks/slice-create.ts +7 -66
  67. package/src/lib/getSvelteMajor.ts +23 -0
  68. package/src/lib/requireResolve.ts +30 -0
  69. package/src/plugin.ts +5 -2
  70. package/src/simulator/SliceSimulator.svelte +3 -2
@@ -3,8 +3,14 @@ import { source } from "common-tags";
3
3
  import type { DocumentationReadHook } from "@slicemachine/plugin-kit";
4
4
 
5
5
  import { getJSFileExtension } from "../lib/getJSFileExtension";
6
+ import { checkIsTypeScriptProject } from "../lib/checkIsTypeScriptProject";
6
7
 
7
8
  import type { PluginOptions } from "../types";
9
+ import {
10
+ componentFileTemplate,
11
+ dataFileTemplate,
12
+ } from "./documentation-read.templates";
13
+ import { getSvelteMajor } from "../lib/getSvelteMajor";
8
14
 
9
15
  const nestRouteFilePath = (filePath: string, nesting: string): string => {
10
16
  return [
@@ -22,68 +28,17 @@ export const documentationRead: DocumentationReadHook<PluginOptions> = async (
22
28
  const { model } = data.data;
23
29
 
24
30
  const pageDataExtension = await getJSFileExtension({ helpers, options });
31
+ const pageDataLanguage =
32
+ pageDataExtension === "ts" ? "typescript" : "javascript";
33
+ const typescript = await checkIsTypeScriptProject({ options, helpers });
25
34
 
26
- const routePath = `src/routes/${model.repeatable ? "[uid]" : model.id}`;
35
+ const routePath = `src/routes/[[preview=preview]]/${
36
+ model.repeatable ? "[uid]" : model.id
37
+ }`;
27
38
  const dataFilePath = `${routePath}/+page.server.${pageDataExtension}`;
28
39
  const componentFilePath = `${routePath}/+page.svelte`;
29
40
 
30
- let dataFileContent: string;
31
- if (model.repeatable) {
32
- dataFileContent = source`
33
- import { createClient } from "$lib/prismicio";
34
-
35
- export async function load({ params, fetch, cookies }) {
36
- const client = createClient({ fetch, cookies });
37
-
38
- const page = await client.getByUID("${model.id}", params.uid);
39
-
40
- return {
41
- page,
42
- };
43
- }
44
-
45
- export async function entries() {
46
- const client = createClient();
47
-
48
- const pages = await client.getAllByType("${model.id}");
49
-
50
- return pages.map((page) => {
51
- return { uid: page.uid };
52
- });
53
- }
54
- `;
55
- } else {
56
- dataFileContent = source`
57
- import { createClient } from "$lib/prismicio";
58
-
59
- export async function load({ params, fetch, cookies }) {
60
- const client = createClient({ fetch, cookies });
61
-
62
- const page = await client.getSingle("${model.id}");
63
-
64
- return {
65
- page,
66
- };
67
- }
68
-
69
- export async function entries() {
70
- return [{}]
71
- }
72
- `;
73
- }
74
-
75
- let componentFileContent = source`
76
- <script>
77
- import { SliceZone } from "@prismicio/svelte";
78
-
79
- import { components } from "$lib/slices";
80
-
81
- export let data;
82
- </script>
83
-
84
- <SliceZone slices={data.page.data.slices} {components} />
85
- `;
86
-
41
+ let dataFileContent = dataFileTemplate({ model, typescript });
87
42
  if (options.format) {
88
43
  dataFileContent = await helpers.format(
89
44
  dataFileContent,
@@ -92,6 +47,13 @@ export const documentationRead: DocumentationReadHook<PluginOptions> = async (
92
47
  includeNewlineAtEnd: false,
93
48
  },
94
49
  );
50
+ }
51
+
52
+ let componentFileContent = componentFileTemplate({
53
+ typescript,
54
+ version: await getSvelteMajor(),
55
+ });
56
+ if (options.format) {
95
57
  componentFileContent = await helpers.format(
96
58
  componentFileContent,
97
59
  helpers.joinPathFromRoot(componentFilePath),
@@ -121,7 +83,7 @@ export const documentationRead: DocumentationReadHook<PluginOptions> = async (
121
83
 
122
84
  Paste in this code:
123
85
 
124
- ${`~~~${pageDataExtension} [${dataFilePath}]\n${dataFileContent}\n~~~`}
86
+ ${`~~~${pageDataLanguage} [${dataFilePath}]\n${dataFileContent}\n~~~`}
125
87
 
126
88
  ## Create your ${model.label}'s page component
127
89
 
@@ -0,0 +1,208 @@
1
+ import { source as svelte, source as ts, source as js } from "common-tags";
2
+
3
+ import { PRISMIC_ENVIRONMENT_ENVIRONMENT_VARIABLE_NAME } from "../constants";
4
+
5
+ export function prismicIOFileTemplate(args: { typescript: boolean }): string {
6
+ const { typescript } = args;
7
+
8
+ const TS = ts`
9
+ import { createClient as baseCreateClient, type Route } from "@prismicio/client";
10
+ import { type CreateClientConfig, enableAutoPreviews } from '@prismicio/svelte/kit';
11
+ import sm from "../../slicemachine.config.json";
12
+
13
+ /**
14
+ * The project's Prismic repository name.
15
+ */
16
+ export const repositoryName =
17
+ import${"."}meta${"."}env.${PRISMIC_ENVIRONMENT_ENVIRONMENT_VARIABLE_NAME} || sm.repositoryName;
18
+
19
+ /**
20
+ * A list of Route Resolver objects that define how a document's \`url\` field is resolved.
21
+ *
22
+ * {@link https://prismic.io/docs/route-resolver}
23
+ */
24
+ // TODO: Update the routes array to match your project's route structure.
25
+ const routes: Route[] = [
26
+ // Examples:
27
+ // { type: "homepage", path: "/" },
28
+ // { type: "page", path: "/:uid" },
29
+ ];
30
+
31
+ /**
32
+ * Creates a Prismic client for the project's repository. The client is used to
33
+ * query content from the Prismic API.
34
+ *
35
+ * @param config - Configuration for the Prismic client.
36
+ */
37
+ export const createClient = ({ cookies, ...config }: CreateClientConfig = {}) => {
38
+ const client = baseCreateClient(repositoryName, {
39
+ routes,
40
+ ...config,
41
+ });
42
+
43
+ enableAutoPreviews({ client, cookies });
44
+
45
+ return client;
46
+ };
47
+ `;
48
+
49
+ const JS = js`
50
+ import { createClient as baseCreateClient } from "@prismicio/client";
51
+ import { enableAutoPreviews } from '@prismicio/svelte/kit';
52
+ import sm from "../../slicemachine.config.json";
53
+
54
+ /**
55
+ * The project's Prismic repository name.
56
+ */
57
+ export const repositoryName =
58
+ import${"."}meta${"."}env.${PRISMIC_ENVIRONMENT_ENVIRONMENT_VARIABLE_NAME} || sm.repositoryName;
59
+
60
+ /**
61
+ * A list of Route Resolver objects that define how a document's \`url\` field is resolved.
62
+ *
63
+ * {@link https://prismic.io/docs/route-resolver#route-resolver}
64
+ *
65
+ * @type {import("@prismicio/client").Route[]}
66
+ */
67
+ // TODO: Update the routes array to match your project's route structure.
68
+ const routes = [
69
+ // Examples:
70
+ // { type: "homepage", path: "/" },
71
+ // { type: "page", path: "/:uid" },
72
+ ];
73
+
74
+ /**
75
+ * Creates a Prismic client for the project's repository. The client is used to
76
+ * query content from the Prismic API.
77
+ *
78
+ * @param {import('@prismicio/svelte/kit').CreateClientConfig} config - Configuration for the Prismic client.
79
+ */
80
+ export const createClient = ({ cookies, ...config } = {}) => {
81
+ const client = prismic.createClient(repositoryName, {
82
+ routes,
83
+ ...config,
84
+ });
85
+
86
+ enableAutoPreviews({ client, cookies });
87
+
88
+ return client;
89
+ };
90
+ `;
91
+
92
+ return typescript ? TS : JS;
93
+ }
94
+
95
+ export function sliceSimulatorPageTemplate(args: { version: number }): string {
96
+ const { version } = args;
97
+
98
+ const v5 = svelte`
99
+ <script>
100
+ import { SliceSimulator } from '@slicemachine/adapter-sveltekit/simulator';
101
+ import { SliceZone } from '@prismicio/svelte';
102
+ import { components } from '$lib/slices';
103
+ </script>
104
+
105
+ <!-- Slot syntax is used for backward compatibility with Svelte <=4. -->
106
+ <SliceSimulator let:slices>
107
+ <SliceZone {slices} {components} />
108
+ </SliceSimulator>
109
+ `;
110
+
111
+ const v4 = svelte`
112
+ <script>
113
+ import { SliceSimulator } from '@slicemachine/adapter-sveltekit/simulator';
114
+ import { SliceZone } from '@prismicio/svelte';
115
+ import { components } from '$lib/slices';
116
+ </script>
117
+
118
+ <SliceSimulator let:slices>
119
+ <SliceZone {slices} {components} />
120
+ </SliceSimulator>
121
+ `;
122
+
123
+ return version <= 4 ? v4 : v5;
124
+ }
125
+
126
+ export function previewAPIRouteTemplate(args: { typescript: boolean }): string {
127
+ const { typescript } = args;
128
+
129
+ const TS = ts`
130
+ import { redirectToPreviewURL } from '@prismicio/svelte/kit';
131
+ import { createClient } from '$lib/prismicio';
132
+ import type { RequestHandler } from "./$types";
133
+
134
+ export const GET: RequestHandler = async ({ fetch, request, cookies }) => {
135
+ const client = createClient({ fetch });
136
+
137
+ return await redirectToPreviewURL({ client, request, cookies });
138
+ }
139
+ `;
140
+
141
+ const JS = js`
142
+ import { redirectToPreviewURL } from '@prismicio/svelte/kit';
143
+ import { createClient } from '$lib/prismicio';
144
+
145
+ /* @type {import("./$types").RequestHandler} */
146
+ export async function GET({ fetch, request, cookies }) {
147
+ const client = createClient({ fetch });
148
+
149
+ return await redirectToPreviewURL({ client, request, cookies });
150
+ }
151
+ `;
152
+
153
+ return typescript ? TS : JS;
154
+ }
155
+
156
+ export function rootLayoutTemplate(args: { version: number }): string {
157
+ const { version } = args;
158
+
159
+ const v5 = svelte`
160
+ <script>
161
+ import { isFilled, asImageSrc } from '@prismicio/client';
162
+ import { PrismicPreview } from '@prismicio/svelte/kit';
163
+ import { page } from '$app/state';
164
+ import { repositoryName } from '$lib/prismicio';
165
+
166
+ const { children } = $props();
167
+ </script>
168
+
169
+ <svelte:head>
170
+ <title>{page.data.page?.data.meta_title}</title>
171
+ <meta property="og:title" content={page.data.page?.data.meta_title} />
172
+ {#if isFilled.keyText(page.data.page?.data.meta_description)}
173
+ <meta name="description" content={page.data.page.data.meta_description} />
174
+ <meta property="og:description" content={page.data.page.data.meta_description} />
175
+ {/if}
176
+ {#if isFilled.image(page.data.page?.data.meta_image)}
177
+ <meta property="og:image" content={asImageSrc(page.data.page.data.meta_image)} />
178
+ {/if}
179
+ </svelte:head>
180
+ {@render children()}
181
+ <PrismicPreview {repositoryName} />
182
+ `;
183
+
184
+ const v4 = svelte`
185
+ <script>
186
+ import { isFilled, asImageSrc } from '@prismicio/client';
187
+ import { PrismicPreview } from '@prismicio/svelte/kit';
188
+ import { page } from '$app/state';
189
+ import { repositoryName } from '$lib/prismicio';
190
+ </script>
191
+
192
+ <svelte:head>
193
+ <title>{page.data.page?.data.meta_title}</title>
194
+ <meta property="og:title" content={page.data.page?.data.meta_title} />
195
+ {#if isFilled.keyText(page.data.page?.data.meta_description)}
196
+ <meta name="description" content={page.data.page.data.meta_description} />
197
+ <meta property="og:description" content={page.data.page.data.meta_description} />
198
+ {/if}
199
+ {#if isFilled.image(page.data.page?.data.meta_image)}
200
+ <meta property="og:image" content={asImageSrc(page.data.page.data.meta_image)} />
201
+ {/if}
202
+ </svelte:head>
203
+ <slot />
204
+ <PrismicPreview {repositoryName} />
205
+ `;
206
+
207
+ return version <= 4 ? v4 : v5;
208
+ }
@@ -9,14 +9,21 @@ import {
9
9
  writeProjectFile,
10
10
  } from "@slicemachine/plugin-kit/fs";
11
11
  import { source } from "common-tags";
12
+ import { loadFile } from "magicast";
12
13
 
13
14
  import { checkIsTypeScriptProject } from "../lib/checkIsTypeScriptProject";
14
15
  import { getJSFileExtension } from "../lib/getJSFileExtension";
16
+ import { getSvelteMajor } from "../lib/getSvelteMajor";
15
17
  import { rejectIfNecessary } from "../lib/rejectIfNecessary";
16
18
  import { upsertSliceLibraryIndexFile } from "../lib/upsertSliceLibraryIndexFile";
17
19
 
18
20
  import type { PluginOptions } from "../types";
19
- import { PRISMIC_ENVIRONMENT_ENVIRONMENT_VARIABLE_NAME } from "../constants";
21
+ import {
22
+ previewAPIRouteTemplate,
23
+ prismicIOFileTemplate,
24
+ rootLayoutTemplate,
25
+ sliceSimulatorPageTemplate,
26
+ } from "./project-init.templates";
20
27
 
21
28
  type InstallDependenciesArgs = {
22
29
  installDependencies: ProjectInitHookData["installDependencies"];
@@ -39,10 +46,6 @@ const createPrismicIOFile = async ({
39
46
  helpers,
40
47
  options,
41
48
  }: CreatePrismicIOFileArgs) => {
42
- const isTypeScriptProject = await checkIsTypeScriptProject({
43
- helpers,
44
- options,
45
- });
46
49
  const extension = await getJSFileExtension({ helpers, options });
47
50
  const filename = path.join(`src/lib/prismicio.${extension}`);
48
51
 
@@ -50,105 +53,8 @@ const createPrismicIOFile = async ({
50
53
  return;
51
54
  }
52
55
 
53
- let contents: string;
54
-
55
- if (isTypeScriptProject) {
56
- contents = source`
57
- import * as prismic from "@prismicio/client";
58
- import { type CreateClientConfig, enableAutoPreviews } from '@prismicio/svelte/kit';
59
- import config from "../../slicemachine.config.json";
60
-
61
- /**
62
- * The project's Prismic repository name.
63
- */
64
- export const repositoryName =
65
- import${"."}meta${"."}env.${PRISMIC_ENVIRONMENT_ENVIRONMENT_VARIABLE_NAME} || config.repositoryName;
66
-
67
- /**
68
- * A list of Route Resolver objects that define how a document's \`url\` field is resolved.
69
- *
70
- * {@link https://prismic.io/docs/route-resolver#route-resolver}
71
- */
72
- // TODO: Update the routes array to match your project's route structure.
73
- const routes: prismic.ClientConfig["routes"] = [
74
- // Examples:
75
- // {
76
- // type: "homepage",
77
- // path: "/",
78
- // },
79
- // {
80
- // type: "page",
81
- // path: "/:uid",
82
- // },
83
- ];
84
-
85
- /**
86
- * Creates a Prismic client for the project's repository. The client is used to
87
- * query content from the Prismic API.
88
- *
89
- * @param config - Configuration for the Prismic client.
90
- */
91
- export const createClient = ({ cookies, ...config }: CreateClientConfig = {}) => {
92
- const client = prismic.createClient(repositoryName, {
93
- routes,
94
- ...config,
95
- });
96
-
97
- enableAutoPreviews({ client, cookies });
98
-
99
- return client;
100
- };
101
- `;
102
- } else {
103
- contents = source`
104
- import * as prismic from "@prismicio/client";
105
- import { enableAutoPreviews } from '@prismicio/svelte/kit';
106
- import config from "../../slicemachine.config.json";
107
-
108
- /**
109
- * The project's Prismic repository name.
110
- */
111
- export const repositoryName =
112
- import${"."}meta${"."}env.${PRISMIC_ENVIRONMENT_ENVIRONMENT_VARIABLE_NAME} || config.repositoryName;
113
-
114
- /**
115
- * A list of Route Resolver objects that define how a document's \`url\` field is resolved.
116
- *
117
- * {@link https://prismic.io/docs/route-resolver#route-resolver}
118
- *
119
- * @type {prismic.ClientConfig["routes"]}
120
- */
121
- // TODO: Update the routes array to match your project's route structure.
122
- const routes = [
123
- // Examples:
124
- // {
125
- // type: "homepage",
126
- // path: "/",
127
- // },
128
- // {
129
- // type: "page",
130
- // path: "/:uid",
131
- // },
132
- ];
133
-
134
- /**
135
- * Creates a Prismic client for the project's repository. The client is used to
136
- * query content from the Prismic API.
137
- *
138
- * @param {import('@prismicio/svelte/kit').CreateClientConfig} config - Configuration for the Prismic client.
139
- */
140
- export const createClient = ({ cookies, ...config } = {}) => {
141
- const client = prismic.createClient(repositoryName, {
142
- routes,
143
- ...config,
144
- });
145
-
146
- enableAutoPreviews({ client, cookies });
147
-
148
- return client;
149
- };
150
- `;
151
- }
56
+ const typescript = await checkIsTypeScriptProject({ helpers, options });
57
+ const contents = prismicIOFileTemplate({ typescript });
152
58
 
153
59
  await writeProjectFile({
154
60
  filename,
@@ -173,17 +79,9 @@ const createSliceSimulatorPage = async ({
173
79
  return;
174
80
  }
175
81
 
176
- const contents = source`
177
- <script>
178
- import { SliceSimulator } from '@slicemachine/adapter-sveltekit/simulator';
179
- import { SliceZone } from '@prismicio/svelte';
180
- import { components } from '$lib/slices';
181
- </script>
182
-
183
- <SliceSimulator let:slices>
184
- <SliceZone {slices} {components} />
185
- </SliceSimulator>
186
- `;
82
+ const contents = sliceSimulatorPageTemplate({
83
+ version: await getSvelteMajor(),
84
+ });
187
85
 
188
86
  await writeProjectFile({
189
87
  filename,
@@ -241,16 +139,8 @@ const createPreviewAPIRoute = async ({
241
139
  return;
242
140
  }
243
141
 
244
- const contents = source`
245
- import { redirectToPreviewURL } from '@prismicio/svelte/kit';
246
- import { createClient } from '$lib/prismicio';
247
-
248
- export async function GET({ fetch, request, cookies }) {
249
- const client = createClient({ fetch });
250
-
251
- return await redirectToPreviewURL({ client, request, cookies });
252
- }
253
- `;
142
+ const typescript = await checkIsTypeScriptProject({ helpers, options });
143
+ const contents = previewAPIRouteTemplate({ typescript });
254
144
 
255
145
  await writeProjectFile({
256
146
  filename,
@@ -327,28 +217,7 @@ const createRootLayoutFile = async ({
327
217
  return;
328
218
  }
329
219
 
330
- const contents = source`
331
- <script>
332
- import { isFilled, asImageSrc } from '@prismicio/client';
333
- import { PrismicPreview } from '@prismicio/svelte/kit';
334
- import { page } from '$app/state';
335
- import { repositoryName } from '$lib/prismicio';
336
- </script>
337
-
338
- <svelte:head>
339
- <title>{page.data.page?.data.meta_title}</title>
340
- <meta property="og:title" content={page.data.page?.data.meta_title} />
341
- {#if isFilled.keyText(page.data.page?.data.meta_description)}
342
- <meta name="description" content={page.data.page.data.meta_description} />
343
- <meta property="og:description" content={page.data.page.data.meta_description} />
344
- {/if}
345
- {#if isFilled.image(page.data.page?.data.meta_image)}
346
- <meta property="og:image" content={asImageSrc(page.data.page.data.meta_image)} />
347
- {/if}
348
- </svelte:head>
349
- <slot />
350
- <PrismicPreview {repositoryName} />
351
- `;
220
+ const contents = rootLayoutTemplate({ version: await getSvelteMajor() });
352
221
 
353
222
  await writeProjectFile({
354
223
  filename,
@@ -421,6 +290,46 @@ const upsertSliceLibraryIndexFiles = async (
421
290
  );
422
291
  };
423
292
 
293
+ const modifyViteConfig = async ({
294
+ helpers,
295
+ options,
296
+ }: SliceMachineContext<PluginOptions>) => {
297
+ let filename = "vite.config.js";
298
+ if (!(await checkHasProjectFile({ filename, helpers }))) {
299
+ filename = "vite.config.ts";
300
+ }
301
+ if (!(await checkHasProjectFile({ filename, helpers }))) {
302
+ // Couldn't find the config file.
303
+ return;
304
+ }
305
+ const filepath = helpers.joinPathFromRoot(filename);
306
+
307
+ const mod = await loadFile(filepath);
308
+ if (mod.exports.default.$type !== "function-call") {
309
+ // Invalid config file.
310
+ return;
311
+ }
312
+
313
+ // Add `./slicemachine.config.json` to allowed files.
314
+ const config = mod.exports.default.$args[0];
315
+ config.server ??= {};
316
+ config.server.fs ??= {};
317
+ config.server.fs.allow ??= [];
318
+ if (!config.server.fs.allow.includes("./slicemachine.config.json")) {
319
+ config.server.fs.allow.push("./slicemachine.config.json");
320
+ }
321
+
322
+ // Remove an empty line above the `server` property.
323
+ const contents = mod.generate().code.replace(/\n\s*\n(?=\s*server:)/, "\n");
324
+
325
+ await writeProjectFile({
326
+ filename,
327
+ contents,
328
+ format: options.format,
329
+ helpers,
330
+ });
331
+ };
332
+
424
333
  export const projectInit: ProjectInitHook<PluginOptions> = async (
425
334
  { installDependencies: _installDependencies },
426
335
  context,
@@ -436,6 +345,7 @@ export const projectInit: ProjectInitHook<PluginOptions> = async (
436
345
  createPreviewRouteMatcherFile(context),
437
346
  createRootLayoutServerFile(context),
438
347
  createRootLayoutFile(context),
348
+ modifyViteConfig(context),
439
349
  ]),
440
350
  );
441
351
 
@@ -0,0 +1,103 @@
1
+ import { SharedSlice } from "@prismicio/types-internal/lib/customtypes";
2
+ import { source as svelte } from "common-tags";
3
+
4
+ import { pascalCase } from "../lib/pascalCase";
5
+
6
+ const PLACEHOLDER = `
7
+ Placeholder component for {slice.slice_type} (variation: {slice.variation}) slices.
8
+ <br />
9
+ <strong>You can edit this slice directly in your code editor.</strong>
10
+ <!--
11
+ 💡 Use Prismic MCP with your code editor
12
+
13
+ Get AI-powered help to build your slice components — based on your actual model.
14
+
15
+ ▶️ Setup:
16
+ 1. Add a new MCP Server in your code editor:
17
+
18
+ {
19
+ "mcpServers": {
20
+ "Prismic MCP": {
21
+ "command": "npx",
22
+ "args": ["-y", "@prismicio/mcp-server"]
23
+ }
24
+ }
25
+ }
26
+
27
+ 2. Select a model optimized for coding (e.g. Claude 3.7 Sonnet or similar)
28
+
29
+ ✅ Then open your slice file and ask your code editor:
30
+ "Code this slice"
31
+
32
+ Your code editor reads your slice model and helps you code faster ⚡
33
+ 📚 Give your feedback: https://community.prismic.io/t/help-us-shape-the-future-of-slice-creation/19505
34
+ -->
35
+ `;
36
+
37
+ export function sliceTemplate(args: {
38
+ model: SharedSlice;
39
+ typescript: boolean;
40
+ version: number;
41
+ }): string {
42
+ const { model, typescript, version } = args;
43
+
44
+ const pascalName = pascalCase(model.name);
45
+
46
+ const v5TS = svelte`
47
+ <script lang="ts">
48
+ import type { Content } from '@prismicio/client';
49
+ import type { SliceComponentProps } from '@prismicio/svelte';
50
+
51
+ type Props = SliceComponentProps<Content.${pascalName}Slice>;
52
+
53
+ const { slice }: Props = $props();
54
+ </script>
55
+
56
+ <section data-slice-type={slice.slice_type} data-slice-variation={slice.variation}>
57
+ ${PLACEHOLDER}
58
+ </section>
59
+ `;
60
+
61
+ const v5JS = svelte`
62
+ <script>
63
+ /* @typedef {import("@prismicio/client").Content} Content */
64
+ /* @typedef {import("@prismicio/svelte").SliceComponentProps} SliceComponentProps */
65
+
66
+ /* @type {SliceComponentProps<Content.${pascalName}Slice>} */
67
+ const { slice } = $props();
68
+ </script>
69
+
70
+ <section data-slice-type={slice.slice_type} data-slice-variation={slice.variation}>
71
+ ${PLACEHOLDER}
72
+ </section>
73
+ `;
74
+
75
+ const v4TS = svelte`
76
+ <script lang="ts">
77
+ import type { Content } from '@prismicio/client';
78
+
79
+ export let slice: Content.${pascalName}Slice;
80
+ </script>
81
+
82
+ <section data-slice-type={slice.slice_type} data-slice-variation={slice.variation}>
83
+ ${PLACEHOLDER}
84
+ </section>
85
+ `;
86
+
87
+ const v4JS = svelte`
88
+ <script>
89
+ /** @type {import("@prismicio/client").Content.${pascalName}Slice} */
90
+ export let slice;
91
+ </script>
92
+
93
+ <section data-slice-type={slice.slice_type} data-slice-variation={slice.variation}>
94
+ ${PLACEHOLDER}
95
+ </section>
96
+ `;
97
+
98
+ if (typescript) {
99
+ return version <= 4 ? v4TS : v5TS;
100
+ }
101
+
102
+ return version <= 4 ? v4JS : v5JS;
103
+ }