Skip to content

Commit 2eeef7e

Browse files
feat: pass environment to dynamicCompileOptions (#1386)
* feat: pass environment to `dynamicCompileOptions` * chore: remove undefined * chore: remove "if available"
1 parent b19af50 commit 2eeef7e

22 files changed

Lines changed: 1103 additions & 24 deletions

File tree

‎.changeset/witty-bars-film.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': minor
3+
---
4+
5+
feat: pass environment to `dynamicCompileOptions`

‎.prettierrc.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default {
2424
'.changeset/pre.json',
2525
'**/vite.config.js.timestamp-*.mjs',
2626
'packages/e2e-tests/dynamic-compile-options/src/components/A.svelte',
27+
'packages/e2e-tests/dynamic-compile-options/src/components/Environment.svelte',
2728
'packages/playground/big/src/pages/**', // lots of generated files
2829
'packages/e2e-tests/scan-deps/src/Svelte*.svelte', // various syntax tests that require no format
2930
'**/.vite-inspect/**',

‎docs/config.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patt
231231
filename: string; // The file to be compiled
232232
code: string; // The preprocessed Svelte code
233233
compileOptions: Partial<CompileOptions>; // The current compiler options
234+
environment: Environment; // The current Vite environment
234235
}) => Promise<Partial<CompileOptions> | void> | Partial<CompileOptions> | void;
235236
```
236237

@@ -243,9 +244,9 @@ A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patt
243244
export default defineConfig({
244245
plugins: [
245246
svelte({
246-
dynamicCompileOptions({ filename, compileOptions }) {
247+
dynamicCompileOptions({ filename, compileOptions, environment }) {
247248
// Dynamically set runes mode per Svelte file
248-
if (forceRunesMode(filename) && !compileOptions.runes) {
249+
if (environment.name === 'client' && forceRunesMode(filename) && !compileOptions.runes) {
249250
return { runes: true };
250251
}
251252
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import Dependency from './src/components/Dependency.svelte';
2+
export { default as Environment } from './src/components/Environment.svelte';
23
export default Dependency;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id="optimizer-environment"> optimizer environment</div>

‎packages/e2e-tests/dynamic-compile-options/__tests__/dynamic-compile-options.spec.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ test('should respect dynamic compile option preserveWhitespace: true for A', asy
44
expect(await getText('#A')).toBe(' preserved leading whitespace');
55
expect(await getText('#B')).toBe('removed leading whitespace');
66
});
7+
8+
test('passes the current environment to dynamicCompileOptions', async () => {
9+
expect(await getText('#environment')).toBe(' environment');
10+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<script>
22
import A from './components/A.svelte';
33
import B from './components/B.svelte';
4+
import Environment from './components/Environment.svelte';
45
</script>
56

67
<A />
78
<B />
9+
<Environment />
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id="environment"> environment</div>

‎packages/e2e-tests/dynamic-compile-options/vite.config.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ export default defineConfig(() => {
55
return {
66
plugins: [
77
svelte({
8-
dynamicCompileOptions({ filename }) {
8+
dynamicCompileOptions({ filename, environment }) {
99
if (filename.endsWith('A.svelte')) {
1010
return {
1111
preserveWhitespace: true
1212
};
1313
}
14+
if (filename.endsWith('Environment.svelte') && environment?.name === 'client') {
15+
return {
16+
preserveWhitespace: true
17+
};
18+
}
1419
}
1520
})
1621
],

‎packages/e2e-tests/prebundle-svelte-deps/__tests__/prebundle-svelte-deps.spec.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ async function expectPageToWork() {
2525
expect(await getText('#module button:first-child')).toBe('count is 0');
2626
}
2727

28+
test('passes the current environment while compiling a Svelte dependency', async () => {
29+
expect(await getText('#optimizer-environment')).toBe(' optimizer environment');
30+
});
31+
2832
if (!isBuild) {
2933
test('page works with pre-bundling enabled', async () => {
3034
await expectPageToWork();

0 commit comments

Comments
 (0)