Skip to content

Commit 04301ac

Browse files
committed
feat: add filtering options to svelteJsxSnippet plugin
This commit introduces the ability to include or exclude files from being processed by the svelteJsxSnippet plugin. This is achieved by using the createFilter function from the @rollup/pluginutils package. The filtering options can be passed as an argument to the svelteJsxSnippet function.
1 parent 194fef3 commit 04301ac

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

packages/svelte-jsx-snippet/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"@babel/generator": "^7.24.10",
5252
"@babel/parser": "^7.24.8",
5353
"@babel/types": "^7.24.9",
54+
"@rollup/pluginutils": "^5.1.0",
5455
"@types/babel__generator": "^7.6.8",
5556
"magic-string": "^0.30.10",
5657
"svelte": "5.0.0-next.183",

packages/svelte-jsx-snippet/src/vite.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,44 @@
11
import type { Plugin } from "vite";
22
import { transform } from "./transformer";
3+
import { type FilterPattern, createFilter as rollupCreateFilter } from '@rollup/pluginutils';
4+
5+
6+
export interface Options {
7+
include?: FilterPattern;
8+
exclude?: FilterPattern;
9+
};
10+
11+
/**
12+
* Create a filter function from the given include and exclude patterns.
13+
* @param include - An array of minimatch or regex pattern strings. @default [/\.[mc]?[jt]sx$/] only handle .jsx, .tsx
14+
* @param exclude - An array of minimatch or regex pattern strings.
15+
*/
16+
function createFilter(
17+
include: Options['include'],
18+
exclude: Options['exclude'],
19+
): ReturnType<typeof rollupCreateFilter> {
20+
return rollupCreateFilter(
21+
include ?? [/\.[mc]?[jt]sx$/],
22+
exclude
23+
);
24+
}
25+
26+
export function svelteJsxSnippet(options: Options = {}): Plugin {
27+
const filter = createFilter(options.include, options.exclude);
328

4-
export function svelteJsxSnippet(): Plugin {
529
let dev = false;
630
return {
731
name: "svelte-jsx-snippet",
832
enforce: "pre",
933
configResolved(config) {
1034
dev = config.command === "serve";
1135
},
36+
1237
/**
1338
* Transform JSX to snippet (generated) js
1439
*/
1540
transform(code, id, options) {
16-
// only handle .jsx, .tsx
17-
if (!/\.[mc]?[jt]sx$/.test(id)) return null;
41+
if (!filter(id)) return;
1842

1943
return transform(code, {
2044
dev,

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)