Skip to content

Commit 3a45ae3

Browse files
authored
feat: promote experimental.dynamicCompileOptions (#765)
* feat: promote experimental.dynamicCompileOptions * fix: prevent prettier from removing leading ws of testcase
1 parent b4b4fdd commit 3a45ae3

File tree

16 files changed

+163
-52
lines changed

16 files changed

+163
-52
lines changed

.changeset/nasty-queens-beam.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
feat(compile): promote experimental.dynamicCompileOptions to stable

.prettierrc.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ export default {
1515
options: { parser: 'typescript' }
1616
},
1717
{
18-
files: ['**/CHANGELOG.md', '.github/renovate.json5'],
18+
files: [
19+
'**/vite.config.js.timestamp-*.mjs',
20+
'**/CHANGELOG.md',
21+
'.github/renovate.json5',
22+
'packages/e2e-tests/dynamic-compile-options/src/components/A.svelte'
23+
],
1924
options: {
20-
requirePragma: true
25+
rangeEnd: 0
2126
}
2227
},
2328
{

docs/config.md

+32-32
Original file line numberDiff line numberDiff line change
@@ -221,38 +221,6 @@ A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patt
221221

222222
Inspector mode shows you the file location where the element under cursor is defined and you can click to quickly open your code editor at this location.
223223

224-
## Experimental options
225-
226-
These options are considered experimental and breaking changes to them can occur in any release! Specify them under the `experimental` option.
227-
228-
Either in Vite config:
229-
230-
```js
231-
// vite.config.js
232-
export default defineConfig({
233-
plugins: [
234-
svelte({
235-
experimental: {
236-
// experimental options
237-
}
238-
})
239-
]
240-
});
241-
```
242-
243-
or in Svelte config:
244-
245-
```js
246-
// svelte.config.js
247-
export default {
248-
vitePlugin: {
249-
experimental: {
250-
// experimental options
251-
}
252-
}
253-
};
254-
```
255-
256224
### dynamicCompileOptions
257225

258226
- **Type:**
@@ -287,6 +255,38 @@ export default {
287255
});
288256
```
289257

258+
## Experimental options
259+
260+
These options are considered experimental and breaking changes to them can occur in any release! Specify them under the `experimental` option.
261+
262+
Either in Vite config:
263+
264+
```js
265+
// vite.config.js
266+
export default defineConfig({
267+
plugins: [
268+
svelte({
269+
experimental: {
270+
// experimental options
271+
}
272+
})
273+
]
274+
});
275+
```
276+
277+
or in Svelte config:
278+
279+
```js
280+
// svelte.config.js
281+
export default {
282+
vitePlugin: {
283+
experimental: {
284+
// experimental options
285+
}
286+
}
287+
};
288+
```
289+
290290
### sendWarningsToBrowser
291291

292292
- **Type:** `boolean`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { getText } from '~utils';
2+
3+
test('should respect dynamic compile option preserveWhitespace: true for A', async () => {
4+
expect(await getText('#A')).toBe(' preserved leading whitespace');
5+
expect(await getText('#B')).toBe('removed leading whitespace');
6+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width,initial-scale=1" />
6+
7+
<title>Svelte app</title>
8+
9+
<script type="module" src="/src/main.js"></script>
10+
</head>
11+
12+
<body></body>
13+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "e2e-tests-custom-extensions",
3+
"private": true,
4+
"version": "1.0.0",
5+
"scripts": {
6+
"build": "vite build",
7+
"dev": "vite",
8+
"preview": "vite preview"
9+
},
10+
"devDependencies": {
11+
"@sveltejs/vite-plugin-svelte": "workspace:^",
12+
"svelte": "^4.2.1",
13+
"vite": "^5.0.0-beta.4"
14+
},
15+
"type": "module"
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import A from './components/A.svelte';
3+
import B from './components/B.svelte';
4+
</script>
5+
6+
<A />
7+
<B />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id="A"> preserved leading whitespace</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id="B">removed leading whitespace</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import App from './App.svelte';
2+
3+
const app = new App({
4+
target: document.body
5+
});
6+
7+
export default app;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { svelte } from '@sveltejs/vite-plugin-svelte';
2+
import { defineConfig } from 'vite';
3+
4+
export default defineConfig(() => {
5+
return {
6+
plugins: [
7+
svelte({
8+
dynamicCompileOptions({ filename }) {
9+
if (filename.endsWith('A.svelte')) {
10+
return {
11+
preserveWhitespace: true
12+
};
13+
}
14+
}
15+
})
16+
],
17+
build: {
18+
// make build faster by skipping transforms and minification
19+
target: 'esnext',
20+
minify: false
21+
},
22+
server: {
23+
watch: {
24+
// During tests we edit the files too fast and sometimes chokidar
25+
// misses change events, so enforce polling for consistency
26+
usePolling: true,
27+
interval: 100
28+
}
29+
}
30+
};
31+
});

packages/vite-plugin-svelte/src/index.d.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,7 @@ interface SvelteOptions {
121121
* @see https://svelte.dev/docs#svelte_compile
122122
*/
123123
compilerOptions?: Omit<CompileOptions, 'filename' | 'format' | 'generate'>;
124-
/**
125-
* Handles warning emitted from the Svelte compiler
126-
*/
127-
onwarn?: (warning: Warning, defaultHandler?: (warning: Warning) => void) => void;
128-
/**
129-
* Options for vite-plugin-svelte
130-
*/
131-
vitePlugin?: PluginOptions;
132-
}
133124

134-
/**
135-
* These options are considered experimental and breaking changes to them can occur in any release
136-
*/
137-
interface ExperimentalOptions {
138125
/**
139126
* A function to update `compilerOptions` before compilation
140127
*
@@ -159,6 +146,21 @@ interface ExperimentalOptions {
159146
code: string;
160147
compileOptions: Partial<CompileOptions>;
161148
}) => Promise<Partial<CompileOptions> | void> | Partial<CompileOptions> | void;
149+
150+
/**
151+
* Handles warning emitted from the Svelte compiler
152+
*/
153+
onwarn?: (warning: Warning, defaultHandler?: (warning: Warning) => void) => void;
154+
/**
155+
* Options for vite-plugin-svelte
156+
*/
157+
vitePlugin?: PluginOptions;
158+
}
159+
160+
/**
161+
* These options are considered experimental and breaking changes to them can occur in any release
162+
*/
163+
interface ExperimentalOptions {
162164
/**
163165
* send a websocket message with svelte compiler warnings during dev
164166
*

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export const _createCompileSvelte = (makeHot) => {
100100
};
101101
}
102102
const finalCode = preprocessed ? preprocessed.code : code;
103-
const dynamicCompileOptions = await options.experimental?.dynamicCompileOptions?.({
103+
const dynamicCompileOptions = await options?.dynamicCompileOptions?.({
104104
filename,
105105
code: finalCode,
106106
compileOptions

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,18 @@ async function compileSvelte(options, { filename, code }, statsCollection) {
8181

8282
const finalCode = preprocessed ? preprocessed.code : code;
8383

84-
const dynamicCompileOptions = await options.experimental?.dynamicCompileOptions?.({
84+
const dynamicCompileOptions = await options?.dynamicCompileOptions?.({
8585
filename,
8686
code: finalCode,
8787
compileOptions
8888
});
8989

9090
if (dynamicCompileOptions && log.debug.enabled) {
91-
log.debug(`dynamic compile options for ${filename}: ${JSON.stringify(dynamicCompileOptions)}`);
91+
log.debug(
92+
`dynamic compile options for ${filename}: ${JSON.stringify(dynamicCompileOptions)}`,
93+
undefined,
94+
'compile'
95+
);
9296
}
9397

9498
const finalCompileOptions = dynamicCompileOptions

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const allowedPluginOptions = new Set([
3636
'disableDependencyReinclusion',
3737
'prebundleSvelteLibraries',
3838
'inspector',
39+
'dynamicCompileOptions',
3940
'experimental'
4041
]);
4142

@@ -316,13 +317,13 @@ function removeIgnoredOptions(options) {
316317
function handleDeprecatedOptions(options) {
317318
const experimental = /** @type {Record<string, any>} */ (options.experimental);
318319
if (experimental) {
319-
for (const promoted of ['prebundleSvelteLibraries', 'inspector']) {
320+
for (const promoted of ['prebundleSvelteLibraries', 'inspector', 'dynamicCompileOptions']) {
320321
if (experimental[promoted]) {
321322
//@ts-expect-error untyped assign
322323
options[promoted] = experimental[promoted];
323324
delete experimental[promoted];
324325
log.warn(
325-
`Option "vitePlugin.experimental.${promoted}" is no longer experimental and has moved to "vitePlugin.${promoted}". Please update your svelte config.`
326+
`Option "experimental.${promoted}" is no longer experimental and has moved to "${promoted}". Please update your Svelte or Vite config.`
326327
);
327328
}
328329
}

pnpm-lock.yaml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)