Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed May 5, 2023
1 parent 933db51 commit 98fd957
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 45 deletions.
1 change: 1 addition & 0 deletions packages/v-scope/src/api.ts
@@ -0,0 +1 @@
export * from './core'
12 changes: 3 additions & 9 deletions packages/v-scope/src/core/index.ts
Expand Up @@ -20,16 +20,10 @@ import {
} from '@babel/types'
import { SCOPE_DIRECTIVE } from './constant'

export function preTransform(code: string, id: string) {
const s = new MagicString(code)
export * from './constant'

return getTransformResult(s, id)
}

export function postTransform(code: string, id: string) {
if (!code.includes(SCOPE_DIRECTIVE)) {
return
}
export function transformVScope(code: string, id: string) {
if (!code.includes(SCOPE_DIRECTIVE)) return

const lang = getLang(id)
const program = babelParse(code, lang)
Expand Down
41 changes: 7 additions & 34 deletions packages/v-scope/src/index.ts
Expand Up @@ -2,12 +2,13 @@ import { createUnplugin } from 'unplugin'
import {
type BaseOptions,
type MarkRequired,
REGEX_SETUP_SFC,
REGEX_VUE_SFC,
REGEX_VUE_SUB,
createFilter,
detectVueVersion,
} from '@vue-macros/common'
import { postTransform, preTransform } from './core'
import { transformVScope } from './core'

export type Options = BaseOptions

Expand All @@ -16,35 +17,15 @@ export type OptionsResolved = MarkRequired<Options, 'include' | 'version'>
function resolveOption(options: Options): OptionsResolved {
const version = options.version || detectVueVersion()
return {
include: [REGEX_VUE_SFC, REGEX_VUE_SUB],
include: [REGEX_VUE_SFC, REGEX_SETUP_SFC, REGEX_VUE_SUB],
...options,
version,
}
}

const name = 'unplugin-vue-v-scope'

export const PrePlugin = createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
const options = resolveOption(userOptions)
const filter = createFilter(options)

return {
name: `${name}-pre`,
enforce: 'pre',

transformInclude(id) {
return filter(id)
},

transform(code, id) {
return preTransform(code, id)
},
}
}
)

export const PostPlugin = createUnplugin<Options | undefined, false>(
export default createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
const options = resolveOption(userOptions)
const filter = createFilter(options)
Expand All @@ -54,31 +35,23 @@ export const PostPlugin = createUnplugin<Options | undefined, false>(
}

return {
name: `${name}-post`,
name,
enforce: 'post',

transformInclude,
transform(code, id) {
return postTransform(code, id)
return transformVScope(code, id)
},

rollup: {
transform: {
order: 'post',
handler(code, id) {
if (!transformInclude(id)) return
return postTransform(code, id)
return transformVScope(code, id)
},
},
},
}
}
)

const plugin = createUnplugin<Options | undefined, true>(
(userOptions = {}, meta) => {
return [PrePlugin.raw(userOptions, meta), PostPlugin.raw(userOptions, meta)]
}
)

export default plugin
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 98fd957

Please sign in to comment.