Skip to content

Commit 571d943

Browse files
committed
Support mdsvex
1 parent 98aaaeb commit 571d943

File tree

6 files changed

+35
-2
lines changed

6 files changed

+35
-2
lines changed

docs/src/pages/docs/packages/whyframe-svelte.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,10 @@ A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patt
2525
- **Type:** `string | RegExp | (string | RegExp)[]`
2626

2727
A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patterns, which specifies the files to be ignored by the plugin.
28+
29+
### preprocess
30+
31+
- **Type:** `boolean`
32+
- **Default:** `false`
33+
34+
Whether to run `whyframe`'s Svelte transformation as a [preprocessor](https://svelte.dev/docs#compile-time-svelte-preprocess). This is only required when using a markup preprocessor that changes the Svelte syntax, e.g. [mdsvex](https://mdsvex.pngwn.io).

packages/svelte/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Bump dependencies
66
- Refactor files for unit tests
7+
- Add `preprocess` option for `mdsvex` support
78

89
## 0.1.3 (2022-10-03)
910

packages/svelte/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { FilterPattern, Plugin } from 'vite'
33
export interface Options {
44
include?: FilterPattern
55
exclude?: FilterPattern
6+
preprocess?: boolean
67
}
78

89
export declare function whyframeSvelte(options?: Options): Plugin

packages/svelte/src/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ import { transform } from './shared.js'
77
export function whyframeSvelte(options) {
88
/** @type {import('@whyframe/core').Api} */
99
let api
10+
/** @type {any} */
11+
let ctx
1012

1113
const filter = createFilter(options?.include || /\.svelte$/, options?.exclude)
1214

1315
/** @type {import('vite').Plugin} */
1416
const plugin = {
1517
name: 'whyframe:svelte',
1618
enforce: 'pre',
19+
buildStart() {
20+
ctx = this
21+
},
1722
configResolved(c) {
1823
api = c.plugins.find((p) => p.name === 'whyframe:api')?.api
1924
if (!api) {
@@ -40,5 +45,19 @@ export function whyframeSvelte(options) {
4045
}
4146
}
4247

48+
if (options?.preprocess) {
49+
// convert to vite-plugin-svelte's api so typescript etc are already preprocessed
50+
const _transform = plugin.transform
51+
delete plugin.transform
52+
plugin.api = {
53+
sveltePreprocess: {
54+
markup({ content, filename }) {
55+
// @ts-expect-error
56+
return _transform.apply(ctx, [content, filename])
57+
}
58+
}
59+
}
60+
}
61+
4362
return plugin
4463
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Markdown
22

3-
Ahoy
3+
<iframe data-why title="mdsvex">Works in mdsvex too!</iframe>
4+
5+
Simply:
46

7+
```html
58
<iframe data-why title="mdsvex">Works in mdsvex too!</iframe>
9+
```

playground/sveltekit/vite.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export default defineConfig({
1212
components: [{ name: 'Story', showSource: true }]
1313
}),
1414
whyframeSvelte({
15-
include: /\.(svelte|svx)$/
15+
include: /\.(svelte|svx)$/,
16+
preprocess: true // required for Svelte preprocessors that affect markup, e.g. mdsvex
1617
})
1718
]
1819
})

0 commit comments

Comments
 (0)