Skip to content

Commit

Permalink
feat: using rollup pluginutils with include and exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
ubuntu committed Mar 7, 2021
1 parent a8b4543 commit 658de66
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 4 deletions.
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)
}

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

0 comments on commit 658de66

Please sign in to comment.