Skip to content

Commit 79a56c4

Browse files
authored
fix: allow preprocess plugin to run twice (#1206)
* fix: allow preprocess plugin to run twice * fix: set filter * chore: fix filter * chore: add test
1 parent e10850b commit 79a56c4

File tree

10 files changed

+101
-19
lines changed

10 files changed

+101
-19
lines changed

.changeset/eight-donkeys-count.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: allow preprocess plugin to run twice
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { fileURLToPath } from 'node:url';
2+
import { build, type Rollup, type InlineConfig } from 'vite';
3+
import { svelte } from '@sveltejs/vite-plugin-svelte';
4+
import { isBuild } from '~utils';
5+
6+
test.runIf(isBuild)('build-multiple', async () => {
7+
const sharedConfig: InlineConfig = {
8+
configFile: false,
9+
root: fileURLToPath(new URL('../', import.meta.url)),
10+
build: {
11+
write: false
12+
},
13+
logLevel: 'silent',
14+
plugins: [svelte()]
15+
};
16+
17+
// Ensure two builds work as expected and have no build errors
18+
19+
const output1 = await build({ ...sharedConfig, mode: 'production' });
20+
expect(output1).toHaveProperty('output');
21+
expect(
22+
(output1 as Rollup.RollupOutput).output.find(
23+
(c) => c.type === 'chunk' && c.code.includes('mode: production')
24+
)
25+
).toBeDefined();
26+
27+
const output2 = await build({ ...sharedConfig, mode: 'staging' });
28+
expect(output2).toHaveProperty('output');
29+
expect(
30+
(output2 as Rollup.RollupOutput).output.find(
31+
(c) => c.type === 'chunk' && c.code.includes('mode: staging')
32+
)
33+
).toBeDefined();
34+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" href="/favicon.png" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Svelte App</title>
8+
</head>
9+
<body>
10+
<script type="module" src="/src/index.js"></script>
11+
</body>
12+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "e2e-tests-build-multiple",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite dev",
8+
"build": "vite build",
9+
"preview": "vite preview"
10+
},
11+
"devDependencies": {
12+
"@sveltejs/vite-plugin-svelte": "workspace:^",
13+
"svelte": "^5.38.2",
14+
"vite": "^7.1.2"
15+
}
16+
}
3.05 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>mode: {import.meta.env.MODE}</p>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import App from './App.svelte';
2+
import { mount } from 'svelte';
3+
mount(App, { target: document.body });
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { svelte } from '@sveltejs/vite-plugin-svelte';
2+
import { defineConfig } from 'vite';
3+
4+
export default defineConfig({
5+
plugins: [svelte()]
6+
});

packages/vite-plugin-svelte/src/plugins/preprocess.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,21 @@ export function preprocess(api) {
3131
name: 'vite-plugin-svelte:preprocess',
3232
enforce: 'pre',
3333
configResolved(c) {
34-
//@ts-expect-error defined below but filter not in type
35-
plugin.transform.filter = api.filter;
3634
options = api.options;
3735
if (arraify(options.preprocess).length > 0) {
3836
preprocessSvelte = createPreprocessSvelte(options, c);
37+
// @ts-expect-error defined below but filter not in type
38+
plugin.transform.filter = api.filter;
3939
} else {
4040
log.debug(
4141
`disabling ${plugin.name} because no preprocessor is configured`,
4242
undefined,
4343
'preprocess'
4444
);
45-
delete plugin.transform;
45+
// @ts-expect-error force set undefined to clear memory
46+
preprocessSvelte = undefined;
47+
// @ts-expect-error defined below but filter not in type
48+
plugin.transform.filter = { id: /$./ }; // never match
4649
}
4750
},
4851
configureServer(server) {

pnpm-lock.yaml

Lines changed: 18 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)