Skip to content

Commit 0f71911

Browse files
committed
feat(vue-jsx): add defineComponentName option
1 parent 37c9073 commit 0f71911

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

Diff for: packages/plugin-vue-jsx/README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,20 @@ A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patt
3535

3636
> See [@vue/babel-plugin-jsx](https://github.com/vuejs/jsx-next) for other options.
3737
38+
### defineComponentName
39+
40+
Type: `string[]`
41+
42+
Default: `['defineComponent']`
43+
44+
The name of the function to be used for defining components. This is useful when you have a custom `defineComponent` function.
45+
3846
## HMR Detection
3947

4048
This plugin supports HMR of Vue JSX components. The detection requirements are:
4149

4250
- The component must be exported.
43-
- The component must be declared by calling `defineComponent` via a root-level statement, either variable declaration or export declaration.
51+
- The component must be declared by calling `defineComponent` or the name specified in `defineComponentName` via a root-level statement, either variable declaration or export declaration.
4452

4553
### Supported patterns
4654

Diff for: packages/plugin-vue-jsx/src/index.ts

+31-9
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ function vueJsxPlugin(options: Options = {}): Plugin {
3838
let needHmr = false
3939
let needSourceMap = true
4040

41-
const { include, exclude, babelPlugins = [], ...babelPluginOptions } = options
41+
const {
42+
include,
43+
exclude,
44+
babelPlugins = [],
45+
defineComponentName = ['defineComponent'],
46+
...babelPluginOptions
47+
} = options
4248
const filter = createFilter(include || /\.[jt]sx$/, exclude)
4349

4450
return {
@@ -103,7 +109,9 @@ function vueJsxPlugin(options: Options = {}): Plugin {
103109
visitor: {
104110
CallExpression: {
105111
enter(_path: babel.NodePath<CallExpression>) {
106-
if (isDefineComponentCall(_path.node)) {
112+
if (
113+
isDefineComponentCall(_path.node, defineComponentName)
114+
) {
107115
const callee = _path.node.callee as Identifier
108116
callee.name = `/* @__PURE__ */ ${callee.name}`
109117
}
@@ -144,7 +152,7 @@ function vueJsxPlugin(options: Options = {}): Plugin {
144152

145153
for (const node of result.ast!.program.body) {
146154
if (node.type === 'VariableDeclaration') {
147-
const names = parseComponentDecls(node)
155+
const names = parseComponentDecls(node, defineComponentName)
148156
if (names.length) {
149157
declaredComponents.push(...names)
150158
}
@@ -156,7 +164,10 @@ function vueJsxPlugin(options: Options = {}): Plugin {
156164
node.declaration.type === 'VariableDeclaration'
157165
) {
158166
hotComponents.push(
159-
...parseComponentDecls(node.declaration).map((name) => ({
167+
...parseComponentDecls(
168+
node.declaration,
169+
defineComponentName,
170+
).map((name) => ({
160171
local: name,
161172
exported: name,
162173
id: getHash(id + name),
@@ -194,7 +205,9 @@ function vueJsxPlugin(options: Options = {}): Plugin {
194205
id: getHash(id + 'default'),
195206
})
196207
}
197-
} else if (isDefineComponentCall(node.declaration)) {
208+
} else if (
209+
isDefineComponentCall(node.declaration, defineComponentName)
210+
) {
198211
hasDefault = true
199212
hotComponents.push({
200213
local: '__default__',
@@ -254,22 +267,31 @@ function vueJsxPlugin(options: Options = {}): Plugin {
254267
}
255268
}
256269

257-
function parseComponentDecls(node: types.VariableDeclaration) {
270+
function parseComponentDecls(
271+
node: types.VariableDeclaration,
272+
fnNames: string[],
273+
) {
258274
const names = []
259275
for (const decl of node.declarations) {
260-
if (decl.id.type === 'Identifier' && isDefineComponentCall(decl.init)) {
276+
if (
277+
decl.id.type === 'Identifier' &&
278+
isDefineComponentCall(decl.init, fnNames)
279+
) {
261280
names.push(decl.id.name)
262281
}
263282
}
264283
return names
265284
}
266285

267-
function isDefineComponentCall(node?: types.Node | null) {
286+
function isDefineComponentCall(
287+
node: types.Node | null | undefined,
288+
names: string[],
289+
) {
268290
return (
269291
node &&
270292
node.type === 'CallExpression' &&
271293
node.callee.type === 'Identifier' &&
272-
node.callee.name === 'defineComponent'
294+
names.includes(node.callee.name)
273295
)
274296
}
275297

Diff for: packages/plugin-vue-jsx/src/types.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ export interface FilterOptions {
66
exclude?: FilterPattern
77
}
88

9-
export type Options = VueJSXPluginOptions &
10-
FilterOptions & { babelPlugins?: any[] }
9+
export interface Options extends VueJSXPluginOptions, FilterOptions {
10+
babelPlugins?: any[]
11+
/** @default ['defineComponent'] */
12+
defineComponentName?: string[]
13+
}

0 commit comments

Comments
 (0)