Skip to content

Commit a07a0d2

Browse files
authored
Merge pull request storybookjs#35132 from storybookjs/kasper/fix-vite-config-plugins-injection
Build: Anchor vite config injection on the plugins key in sandbox-parts
2 parents 8d02690 + 13d2691 commit a07a0d2

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

scripts/tasks/sandbox-parts.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -587,17 +587,20 @@ export async function setupVitest(details: TemplateDetails, options: PassedOptio
587587

588588
let fileContent = await readFile(join(sandboxDir, configFile), 'utf-8');
589589

590-
// Insert resolve: { preserveSymlinks: true } and optionally server.fs.allow as siblings to plugins
591-
// Handles both defineConfig({ ... }) and defineWorkspace([ ... , { ... }])
592-
fileContent = fileContent.replace(/(plugins\s*:\s*\[[^\]]*\],?)/, (match) => {
593-
let replacement = `${match}\n resolve: {\n preserveSymlinks: true\n },`;
590+
// Insert resolve: { preserveSymlinks: true } and optionally server.fs.allow as siblings to
591+
// plugins. Handles both defineConfig({ ... }) and defineWorkspace([ ... , { ... }]). Anchored
592+
// on the `plugins:` key (injecting before it) instead of matching the whole array: plugin code
593+
// may contain `]` (e.g. the regex literal in the sveltekit template), which a bracket-counting
594+
// regex like `\[[^\]]*\]` would cut short, splicing the injection into the middle of it.
595+
fileContent = fileContent.replace(/^([ \t]*)plugins\s*:/m, (match, indent) => {
596+
let injected = `${indent}resolve: {\n${indent} preserveSymlinks: true\n${indent}},\n`;
594597

595598
// In linked mode, also add server.fs.allow to allow Vite to serve files from the monorepo root
596599
if (options.link) {
597-
replacement += `\n server: {\n fs: {\n allow: ['../../..']\n }\n },`;
600+
injected += `${indent}server: {\n${indent} fs: {\n${indent} allow: ['../../..']\n${indent} }\n${indent}},\n`;
598601
}
599602

600-
return replacement;
603+
return `${injected}${match}`;
601604
});
602605

603606
// search for storybookTest({...}) and place `tags: 'vitest'` into it but tags option doesn't exist yet in the config. Also consider multi line

0 commit comments

Comments
 (0)