Skip to content

Commit

Permalink
refactor: generate plugin name
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Aug 29, 2023
1 parent 068ebc0 commit 86c62f4
Show file tree
Hide file tree
Showing 27 changed files with 116 additions and 35 deletions.
16 changes: 16 additions & 0 deletions macros/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { defineMacro } from 'unplugin-macros'

const root = path.resolve(fileURLToPath(new URL(import.meta.url)), '../..')

function getPkgName(filePath: string) {
const relative = path.relative(root, filePath)
const [, pkgName] = relative.split(path.sep)
return pkgName
}

export const generatePluginName = defineMacro(function () {
const pkgName = getPkgName(this.id)
return `unplugin-vue-${pkgName}`
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"tsup": "^7.2.0",
"tsx": "^3.12.7",
"typescript": "~5.2.2",
"unplugin-macros": "^0.8.3",
"vite": "^4.4.9",
"vitest": "^0.34.3",
"vue": "^3.3.4",
Expand Down
3 changes: 2 additions & 1 deletion packages/better-define/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { RollupResolve, setResolveTSFileIdImpl } from '@vue-macros/api'
import { type PluginContext } from 'rollup'
import { transformBetterDefine } from './core'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export interface Options extends BaseOptions {
isProduction?: boolean
Expand All @@ -32,7 +33,7 @@ function resolveOptions(options: Options): OptionsResolved {
}
}

const name = 'unplugin-vue-better-define'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}, { framework }) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/chain-call/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
detectVueVersion,
} from '@vue-macros/common'
import { transformChainCall } from './core'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export type Options = BaseOptions
export type OptionsResolved = MarkRequired<Options, 'include' | 'version'>
Expand All @@ -22,7 +23,7 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-vue-chain-call'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/define-emit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { RollupResolve, setResolveTSFileIdImpl } from '@vue-macros/api'
import { type PluginContext } from 'rollup'
import { transformDefineEmit } from './core'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export interface Options extends BaseOptions {
isProduction?: boolean
Expand All @@ -31,7 +32,7 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-vue-define-emit'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}, { framework }) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/define-models/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
useVmodelHelperCode,
useVmodelHelperId,
} from './core/helper'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export interface Options extends BaseOptions {
/**
Expand All @@ -42,7 +43,7 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-vue-define-models'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/define-options/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
detectVueVersion,
} from '@vue-macros/common'
import { transformDefineOptions } from './core'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export type Options = BaseOptions
export type OptionsResolved = MarkRequired<Options, 'include' | 'version'>
Expand All @@ -22,7 +23,7 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-vue-define-options'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/define-prop/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { RollupResolve, setResolveTSFileIdImpl } from '@vue-macros/api'
import { type PluginContext } from 'rollup'
import { type Edition, transformDefineProp } from './core'
import { helperCode, helperId } from './core/helper'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export interface Options extends BaseOptions {
isProduction?: boolean
Expand All @@ -35,7 +36,7 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-vue-define-prop'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}, { framework }) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/define-props-refs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
detectVueVersion,
} from '@vue-macros/common'
import { transformDefinePropsRefs } from './core/index'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export { transformDefinePropsRefs } from './core'

Expand All @@ -24,7 +25,7 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-vue-define-props-refs'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
Expand Down
5 changes: 2 additions & 3 deletions packages/define-props/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
detectVueVersion,
} from '@vue-macros/common'
import { transformDefineProps } from './core'

export { transformDefineProps } from './core'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export type Options = BaseOptions
export type OptionsResolved = MarkRequired<Options, 'include' | 'version'>
Expand All @@ -24,7 +23,7 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-vue-define-props'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/define-render/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
detectVueVersion,
} from '@vue-macros/common'
import { transformDefineRender } from './core'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export type Options = BaseOptions
export type OptionsResolved = MarkRequired<Options, 'include' | 'version'>
Expand All @@ -24,8 +25,7 @@ function resolveOption(options: Options): OptionsResolved {
...options,
}
}

const name = 'unplugin-vue-define-render'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/define-slots/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
detectVueVersion,
} from '@vue-macros/common'
import { transformDefineSlots } from './core'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export type Options = BaseOptions
export type OptionsResolved = MarkRequired<Options, 'include' | 'version'>
Expand All @@ -22,7 +23,7 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-vue-define-slots'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/export-expose/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
detectVueVersion,
} from '@vue-macros/common'
import { transformExportExpose } from './core'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export type Options = BaseOptions
export type OptionsResolved = MarkRequired<Options, 'include' | 'version'>
Expand All @@ -22,7 +23,7 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-vue-export-expose'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/export-props/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
detectVueVersion,
} from '@vue-macros/common'
import { transformExportProps } from './core'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export type Options = BaseOptions
export type OptionsResolved = MarkRequired<Options, 'include' | 'version'>
Expand All @@ -22,7 +23,7 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-vue-export-props'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/export-render/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
detectVueVersion,
} from '@vue-macros/common'
import { transformExportRender } from './core'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export type Options = BaseOptions
export type OptionsResolved = MarkRequired<Options, 'include' | 'version'>
Expand All @@ -22,7 +23,7 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-vue-export-render'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/hoist-static/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
detectVueVersion,
} from '@vue-macros/common'
import { transformHoistStatic } from './core'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export type Options = BaseOptions
export type OptionsResolved = MarkRequired<Options, 'include' | 'version'>
Expand All @@ -23,7 +24,7 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-vue-hoist-static'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/jsx-directive/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
detectVueVersion,
} from '@vue-macros/common'
import { transformJsxDirective } from './core'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export type Options = BaseOptions
export type OptionsResolved = MarkRequired<Options, 'version'>
Expand All @@ -23,7 +24,7 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-jsx-vue-directive'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/macros/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ import VueSetupSFC, {
import VueShortEmits, {
type Options as OptionsShortEmits,
} from '@vue-macros/short-emits'

import { excludeDepOptimize } from './core'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export interface FeatureOptionsMap {
betterDefine: OptionsBetterDefine
Expand Down Expand Up @@ -266,8 +268,7 @@ function resolvePlugin(
return unplugin[framework!](options)
}

const name = 'unplugin-vue-macros'

const name = generatePluginName()
export default createCombinePlugin<Options | undefined>(
(userOptions = {}, meta) => {
const options = resolveOptions(userOptions)
Expand Down
5 changes: 3 additions & 2 deletions packages/named-template/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import {
QUERY_NAMED_TEMPLATE,
QUERY_TEMPLATE,
} from './core/constants'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export type Options = BaseOptions

export type OptionsResolved = MarkRequired<Options, 'include' | 'version'>

function resolveOption(options: Options): OptionsResolved {
Expand All @@ -31,7 +31,8 @@ export type TemplateContent = Record<
Record<string, string> & { [MAIN_TEMPLATE]?: string }
>

const name = 'unplugin-vue-named-template'
const name = generatePluginName()

export const PrePlugin = createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
const options = resolveOption(userOptions)
Expand Down
3 changes: 2 additions & 1 deletion packages/reactivity-transform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '@vue-macros/common'
import { shouldTransform, transform, transformVueSFC } from './core'
import { helperCode, helperId } from './core/helper'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export type Options = BaseOptions
export type OptionsResolved = MarkRequired<Options, 'include' | 'version'>
Expand All @@ -27,7 +28,7 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-reactivity-transform'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/setup-block/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
detectVueVersion,
} from '@vue-macros/common'
import { transformSetupBlock } from './core'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export interface Options extends BaseOptions {
defaultLang?: string
Expand All @@ -25,7 +26,7 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-vue-setup-block'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/setup-component/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
transformSetupComponent,
} from './core'
import { getMainModule, isSubModule } from './core/sub-module'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export type { SetupComponentContext } from './core'

Expand All @@ -42,7 +43,8 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-vue-setup-component'
const name = generatePluginName()

const PrePlugin = createUnplugin<Options | undefined, false>(
(userOptions = {}, meta) => {
const options = resolveOption(userOptions)
Expand Down
3 changes: 2 additions & 1 deletion packages/setup-sfc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
detectVueVersion,
} from '@vue-macros/common'
import { hotUpdateSetupSFC, transformSetupSFC } from './core'
import { generatePluginName } from '#macros' assert { type: 'macro' }

export type Options = BaseOptions
export type OptionsResolved = MarkRequired<Options, 'include' | 'version'>
Expand All @@ -21,7 +22,7 @@ function resolveOption(options: Options): OptionsResolved {
}
}

const name = 'unplugin-vue-setup-sfc'
const name = generatePluginName()

export default createUnplugin<Options | undefined, false>(
(userOptions = {}) => {
Expand Down
Loading

0 comments on commit 86c62f4

Please sign in to comment.