Skip to content

Commit

Permalink
feat: promote experimental.dynamicCompileOptions (#765)
Browse files Browse the repository at this point in the history
* feat: promote experimental.dynamicCompileOptions

* fix: prevent prettier from removing leading ws of testcase
  • Loading branch information
dominikg authored Oct 15, 2023
1 parent b4b4fdd commit 3a45ae3
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 52 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-queens-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': patch
---

feat(compile): promote experimental.dynamicCompileOptions to stable
9 changes: 7 additions & 2 deletions .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ export default {
options: { parser: 'typescript' }
},
{
files: ['**/CHANGELOG.md', '.github/renovate.json5'],
files: [
'**/vite.config.js.timestamp-*.mjs',
'**/CHANGELOG.md',
'.github/renovate.json5',
'packages/e2e-tests/dynamic-compile-options/src/components/A.svelte'
],
options: {
requirePragma: true
rangeEnd: 0
}
},
{
Expand Down
64 changes: 32 additions & 32 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,38 +221,6 @@ A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patt

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.

## Experimental options

These options are considered experimental and breaking changes to them can occur in any release! Specify them under the `experimental` option.

Either in Vite config:

```js
// vite.config.js
export default defineConfig({
plugins: [
svelte({
experimental: {
// experimental options
}
})
]
});
```

or in Svelte config:

```js
// svelte.config.js
export default {
vitePlugin: {
experimental: {
// experimental options
}
}
};
```

### dynamicCompileOptions

- **Type:**
Expand Down Expand Up @@ -287,6 +255,38 @@ export default {
});
```

## Experimental options

These options are considered experimental and breaking changes to them can occur in any release! Specify them under the `experimental` option.

Either in Vite config:

```js
// vite.config.js
export default defineConfig({
plugins: [
svelte({
experimental: {
// experimental options
}
})
]
});
```

or in Svelte config:

```js
// svelte.config.js
export default {
vitePlugin: {
experimental: {
// experimental options
}
}
};
```

### sendWarningsToBrowser

- **Type:** `boolean`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getText } from '~utils';

test('should respect dynamic compile option preserveWhitespace: true for A', async () => {
expect(await getText('#A')).toBe(' preserved leading whitespace');
expect(await getText('#B')).toBe('removed leading whitespace');
});
13 changes: 13 additions & 0 deletions packages/e2e-tests/dynamic-compile-options/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />

<title>Svelte app</title>

<script type="module" src="/src/main.js"></script>
</head>

<body></body>
</html>
16 changes: 16 additions & 0 deletions packages/e2e-tests/dynamic-compile-options/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "e2e-tests-custom-extensions",
"private": true,
"version": "1.0.0",
"scripts": {
"build": "vite build",
"dev": "vite",
"preview": "vite preview"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "workspace:^",
"svelte": "^4.2.1",
"vite": "^5.0.0-beta.4"
},
"type": "module"
}
7 changes: 7 additions & 0 deletions packages/e2e-tests/dynamic-compile-options/src/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import A from './components/A.svelte';
import B from './components/B.svelte';
</script>

<A />
<B />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div id="A"> preserved leading whitespace</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div id="B">removed leading whitespace</div>
7 changes: 7 additions & 0 deletions packages/e2e-tests/dynamic-compile-options/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import App from './App.svelte';

const app = new App({
target: document.body
});

export default app;
31 changes: 31 additions & 0 deletions packages/e2e-tests/dynamic-compile-options/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { svelte } from '@sveltejs/vite-plugin-svelte';
import { defineConfig } from 'vite';

export default defineConfig(() => {
return {
plugins: [
svelte({
dynamicCompileOptions({ filename }) {
if (filename.endsWith('A.svelte')) {
return {
preserveWhitespace: true
};
}
}
})
],
build: {
// make build faster by skipping transforms and minification
target: 'esnext',
minify: false
},
server: {
watch: {
// During tests we edit the files too fast and sometimes chokidar
// misses change events, so enforce polling for consistency
usePolling: true,
interval: 100
}
}
};
});
28 changes: 15 additions & 13 deletions packages/vite-plugin-svelte/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,7 @@ interface SvelteOptions {
* @see https://svelte.dev/docs#svelte_compile
*/
compilerOptions?: Omit<CompileOptions, 'filename' | 'format' | 'generate'>;
/**
* Handles warning emitted from the Svelte compiler
*/
onwarn?: (warning: Warning, defaultHandler?: (warning: Warning) => void) => void;
/**
* Options for vite-plugin-svelte
*/
vitePlugin?: PluginOptions;
}

/**
* These options are considered experimental and breaking changes to them can occur in any release
*/
interface ExperimentalOptions {
/**
* A function to update `compilerOptions` before compilation
*
Expand All @@ -159,6 +146,21 @@ interface ExperimentalOptions {
code: string;
compileOptions: Partial<CompileOptions>;
}) => Promise<Partial<CompileOptions> | void> | Partial<CompileOptions> | void;

/**
* Handles warning emitted from the Svelte compiler
*/
onwarn?: (warning: Warning, defaultHandler?: (warning: Warning) => void) => void;
/**
* Options for vite-plugin-svelte
*/
vitePlugin?: PluginOptions;
}

/**
* These options are considered experimental and breaking changes to them can occur in any release
*/
interface ExperimentalOptions {
/**
* send a websocket message with svelte compiler warnings during dev
*
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-svelte/src/utils/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const _createCompileSvelte = (makeHot) => {
};
}
const finalCode = preprocessed ? preprocessed.code : code;
const dynamicCompileOptions = await options.experimental?.dynamicCompileOptions?.({
const dynamicCompileOptions = await options?.dynamicCompileOptions?.({
filename,
code: finalCode,
compileOptions
Expand Down
8 changes: 6 additions & 2 deletions packages/vite-plugin-svelte/src/utils/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,18 @@ async function compileSvelte(options, { filename, code }, statsCollection) {

const finalCode = preprocessed ? preprocessed.code : code;

const dynamicCompileOptions = await options.experimental?.dynamicCompileOptions?.({
const dynamicCompileOptions = await options?.dynamicCompileOptions?.({
filename,
code: finalCode,
compileOptions
});

if (dynamicCompileOptions && log.debug.enabled) {
log.debug(`dynamic compile options for ${filename}: ${JSON.stringify(dynamicCompileOptions)}`);
log.debug(
`dynamic compile options for ${filename}: ${JSON.stringify(dynamicCompileOptions)}`,
undefined,
'compile'
);
}

const finalCompileOptions = dynamicCompileOptions
Expand Down
5 changes: 3 additions & 2 deletions packages/vite-plugin-svelte/src/utils/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const allowedPluginOptions = new Set([
'disableDependencyReinclusion',
'prebundleSvelteLibraries',
'inspector',
'dynamicCompileOptions',
'experimental'
]);

Expand Down Expand Up @@ -316,13 +317,13 @@ function removeIgnoredOptions(options) {
function handleDeprecatedOptions(options) {
const experimental = /** @type {Record<string, any>} */ (options.experimental);
if (experimental) {
for (const promoted of ['prebundleSvelteLibraries', 'inspector']) {
for (const promoted of ['prebundleSvelteLibraries', 'inspector', 'dynamicCompileOptions']) {
if (experimental[promoted]) {
//@ts-expect-error untyped assign
options[promoted] = experimental[promoted];
delete experimental[promoted];
log.warn(
`Option "vitePlugin.experimental.${promoted}" is no longer experimental and has moved to "vitePlugin.${promoted}". Please update your svelte config.`
`Option "experimental.${promoted}" is no longer experimental and has moved to "${promoted}". Please update your Svelte or Vite config.`
);
}
}
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3a45ae3

Please sign in to comment.