diff --git a/README.md b/README.md index 38dfaf9..46fa0e6 100644 --- a/README.md +++ b/README.md @@ -113,10 +113,10 @@ First install `@vitejs/plugin-react`, `@vitejs/plugin-vue` and `@vitejs/plugin-v + The main project is Vue: ```js import { defineConfig } from 'vite' -// >= veaury@2.1.1 -import veauryVitePlugins from 'veaury/vite/index.js' -// if vite 6, should use `veaury/vite/esm` -// import veauryVitePlugins from 'veaury/vite/esm' + +// if mode cjs, should use `veaury/vite/cjs` +import veauryVitePlugins from 'veaury/vite/esm' + export default defineConfig({ plugins: [ @@ -141,15 +141,16 @@ export default defineConfig({ + The main project is React: ```js import { defineConfig } from 'vite' -// >= veaury@2.1.1 -import veauryVitePlugins from 'veaury/vite/index.js' + +// if mode cjs, should use `veaury/vite/cjs` +import veauryVitePlugins from 'veaury/vite/esm' export default defineConfig({ plugins: [ // Turn off react plugin // react(), // When the type of veauryVitePlugins is set to react, - // only jsx in .vue file will be parsed with vue jsx, + // only jsx in .vue files and files in a directory named "vue_app" will be parsed using vue jsx, // jsx in other files will be parsed with react jsx veauryVitePlugins({ type: 'react', @@ -164,17 +165,19 @@ export default defineConfig({ }) ``` -If you want to customize the compilation scope of vueJsx, you can configure overrides by setting type to `custom`. +If you want to customize the compilation scope of vueJsx, you can configure overrides by setting type to `custom`. +Use `vueJsxInclude` and `vueJsxExclude` to configure the file range to be parsed using vue jsx, the others will be parsed using react jsx. ```js import { defineConfig } from 'vite' -// >= veaury@2.1.1 -import veauryVitePlugins from 'veaury/vite/index.js' + +// if mode cjs, should use `veaury/vite/cjs` +import veauryVitePlugins from 'veaury/vite/esm' export default defineConfig({ plugins: [ veauryVitePlugins({ type: 'custom', - // The jsx in .vue files and in the directory named 'vue_app' will be parsed with vue jsx. + // Only jsx in .vue files and files in a directory named "react_app" will be parsed using vue jsx , others will be parsed using react jsx. vueJsxInclude: [/vue&type=script&lang\.[tj]sx$/i, /vue&type=script&setup=true&lang\.[tj]sx$/i, /[/\\]vue_app[\\/][\w\W]+\.[tj]sx$/], // vueJsxExclude: [], // Configuration of @vitejs/plugin-vue diff --git a/README_zhcn.md b/README_zhcn.md index d07a0af..6528cd3 100644 --- a/README_zhcn.md +++ b/README_zhcn.md @@ -93,10 +93,9 @@ $ npm i veaury -S + 主项目是vue: ```js import { defineConfig } from 'vite' -// >= veaury@2.1.1 -import veauryVitePlugins from 'veaury/vite/index.js' -// 如果是vite 6, 应该使用 `veaury/vite/esm` -// import veauryVitePlugins from 'veaury/vite/esm' + +// 如果是cjs模式,使用 'veaury/vite/cjs' +import veauryVitePlugins from 'veaury/vite/esm' export default defineConfig({ plugins: [ @@ -113,14 +112,15 @@ export default defineConfig({ + The main project is React: ```js import { defineConfig } from 'vite' -// >= veaury@2.1.1 -import veauryVitePlugins from 'veaury/vite/index.js' + +// 如果是cjs模式,使用 'veaury/vite/cjs' +import veauryVitePlugins from 'veaury/vite/esm' export default defineConfig({ plugins: [ // 关闭 react 插件 // react(), - // type设为react时,所有.vue文件里的jsx将以vue jsx进行编译,其他文件的jsx都是以react jsx编译 + // type设为react时,所有.vue文件中的jsx以及在名为vue_app目录里的jsx文件都将以vue jsx编译,其他的以react jsx编译 veauryVitePlugins({ type: 'react' }) @@ -131,14 +131,15 @@ export default defineConfig({ 如果想自定义vue jsx编译的范围, 可以将type设置为`custom`,然后通过设置`vueJsxInclude` 和 `vueJsxExclude`来自定义编译范围 ```js import { defineConfig } from 'vite' -// >= veaury@2.1.1 -import veauryVitePlugins from 'veaury/vite/index.js' + +// 如果是cjs模式,使用 'veaury/vite/cjs' +import veauryVitePlugins from 'veaury/vite/esm' export default defineConfig({ plugins: [ veauryVitePlugins({ type: 'custom', - // 所有.vue文件中的jsx以及在名为vue_app目录里的jsx文件都将以vue jsx编译 + // 所有.vue文件中的jsx以及在名为vue_app目录里的jsx文件都将以vue jsx编译,其他的以react jsx编译 vueJsxInclude: [/vue&type=script&lang\.[tj]sx$/i, /vue&type=script&setup=true&lang\.[tj]sx$/i, /[/\\]vue_app[\\/][\w\W]+\.[tj]sx$/], // vueJsxExclude: [] }) diff --git a/package.json b/package.json index 15ee002..d373beb 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "veaury", "private": false, - "version": "2.4.2", + "version": "2.4.3", "description": "Use React in Vue3 and Vue3 in React, And as perfect as possible!", "main": "dist/veaury.umd.js", "module": "dist/veaury.esm.js", diff --git a/vite/cjs/index.js b/vite/cjs/index.js new file mode 100644 index 0000000..0c47cf4 --- /dev/null +++ b/vite/cjs/index.js @@ -0,0 +1,70 @@ +const vue = require('@vitejs/plugin-vue') +const react = require('@vitejs/plugin-react') +const vueJsx = require('@vitejs/plugin-vue-jsx') +// const requireTransform = require('vite-plugin-require-transform').default +// function ReactDOMTransformPlugin() { +// return { +// async resolveId(source, importer, options) { +// if (source.match(/react-dom\/client/)) { +// const resolution = await this.resolve(source, importer, { skipSelf: true, ...options }) +// if (!resolution) { +// return { id: 'veaury-fake-react-dom-client', moduleSideEffects: true } +// } +// } +// }, +// load(id) { +// if (id === 'veaury-fake-react-dom-client') { +// return `export * from 'react-dom'; export {default} from 'react-dom';` +// } +// } +// } +// } + +function veauryVitePlugins({type, vueJsxInclude, vueJsxExclude, vueOptions = {}, vueJsxOptions: initVueJsxOptions = {}, reactOptions = {}}) { + + let vueJsxOptions = {...initVueJsxOptions} + if (type === 'react') { + vueJsxOptions.include = [/vue&type=script&lang\.[tj]sx$/i, /vue&type=script&setup=true&lang\.[tj]sx$/i, /[/\\]vue_app[\\/][\w\W]+\.[tj]sx$/] + } + if (type === 'vue') { + vueJsxOptions.exclude = [/[/\\]react_app[\\/$]+/] + } + if (type === 'custom') { + if (vueJsxInclude) { + vueJsxOptions.include = vueJsxInclude + } + if (vueJsxExclude) { + vueJsxOptions.exclude = vueJsxExclude + } + } + + return [ + // ReactDOMTransformPlugin(), + // requireTransform({ + + // fileRegex: /veaury/ + // }), + vue(vueOptions), + // Make vueJsx plugin run time earlier + { + ...vueJsx(vueJsxOptions), + enforce: 'pre' + }, + // recover esbuild include + { + config(){ + return { + esbuild: { + include: /\.[jt]sx?$/ + } + } + } + }, + react({ + jsxImportSource: 'react', + ...reactOptions + }) + ] +} + +module.exports = veauryVitePlugins diff --git a/vite/index.js b/vite/index.js index 6d1cd46..e78bb13 100644 --- a/vite/index.js +++ b/vite/index.js @@ -1,70 +1,3 @@ -const vue = require('@vitejs/plugin-vue') -const react = require('@vitejs/plugin-react') -const vueJsx = require('@vitejs/plugin-vue-jsx') -// const requireTransform = require('vite-plugin-require-transform').default -// function ReactDOMTransformPlugin() { -// return { -// async resolveId(source, importer, options) { -// if (source.match(/react-dom\/client/)) { -// const resolution = await this.resolve(source, importer, { skipSelf: true, ...options }) -// if (!resolution) { -// return { id: 'veaury-fake-react-dom-client', moduleSideEffects: true } -// } -// } -// }, -// load(id) { -// if (id === 'veaury-fake-react-dom-client') { -// return `export * from 'react-dom'; export {default} from 'react-dom';` -// } -// } -// } -// } - -function veauryVitePlugins({type, vueJsxInclude, vueJsxExclude, vueOptions = {}, vueJsxOptions: initVueJsxOptions = {}, reactOptions = {}}) { - - let vueJsxOptions = {...initVueJsxOptions} - if (type === 'react') { - vueJsxOptions.include = [/vue&type=script&lang\.[tj]sx$/i, /vue&type=script&setup=true&lang\.[tj]sx$/i, /[/\\]vue_app[\\/][\w\W]+\.[tj]sx$/] - } - if (type === 'vue') { - vueJsxOptions.exclude = [/[/\\]react_app[\\/$]+/] - } - if (type === 'custom') { - if (vueJsxInclude) { - vueJsxOptions.include = vueJsxInclude - } - if (vueJsxExclude) { - vueJsxOptions.exclude = vueJsxExclude - } - } - - return [ - // ReactDOMTransformPlugin(), - // requireTransform({ - - // fileRegex: /veaury/ - // }), - vue(vueOptions), - // Make vueJsx plugin run time earlier - { - ...vueJsx(vueJsxOptions), - enforce: 'pre' - }, - // recover esbuild include - { - config(){ - return { - esbuild: { - include: /\.[jt]sx?$/ - } - } - } - }, - react({ - jsxImportSource: 'react', - ...reactOptions - }) - ] -} +const veauryVitePlugins = require('./cjs') module.exports = veauryVitePlugins