Skip to content

Commit 4683ad6

Browse files
authored
feat: improve svelte-preprocess api for plugins (#98)
1 parent bbc1c17 commit 4683ad6

File tree

6 files changed

+79
-20
lines changed

6 files changed

+79
-20
lines changed

.changeset/eleven-windows-roll.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
Add option to ignore svelte preprocessors of other vite plugins
6+
7+
- ignore them all: `ignorePluginPreprocessors: true`
8+
- ignore by name: `ignorePluginPreprocessors: ['<name of plugin>',...]`

.changeset/flat-mayflies-smell.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
Move plugin preprocessor definition to api namespace
6+
7+
Plugins that provide `myplugin.sveltePreprocess`, should move it to `myplugin.api.sveltePreprocess`, as suggested by [rollup](https://rollupjs.org/guide/en/#direct-plugin-communication)

packages/vite-plugin-svelte/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,15 @@ If you're unsure whether a library uses the lifecycle API, place it in `optimize
7272
### Add an extra preprocessor
7373

7474
vite-plugin-svelte uses the svelte compiler to split `.svelte` files into js and css and the svelte compiler requires that the css passed to it is already plain css.
75-
If you are building a plugin for vite that transforms css and want it to work out of the box with vite-plugin-svelte, you can add a `sveltePreprocess: PreprocessorGroup` to your vite plugin definition and vite-plugin-svelte will pick it up and add it to the list of svelte preprocessors used at runtime.
75+
If you are building a plugin for vite that transforms css and want it to work out of the box with vite-plugin-svelte, you can add a `api.sveltePreprocess: PreprocessorGroup` to your vite plugin definition and vite-plugin-svelte will pick it up and add it to the list of svelte preprocessors used at runtime.
7676

7777
```js
7878
const vitePluginCoolCss = {
7979
name: 'vite-plugin-coolcss',
80-
sveltePreprocess: {
81-
/* your PreprocessorGroup here */
80+
api: {
81+
sveltePreprocess: {
82+
/* your PreprocessorGroup here */
83+
}
8284
}
8385
/*... your cool css plugin implementation here .. */
8486
};

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import {
99
validateInlineOptions,
1010
Options,
1111
ResolvedOptions,
12-
resolveOptions,
13-
PreprocessorGroup
12+
resolveOptions
1413
} from './utils/options';
1514
import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache';
1615

@@ -19,14 +18,6 @@ import { resolveViaPackageJsonSvelte } from './utils/resolve';
1918
import { addExtraPreprocessors } from './utils/preprocess';
2019
import { PartialResolvedId } from 'rollup';
2120

22-
// extend the Vite plugin interface to be able to have `sveltePreprocess` injection
23-
declare module 'vite' {
24-
// eslint-disable-next-line no-unused-vars
25-
interface Plugin {
26-
sveltePreprocess?: PreprocessorGroup;
27-
}
28-
}
29-
3021
export function svelte(inlineOptions?: Partial<Options>): Plugin {
3122
if (process.env.DEBUG != null) {
3223
log.setLevel('debug');

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const knownOptions = new Set([
1515
'preprocess',
1616
'hot',
1717
'disableCssHmr',
18-
'useVitePreprocess'
18+
'useVitePreprocess',
19+
'ignorePluginPreprocessors'
1920
]);
2021

2122
function buildDefaultOptions(isProduction: boolean, options: Partial<Options>): Partial<Options> {
@@ -380,6 +381,14 @@ export interface Options {
380381
*/
381382
disableCssHmr?: boolean;
382383

384+
/**
385+
* vite plugins can contribute additional preprocessors by defining api.sveltePreprocess.
386+
* If you don't want to use them, set this to true to ignore them all or use an array of strings with plugin names to specify which
387+
*
388+
* @default false
389+
*/
390+
ignorePluginPreprocessors?: boolean | string[];
391+
383392
/**
384393
* use vite as extra css preprocessor EXPERIMENTAL!
385394
* @default false

packages/vite-plugin-svelte/src/utils/preprocess.ts

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ResolvedConfig, TransformResult } from 'vite';
1+
import { ResolvedConfig, TransformResult, Plugin } from 'vite';
22
import MagicString from 'magic-string';
33
import { Preprocessor, PreprocessorGroup, ResolvedOptions } from './options';
44
import { TransformPluginContext } from 'rollup';
@@ -86,16 +86,58 @@ function buildExtraPreprocessors(options: ResolvedOptions, config: ResolvedConfi
8686
extraPreprocessors.push(createVitePreprocessorGroup(config, options));
8787
}
8888

89-
const pluginsWithPreprocessors = config.plugins.filter((p) => p?.sveltePreprocess);
90-
if (pluginsWithPreprocessors.length > 0) {
89+
// @ts-ignore
90+
const pluginsWithPreprocessorsDeprecated = config.plugins.filter((p) => p?.sveltePreprocess);
91+
if (pluginsWithPreprocessorsDeprecated.length > 0) {
92+
log.warn(
93+
`The following plugins use the deprecated 'plugin.sveltePreprocess' field. Please contact their maintainers and ask them to move it to 'plugin.api.sveltePreprocess': ${pluginsWithPreprocessorsDeprecated
94+
.map((p) => p.name)
95+
.join(', ')}`
96+
);
97+
// patch plugin to avoid breaking
98+
pluginsWithPreprocessorsDeprecated.forEach((p) => {
99+
if (!p.api) {
100+
p.api = {};
101+
}
102+
if (p.api.sveltePreprocess === undefined) {
103+
// @ts-ignore
104+
p.api.sveltePreprocess = p.sveltePreprocess;
105+
} else {
106+
log.error(
107+
`ignoring plugin.sveltePreprocess of ${p.name} because it already defined plugin.api.sveltePreprocess.`
108+
);
109+
}
110+
});
111+
}
112+
113+
const pluginsWithPreprocessors: Plugin[] = config.plugins.filter((p) => p?.api?.sveltePreprocess);
114+
const ignored: Plugin[] = [],
115+
included: Plugin[] = [];
116+
for (const p of pluginsWithPreprocessors) {
117+
if (
118+
options.ignorePluginPreprocessors === true ||
119+
(Array.isArray(options.ignorePluginPreprocessors) &&
120+
options.ignorePluginPreprocessors?.includes(p.name))
121+
) {
122+
ignored.push(p);
123+
} else {
124+
included.push(p);
125+
}
126+
}
127+
if (ignored.length > 0) {
91128
log.debug(
92-
`adding preprocessors from other vite plugins: ${pluginsWithPreprocessors
129+
`Ignoring svelte preprocessors defined by these vite plugins: ${ignored
93130
.map((p) => p.name)
94131
.join(', ')}`
95132
);
96-
extraPreprocessors.push(
97-
...pluginsWithPreprocessors.map((p) => p.sveltePreprocess as PreprocessorGroup)
133+
}
134+
if (included.length > 0) {
135+
log.debug(
136+
`Adding svelte preprocessors defined by these vite plugins: ${included
137+
.map((p) => p.name)
138+
.join(', ')}`
98139
);
140+
extraPreprocessors.push(...pluginsWithPreprocessors.map((p) => p.api.sveltePreprocess));
99141
}
100142

101143
if (options.hot && !options.disableCssHmr) {

0 commit comments

Comments
 (0)