Skip to content

Commit

Permalink
feat: add plugins option for babel parser
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jun 18, 2022
1 parent edb4024 commit f3e8be2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/core/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '@babel/types'
import MagicString from 'magic-string'
import { escapeString, parseObject, styleToString } from './utils'
import type { OptionsResolved } from './options'
import type {
Expression,
JSX,
Expand All @@ -36,10 +37,10 @@ import type {
TemplateLiteral,
} from '@babel/types'

export const convert = (code: string, debug?: boolean) => {
export const convert = (code: string, { debug, plugins }: OptionsResolved) => {
const ast = parse(code, {
sourceType: 'module',
plugins: ['typescript', 'jsx'],
plugins,
})

const nodes: [JSX, Expression][] = []
Expand Down
24 changes: 24 additions & 0 deletions src/core/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { ParserPlugin } from '@babel/parser'
import type { FilterPattern } from '@rollup/pluginutils'

export interface Options {
include?: FilterPattern
exclude?: FilterPattern | undefined
debug?: boolean
/**
* Plugins for `@babel/parser`
* @default `['typescript', 'jsx']`
*/
plugins?: ParserPlugin[]
}

export type OptionsResolved = Required<Options>

export function resolveOption(options: Options): OptionsResolved {
return {
include: options.include || [/\.[jt]sx$/],
exclude: options.exclude || undefined,
debug: options.debug ?? false,
plugins: ['typescript', 'jsx'],
}
}
21 changes: 3 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
import { createUnplugin } from 'unplugin'
import { createFilter } from '@rollup/pluginutils'
import { convert } from './core/convert'
import type { FilterPattern } from '@rollup/pluginutils'
import { resolveOption } from './core/options'
import type { Options } from './core/options'

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

export interface Options {
include?: FilterPattern
exclude?: FilterPattern | undefined
debug?: boolean
}

export type OptionsResolved = Required<Options>

function resolveOption(options: Options): OptionsResolved {
return {
include: options.include || [/\.[jt]sx$/],
exclude: options.exclude || undefined,
debug: options.debug ?? false,
}
}

export default createUnplugin<Options>((options = {}) => {
const opt = resolveOption(options)
const filter = createFilter(opt.include, opt.exclude)
Expand All @@ -38,7 +23,7 @@ export default createUnplugin<Options>((options = {}) => {

transform(code) {
try {
return convert(code, opt.debug)
return convert(code, opt)
} catch (err: unknown) {
this.error(`${name} ${err}`)
}
Expand Down

1 comment on commit f3e8be2

@vercel
Copy link

@vercel vercel bot commented on f3e8be2 Jun 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.