From 658de66ebe0ddc869c410969c0277fc4f9b6c57d Mon Sep 17 00:00:00 2001 From: ubuntu Date: Sun, 7 Mar 2021 17:37:58 +0800 Subject: [PATCH] feat: using rollup pluginutils with include and exclude --- packages/playground/vue-jsx/OtherExt.tesx | 9 +++++++ .../vue-jsx/__tests__/vue-jsx.spec.ts | 1 + packages/playground/vue-jsx/main.jsx | 2 ++ packages/playground/vue-jsx/vite.config.js | 6 ++++- packages/plugin-vue-jsx/index.js | 27 ++++++++++++++++--- packages/plugin-vue-jsx/package.json | 1 + 6 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 packages/playground/vue-jsx/OtherExt.tesx diff --git a/packages/playground/vue-jsx/OtherExt.tesx b/packages/playground/vue-jsx/OtherExt.tesx new file mode 100644 index 00000000000000..7ae585a014c566 --- /dev/null +++ b/packages/playground/vue-jsx/OtherExt.tesx @@ -0,0 +1,9 @@ +import { defineComponent } from 'vue' + +const Default = defineComponent(() => { + return () => ( +

Other Ext

+ ) +}) + +export default Default diff --git a/packages/playground/vue-jsx/__tests__/vue-jsx.spec.ts b/packages/playground/vue-jsx/__tests__/vue-jsx.spec.ts index 104d3bd52642b9..b17ba40ce772be 100644 --- a/packages/playground/vue-jsx/__tests__/vue-jsx.spec.ts +++ b/packages/playground/vue-jsx/__tests__/vue-jsx.spec.ts @@ -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 () => { diff --git a/packages/playground/vue-jsx/main.jsx b/packages/playground/vue-jsx/main.jsx index b0cb108d6c7c88..f9de952320d709 100644 --- a/packages/playground/vue-jsx/main.jsx +++ b/packages/playground/vue-jsx/main.jsx @@ -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 ( @@ -9,6 +10,7 @@ function App() { + ) } diff --git a/packages/playground/vue-jsx/vite.config.js b/packages/playground/vue-jsx/vite.config.js index ba8fcd75a71c05..ae8ffc730950ea 100644 --- a/packages/playground/vue-jsx/vite.config.js +++ b/packages/playground/vue-jsx/vite.config.js @@ -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 diff --git a/packages/plugin-vue-jsx/index.js b/packages/plugin-vue-jsx/index.js index 0f15b97fa35cbb..425ed468f6634e 100644 --- a/packages/plugin-vue-jsx/index.js +++ b/packages/plugin-vue-jsx/index.js @@ -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' @@ -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 = {}) { @@ -71,8 +78,22 @@ function vueJsxPlugin(options = {}) { }, transform(code, id, ssr) { - if (/\.[jt]sx$/.test(id)) { - const plugins = [importMeta, [jsx, options]] + /** + * @type Array + */ + 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'), diff --git a/packages/plugin-vue-jsx/package.json b/packages/plugin-vue-jsx/package.json index 9bb1dbfd3582a8..4da6ca396c8671 100644 --- a/packages/plugin-vue-jsx/package.json +++ b/packages/plugin-vue-jsx/package.json @@ -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" }