Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add extensions option to vue-jsx plugin #1953

Merged
merged 2 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/playground/vue-jsx/OtherExt.tesx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineComponent } from 'vue'

const Default = defineComponent(() => {
return () => (
<p class="other-ext">Other Ext</p>
)
})

export default Default
1 change: 1 addition & 0 deletions packages/playground/vue-jsx/__tests__/vue-jsx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ test('should render', async () => {
expect(await page.textContent('.named-specifier')).toMatch('1')
expect(await page.textContent('.default')).toMatch('2')
expect(await page.textContent('.default-tsx')).toMatch('3')
expect(await page.textContent('.other-ext')).toMatch('Other Ext')
})

test('should update', async () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/playground/vue-jsx/main.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createApp } from 'vue'
import { Named, NamedSpec, default as Default } from './Comps'
import { default as TsxDefault } from './Comp'
import OtherExt from './OtherExt.tesx'

function App() {
return (
Expand All @@ -9,6 +10,7 @@ function App() {
<NamedSpec />
<Default />
<TsxDefault />
<OtherExt />
</>
)
}
Expand Down
6 changes: 5 additions & 1 deletion packages/playground/vue-jsx/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const vueJsxPlugin = require('@vitejs/plugin-vue-jsx')
* @type {import('vite').UserConfig}
*/
module.exports = {
plugins: [vueJsxPlugin()],
plugins: [
vueJsxPlugin({
include: ['**/*.tesx']
})
],
build: {
// to make tests faster
minify: false
Expand Down
27 changes: 24 additions & 3 deletions packages/plugin-vue-jsx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const babel = require('@babel/core')
const jsx = require('@vue/babel-plugin-jsx')
const importMeta = require('@babel/plugin-syntax-import-meta')
const { createFilter } = require('@rollup/pluginutils')
const hash = require('hash-sum')

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

/**
* @param {import('@vue/babel-plugin-jsx').VueJSXPluginOptions} options
* @typedef { import('@rollup/pluginutils').FilterPattern} FilterPattern
* @typedef { { include?: FilterPattern, exclude?: FilterPattern } } CommonOtions
*/

/**
*
* @param {import('@vue/babel-plugin-jsx').VueJSXPluginOptions & CommonOtions} options
* @returns {import('vite').Plugin}
*/
function vueJsxPlugin(options = {}) {
Expand Down Expand Up @@ -71,8 +78,22 @@ function vueJsxPlugin(options = {}) {
},

transform(code, id, ssr) {
if (/\.[jt]sx$/.test(id)) {
const plugins = [importMeta, [jsx, options]]
/**
* @type Array<string | RegExp>
*/
const defaultInclude = ['**/*.jsx', '**/*.tsx']
const { include, exclude, ...babelPluginOptions } = options

if (Array.isArray(include)) {
defaultInclude.push(...include)
} else if (include) {
defaultInclude.push(include)
}
antfu marked this conversation as resolved.
Show resolved Hide resolved

const filter = createFilter(defaultInclude, exclude)

if (filter(id)) {
const plugins = [importMeta, [jsx, babelPluginOptions]]
if (id.endsWith('.tsx')) {
plugins.push([
require('@babel/plugin-transform-typescript'),
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-vue-jsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@babel/core": "^7.12.10",
"@babel/plugin-syntax-import-meta": "^7.10.4",
"@babel/plugin-transform-typescript": "^7.12.1",
"@rollup/pluginutils": "^4.1.0",
"@vue/babel-plugin-jsx": "^1.0.3",
"hash-sum": "^2.0.0"
}
Expand Down