Skip to content

Commit 29c19e0

Browse files
committed
fix: add slicemachine.config.json to vite.config.js
1 parent 2829f0d commit 29c19e0

5 files changed

Lines changed: 107 additions & 11 deletions

File tree

packages/adapter-sveltekit/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"fp-ts": "^2.13.1",
7070
"io-ts": "^2.2.20",
7171
"io-ts-types": "^0.5.19",
72+
"magicast": "0.3.5",
7273
"monocle-ts": "^2.3.13",
7374
"newtype-ts": "^0.3.5",
7475
"pascal-case": "^3.1.2"

packages/adapter-sveltekit/src/hooks/project-init.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
writeProjectFile,
1010
} from "@slicemachine/plugin-kit/fs";
1111
import { source } from "common-tags";
12+
import { loadFile } from "magicast";
1213

1314
import { checkIsTypeScriptProject } from "../lib/checkIsTypeScriptProject";
1415
import { getJSFileExtension } from "../lib/getJSFileExtension";
@@ -289,6 +290,46 @@ const upsertSliceLibraryIndexFiles = async (
289290
);
290291
};
291292

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+
292333
export const projectInit: ProjectInitHook<PluginOptions> = async (
293334
{ installDependencies: _installDependencies },
294335
context,
@@ -304,6 +345,7 @@ export const projectInit: ProjectInitHook<PluginOptions> = async (
304345
createPreviewRouteMatcherFile(context),
305346
createRootLayoutServerFile(context),
306347
createRootLayoutFile(context),
348+
modifyViteConfig(context),
307349
]),
308350
);
309351

packages/adapter-sveltekit/test/__setup__.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ beforeEach(async (ctx) => {
4848
JSON.stringify(config),
4949
);
5050

51+
await fs.writeFile(
52+
path.join(tmpRoot, "vite.config.js"),
53+
`
54+
import { sveltekit } from "@sveltejs/kit/vite";
55+
import { defineConfig } from "vite";
56+
57+
export default defineConfig({
58+
plugins: [sveltekit()],
59+
});
60+
`.trim(),
61+
);
62+
5163
ctx.project = {
5264
root: tmpRoot,
5365
config,

packages/adapter-sveltekit/test/plugin-project-init.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,46 @@ describe("modify slicemachine.config.json", () => {
169169
});
170170
});
171171

172+
describe("modify vite.config.js file", () => {
173+
it("adds ./slicemachine.config.json to server.fs.allow", async (ctx) => {
174+
const log = vi.fn();
175+
const installDependencies = vi.fn();
176+
177+
const configPath = path.resolve(ctx.project.root, "./vite.config.js");
178+
179+
const preContents = await fs.readFile(configPath, "utf8");
180+
expect(preContents).toMatchInlineSnapshot(`
181+
"import { sveltekit } from \\"@sveltejs/kit/vite\\";
182+
import { defineConfig } from \\"vite\\";
183+
184+
export default defineConfig({
185+
plugins: [sveltekit()],
186+
});"
187+
`);
188+
189+
await ctx.pluginRunner.callHook("project:init", {
190+
log,
191+
installDependencies,
192+
});
193+
194+
const postContents = await fs.readFile(configPath, "utf8");
195+
expect(postContents).toMatchInlineSnapshot(`
196+
"import { sveltekit } from \\"@sveltejs/kit/vite\\";
197+
import { defineConfig } from \\"vite\\";
198+
199+
export default defineConfig({
200+
plugins: [sveltekit()],
201+
server: {
202+
fs: {
203+
allow: [\\"./slicemachine.config.json\\"],
204+
},
205+
},
206+
});
207+
"
208+
`);
209+
});
210+
});
211+
172212
describe("prismicio.js file", () => {
173213
it("does not overwrite prismicio file if it already exists", async (ctx) => {
174214
const log = vi.fn();

yarn.lock

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9821,6 +9821,7 @@ __metadata:
98219821
fp-ts: ^2.13.1
98229822
io-ts: ^2.2.20
98239823
io-ts-types: ^0.5.19
9824+
magicast: 0.3.5
98249825
monocle-ts: ^2.3.13
98259826
newtype-ts: ^0.3.5
98269827
pascal-case: ^3.1.2
@@ -24685,6 +24686,17 @@ __metadata:
2468524686
languageName: node
2468624687
linkType: hard
2468724688

24689+
"magicast@npm:0.3.5, magicast@npm:^0.3.5":
24690+
version: 0.3.5
24691+
resolution: "magicast@npm:0.3.5"
24692+
dependencies:
24693+
"@babel/parser": ^7.25.4
24694+
"@babel/types": ^7.25.4
24695+
source-map-js: ^1.2.0
24696+
checksum: 668f07ade907a44bccfc9a9321588473f6d5fa25329aa26b9ad9a3bf87cc2e6f9c482cbdd3e33c0b9ab9b79c065630c599cc055a12f881c8c924ee0d7282cdce
24697+
languageName: node
24698+
linkType: hard
24699+
2468824700
"magicast@npm:^0.3.4":
2468924701
version: 0.3.4
2469024702
resolution: "magicast@npm:0.3.4"
@@ -24696,17 +24708,6 @@ __metadata:
2469624708
languageName: node
2469724709
linkType: hard
2469824710

24699-
"magicast@npm:^0.3.5":
24700-
version: 0.3.5
24701-
resolution: "magicast@npm:0.3.5"
24702-
dependencies:
24703-
"@babel/parser": ^7.25.4
24704-
"@babel/types": ^7.25.4
24705-
source-map-js: ^1.2.0
24706-
checksum: 668f07ade907a44bccfc9a9321588473f6d5fa25329aa26b9ad9a3bf87cc2e6f9c482cbdd3e33c0b9ab9b79c065630c599cc055a12f881c8c924ee0d7282cdce
24707-
languageName: node
24708-
linkType: hard
24709-
2471024711
"make-dir@npm:^1.0.0":
2471124712
version: 1.3.0
2471224713
resolution: "make-dir@npm:1.3.0"

0 commit comments

Comments
 (0)