Skip to content

Commit c8e260d

Browse files
fix: prevent accidental pollution of svelteconfig.extensions (#1171)
1 parent 03e2b76 commit c8e260d

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

.changeset/great-corners-spend.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
fix: prevent accidental pollution of `svelteconfig.extensions`

packages/e2e-tests/_test_dependencies/vite-plugins/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ export function writeResolvedConfig() {
5757
if (value instanceof RegExp) return value.toString();
5858
else return value;
5959
}
60+
61+
// Handle config from svelte.config.js through vite-plugin-svelte
62+
const vps = config.plugins.find((p) => p.name === 'vite-plugin-svelte');
63+
const configFileOptions = vps?.api?.options;
64+
6065
const serializableConfig = {
6166
...config,
6267
plugins: config.plugins.map((p) => p.name)
@@ -66,7 +71,31 @@ export function writeResolvedConfig() {
6671
fs.mkdirSync(dir);
6772
}
6873
const filename = path.join(dir, `vite.config.${cmd}${config.build.ssr ? '.ssr' : ''}.json`);
74+
const optionsFilename = path.join(
75+
dir,
76+
`svelte.options.${cmd}${config.build.ssr ? '.ssr' : ''}.json`
77+
);
6978
fs.writeFileSync(filename, JSON.stringify(serializableConfig, replacer, '\t'), 'utf-8');
79+
fs.writeFileSync(optionsFilename, JSON.stringify(configFileOptions, replacer, '\t'), 'utf-8');
80+
}
81+
};
82+
}
83+
84+
/**
85+
* write resolved config
86+
* @returns {import('vite').Plugin}
87+
*/
88+
export function pushToOptimizeDepsExtensions() {
89+
return {
90+
name: 'push-to-optimize-deps-extensions',
91+
config(config) {
92+
if (!config.optimizeDeps) {
93+
config.optimizeDeps = {};
94+
}
95+
if (!config.optimizeDeps.extensions) {
96+
config.optimizeDeps.extensions = [];
97+
}
98+
config.optimizeDeps.extensions.push('.pushed');
7099
}
71100
};
72101
}

packages/e2e-tests/kit-node/__tests__/kit.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,30 @@ describe('kit-node', () => {
373373
});
374374
});
375375

376+
describe('resolved options', () => {
377+
it('should not be polluted by other plugins pushing to extensions', async () => {
378+
const configs = [];
379+
if (isBuild) {
380+
configs.push('serve', 'build', 'build.ssr');
381+
} else {
382+
configs.push('serve');
383+
}
384+
const expectArrayEqual = (a: string[], b: string[], message: string) => {
385+
const aSorted = a.slice().sort();
386+
const bSorted = b.slice().sort();
387+
expect(aSorted, message).toEqual(bSorted);
388+
};
389+
for (const pattern of configs) {
390+
const filename = `svelte.options.${pattern}.json`;
391+
const config = JSON.parse(
392+
await readFileContent(path.join('logs', 'resolved-configs', filename))
393+
);
394+
const isServe = pattern === 'serve';
395+
expectArrayEqual(['.svelte'], config.extensions, `extensions in ${filename}`);
396+
}
397+
});
398+
});
399+
376400
describe.runIf(isBuild)('output', () => {
377401
it('should produce hermetic build', async () => {
378402
const outputFiles = await glob('./build/**/*', { cwd: testDir, filesOnly: true });

packages/e2e-tests/kit-node/vite.config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { sveltekit } from '@sveltejs/kit/vite';
2-
import { transformValidation, writeResolvedConfig } from 'e2e-test-dep-vite-plugins';
2+
import {
3+
transformValidation,
4+
writeResolvedConfig,
5+
pushToOptimizeDepsExtensions
6+
} from 'e2e-test-dep-vite-plugins';
37

48
/** @type {import('vite').UserConfig} */
59
export default {
@@ -16,6 +20,7 @@ export default {
1620
sourcemap: true // must be true for hermetic build test!
1721
},
1822
plugins: [
23+
pushToOptimizeDepsExtensions(),
1924
transformValidation(),
2025
sveltekit(),
2126
writeResolvedConfig(),

packages/vite-plugin-svelte/src/utils/options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ export async function buildExtraViteConfig(options, config) {
386386
extraViteConfig.optimizeDeps = {
387387
...extraViteConfig.optimizeDeps,
388388
// Experimental Vite API to allow these extensions to be scanned and prebundled
389-
extensions: options.extensions ?? ['.svelte'],
389+
extensions: options.extensions ? [...options.extensions] : ['.svelte'],
390390
// Add esbuild plugin to prebundle Svelte files.
391391
// Currently a placeholder as more information is needed after Vite config is resolved,
392392
// the real Svelte plugin is added in `patchResolvedViteConfig()`

0 commit comments

Comments
 (0)