File tree Expand file tree Collapse file tree 3 files changed +30
-20
lines changed Expand file tree Collapse file tree 3 files changed +30
-20
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import {
1515} from '@babel/types'
1616import MagicString from 'magic-string'
1717import { escapeString , parseObject , styleToString } from './utils'
18+ import type { OptionsResolved } from './options'
1819import 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 ] [ ] = [ ]
Original file line number Diff line number Diff line change 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 || [ / \. [ j t ] s x $ / ] ,
20+ exclude : options . exclude || undefined ,
21+ debug : options . debug ?? false ,
22+ plugins : [ 'typescript' , 'jsx' ] ,
23+ }
24+ }
Original file line number Diff line number Diff line change 11import { createUnplugin } from 'unplugin'
22import { createFilter } from '@rollup/pluginutils'
33import { 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
67declare 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 || [ / \. [ j t ] s x $ / ] ,
21- exclude : options . exclude || undefined ,
22- debug : options . debug ?? false ,
23- }
24- }
25-
2611export 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 }
You can’t perform that action at this time.
0 commit comments