Skip to content

Commit f3e8be2

Browse files
committed
feat: add plugins option for babel parser
1 parent edb4024 commit f3e8be2

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

src/core/convert.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from '@babel/types'
1616
import MagicString from 'magic-string'
1717
import { escapeString, parseObject, styleToString } from './utils'
18+
import type { OptionsResolved } from './options'
1819
import type {
1920
Expression,
2021
JSX,
@@ -36,10 +37,10 @@ import type {
3637
TemplateLiteral,
3738
} from '@babel/types'
3839

39-
export const convert = (code: string, debug?: boolean) => {
40+
export const convert = (code: string, { debug, plugins }: OptionsResolved) => {
4041
const ast = parse(code, {
4142
sourceType: 'module',
42-
plugins: ['typescript', 'jsx'],
43+
plugins,
4344
})
4445

4546
const nodes: [JSX, Expression][] = []

src/core/options.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { ParserPlugin } from '@babel/parser'
2+
import type { FilterPattern } from '@rollup/pluginutils'
3+
4+
export interface Options {
5+
include?: FilterPattern
6+
exclude?: FilterPattern | undefined
7+
debug?: boolean
8+
/**
9+
* Plugins for `@babel/parser`
10+
* @default `['typescript', 'jsx']`
11+
*/
12+
plugins?: ParserPlugin[]
13+
}
14+
15+
export type OptionsResolved = Required<Options>
16+
17+
export function resolveOption(options: Options): OptionsResolved {
18+
return {
19+
include: options.include || [/\.[jt]sx$/],
20+
exclude: options.exclude || undefined,
21+
debug: options.debug ?? false,
22+
plugins: ['typescript', 'jsx'],
23+
}
24+
}

src/index.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
import { createUnplugin } from 'unplugin'
22
import { createFilter } from '@rollup/pluginutils'
33
import { convert } from './core/convert'
4-
import type { FilterPattern } from '@rollup/pluginutils'
4+
import { resolveOption } from './core/options'
5+
import type { Options } from './core/options'
56

67
declare global {
78
const jsxToString: (element: JSX.Element) => string
89
}
910

10-
export interface Options {
11-
include?: FilterPattern
12-
exclude?: FilterPattern | undefined
13-
debug?: boolean
14-
}
15-
16-
export type OptionsResolved = Required<Options>
17-
18-
function resolveOption(options: Options): OptionsResolved {
19-
return {
20-
include: options.include || [/\.[jt]sx$/],
21-
exclude: options.exclude || undefined,
22-
debug: options.debug ?? false,
23-
}
24-
}
25-
2611
export default createUnplugin<Options>((options = {}) => {
2712
const opt = resolveOption(options)
2813
const filter = createFilter(opt.include, opt.exclude)
@@ -38,7 +23,7 @@ export default createUnplugin<Options>((options = {}) => {
3823

3924
transform(code) {
4025
try {
41-
return convert(code, opt.debug)
26+
return convert(code, opt)
4227
} catch (err: unknown) {
4328
this.error(`${name} ${err}`)
4429
}

0 commit comments

Comments
 (0)