Skip to content

Commit fbecf1e

Browse files
authored
feat: include/exclude options for vue-jsx plugin (#1953)
1 parent 344d77e commit fbecf1e

File tree

6 files changed

+32
-4
lines changed

6 files changed

+32
-4
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineComponent } from 'vue'
2+
3+
const Default = defineComponent(() => {
4+
return () => (
5+
<p class="other-ext">Other Ext</p>
6+
)
7+
})
8+
9+
export default Default

packages/playground/vue-jsx/__tests__/vue-jsx.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test('should render', async () => {
55
expect(await page.textContent('.named-specifier')).toMatch('1')
66
expect(await page.textContent('.default')).toMatch('2')
77
expect(await page.textContent('.default-tsx')).toMatch('3')
8+
expect(await page.textContent('.other-ext')).toMatch('Other Ext')
89
})
910

1011
test('should update', async () => {

packages/playground/vue-jsx/main.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createApp } from 'vue'
22
import { Named, NamedSpec, default as Default } from './Comps'
33
import { default as TsxDefault } from './Comp'
4+
import OtherExt from './OtherExt.tesx'
45

56
function App() {
67
return (
@@ -9,6 +10,7 @@ function App() {
910
<NamedSpec />
1011
<Default />
1112
<TsxDefault />
13+
<OtherExt />
1214
</>
1315
)
1416
}

packages/playground/vue-jsx/vite.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ const vueJsxPlugin = require('@vitejs/plugin-vue-jsx')
44
* @type {import('vite').UserConfig}
55
*/
66
module.exports = {
7-
plugins: [vueJsxPlugin()],
7+
plugins: [
8+
vueJsxPlugin({
9+
include: [/\.tesx$/, /\.[jt]sx$/]
10+
})
11+
],
812
build: {
913
// to make tests faster
1014
minify: false

packages/plugin-vue-jsx/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const babel = require('@babel/core')
33
const jsx = require('@vue/babel-plugin-jsx')
44
const importMeta = require('@babel/plugin-syntax-import-meta')
5+
const { createFilter } = require('@rollup/pluginutils')
56
const hash = require('hash-sum')
67

78
const ssrRegisterHelperId = '/__vue-jsx-ssr-register-helper'
@@ -28,7 +29,13 @@ function ssrRegisterHelper(comp, filename) {
2829
}
2930

3031
/**
31-
* @param {import('@vue/babel-plugin-jsx').VueJSXPluginOptions} options
32+
* @typedef { import('@rollup/pluginutils').FilterPattern} FilterPattern
33+
* @typedef { { include?: FilterPattern, exclude?: FilterPattern } } CommonOtions
34+
*/
35+
36+
/**
37+
*
38+
* @param {import('@vue/babel-plugin-jsx').VueJSXPluginOptions & CommonOtions} options
3239
* @returns {import('vite').Plugin}
3340
*/
3441
function vueJsxPlugin(options = {}) {
@@ -71,8 +78,12 @@ function vueJsxPlugin(options = {}) {
7178
},
7279

7380
transform(code, id, ssr) {
74-
if (/\.[jt]sx$/.test(id)) {
75-
const plugins = [importMeta, [jsx, options]]
81+
const { include, exclude, ...babelPluginOptions } = options
82+
83+
const filter = createFilter(include || /\.[jt]sx$/, exclude)
84+
85+
if (filter(id)) {
86+
const plugins = [importMeta, [jsx, babelPluginOptions]]
7687
if (id.endsWith('.tsx')) {
7788
plugins.push([
7889
require('@babel/plugin-transform-typescript'),

packages/plugin-vue-jsx/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@babel/core": "^7.12.10",
3030
"@babel/plugin-syntax-import-meta": "^7.10.4",
3131
"@babel/plugin-transform-typescript": "^7.12.1",
32+
"@rollup/pluginutils": "^4.1.0",
3233
"@vue/babel-plugin-jsx": "^1.0.3",
3334
"hash-sum": "^2.0.0"
3435
}

0 commit comments

Comments
 (0)