Skip to content

Commit

Permalink
Support mdsvex
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Oct 19, 2022
1 parent 98aaaeb commit 571d943
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/src/pages/docs/packages/whyframe-svelte.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patt
- **Type:** `string | RegExp | (string | RegExp)[]`

A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patterns, which specifies the files to be ignored by the plugin.

### preprocess

- **Type:** `boolean`
- **Default:** `false`

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).
1 change: 1 addition & 0 deletions packages/svelte/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Bump dependencies
- Refactor files for unit tests
- Add `preprocess` option for `mdsvex` support

## 0.1.3 (2022-10-03)

Expand Down
1 change: 1 addition & 0 deletions packages/svelte/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { FilterPattern, Plugin } from 'vite'
export interface Options {
include?: FilterPattern
exclude?: FilterPattern
preprocess?: boolean
}

export declare function whyframeSvelte(options?: Options): Plugin
19 changes: 19 additions & 0 deletions packages/svelte/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ import { transform } from './shared.js'
export function whyframeSvelte(options) {
/** @type {import('@whyframe/core').Api} */
let api
/** @type {any} */
let ctx

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

/** @type {import('vite').Plugin} */
const plugin = {
name: 'whyframe:svelte',
enforce: 'pre',
buildStart() {
ctx = this
},
configResolved(c) {
api = c.plugins.find((p) => p.name === 'whyframe:api')?.api
if (!api) {
Expand All @@ -40,5 +45,19 @@ export function whyframeSvelte(options) {
}
}

if (options?.preprocess) {
// convert to vite-plugin-svelte's api so typescript etc are already preprocessed
const _transform = plugin.transform
delete plugin.transform
plugin.api = {
sveltePreprocess: {
markup({ content, filename }) {
// @ts-expect-error
return _transform.apply(ctx, [content, filename])
}
}
}
}

return plugin
}
6 changes: 5 additions & 1 deletion playground/sveltekit/src/routes/docs/+page.svx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Markdown

Ahoy
<iframe data-why title="mdsvex">Works in mdsvex too!</iframe>

Simply:

```html
<iframe data-why title="mdsvex">Works in mdsvex too!</iframe>
```
3 changes: 2 additions & 1 deletion playground/sveltekit/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export default defineConfig({
components: [{ name: 'Story', showSource: true }]
}),
whyframeSvelte({
include: /\.(svelte|svx)$/
include: /\.(svelte|svx)$/,
preprocess: true // required for Svelte preprocessors that affect markup, e.g. mdsvex
})
]
})

0 comments on commit 571d943

Please sign in to comment.