From 2eeef19b6f542c1d86dc308d99b93bfa536a5a30 Mon Sep 17 00:00:00 2001 From: zjxxxxxxxxx <954270063@qq.com> Date: Thu, 6 Jun 2024 20:31:34 +0800 Subject: [PATCH] fix: `sfc` error --- README.md | 16 +++++++++------- src/core/index.ts | 4 ++-- src/core/transform.ts | 8 ++++---- src/core/transform_JSX.ts | 4 ++-- src/types.ts | 14 ++++++++------ test/fixtures/vue-skip/input.vue | 3 --- test/fixtures/vue-skip/options.ts | 4 ---- test/fixtures/vue-skip/output.vue | 3 --- 8 files changed, 25 insertions(+), 31 deletions(-) delete mode 100644 test/fixtures/vue-skip/input.vue delete mode 100644 test/fixtures/vue-skip/options.ts delete mode 100644 test/fixtures/vue-skip/output.vue diff --git a/README.md b/README.md index 6fe6fc9..360d70f 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Add a \_\_source prop to all Elements. - 🌈 Supports `Vue2` and `Vue3`. - 🪐 Support add to ``. -- ✨ JSX support in `.vue`, `.jsx`, `.tsx`. +- ✨ JSX support in `.vue`, `.jsx`, `.tsx`, `.mdx`. - 😃 Supports `Vite`, `Webpack`, `Rspack`, `Vue CLI`, `Rollup`, `esbuild`. > For development only @@ -213,11 +213,6 @@ The following show the default values of the configuration ```ts export interface Options { - /** @default '**\/*.{vue,jsx,tsx}' */ - include?: string | RegExp | (string | RegExp)[]; - /** @default 'node_modules/**' */ - exclude?: string | RegExp | (string | RegExp)[]; - /** * source root path * @@ -230,13 +225,20 @@ export interface Options { * @default false */ sourceMap?: boolean; - /** * Array containing the plugins that you want to enable. * * @default ['jsx', 'typescript'] */ babelParserPlugins?: ParserPlugin[]; + /** + * @default '\/**\/*.{vue,jsx,tsx}' + */ + include?: string | RegExp | (string | RegExp)[]; + /** + * @default '\/node_modules\/**\/*' + */ + exclude?: string | RegExp | (string | RegExp)[]; } ``` diff --git a/src/core/index.ts b/src/core/index.ts index 648a0fc..1018a4c 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -31,11 +31,11 @@ export const unpluginFactory: UnpluginFactory = (options = {}) => { function resolveOptions(opts: Options): ResolvedOptions { return { - include: opts.include ?? '**/*.{vue,jsx,tsx}', - exclude: opts.exclude ?? 'node_modules/**', root: opts.root ?? process.cwd(), sourceMap: opts.sourceMap ?? false, babelParserPlugins: opts.babelParserPlugins ?? [], + include: opts.include ?? '/**/*.{vue,jsx,tsx}', + exclude: opts.exclude ?? '/node_modules/**/*', }; } diff --git a/src/core/transform.ts b/src/core/transform.ts index 3eb63a7..3b1359b 100644 --- a/src/core/transform.ts +++ b/src/core/transform.ts @@ -7,16 +7,16 @@ import { transform_SFC } from './transform_SFC'; import { transform_MDX } from './transform_MDX'; import { transform_JSX } from './transform_JSX'; -const skipRE = new RegExp(` ${TRACE_ID}=['"].+:[0-9]+:[0-9]+['"]`); - export function transform(code: string, id: string, opts: ResolvedOptions) { - if (skipRE.test(code)) return; - const { root, sourceMap } = opts; let s: MagicString; const parsed = parse_ID(id, root); + if (parsed.query[TRACE_ID]) { + return; + } + if (parsed.isSfc) { transform_SFC(code, replace, opts); } else if (parsed.isMdx) { diff --git a/src/core/transform_JSX.ts b/src/core/transform_JSX.ts index 0c66c47..04a8169 100644 --- a/src/core/transform_JSX.ts +++ b/src/core/transform_JSX.ts @@ -1,6 +1,6 @@ import { type Position } from '@vue/compiler-dom'; import { traverse, types as t } from '@babel/core'; -import { parse, ParserPlugin } from '@babel/parser'; +import { parse } from '@babel/parser'; import { type ResolvedOptions } from '../types'; export function transform_JSX( @@ -24,7 +24,7 @@ export function transform_JSX( const ast = parse(code, { sourceType: 'unambiguous', - plugins: [...pluginSet] as ParserPlugin[], + plugins: Array.from(pluginSet), startLine, // babel start at 0 startColumn: startColumn - 1, diff --git a/src/types.ts b/src/types.ts index 9bc3dda..fa7b55c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,11 +1,6 @@ import { ParserPlugin } from '@babel/parser'; export interface Options { - /** @default '**\/*.{vue,jsx,tsx}' */ - include?: string | RegExp | (string | RegExp)[]; - /** @default 'node_modules/**' */ - exclude?: string | RegExp | (string | RegExp)[]; - /** * source root path * @@ -18,13 +13,20 @@ export interface Options { * @default false */ sourceMap?: boolean; - /** * Array containing the plugins that you want to enable. * * @default ['jsx', 'typescript'] */ babelParserPlugins?: ParserPlugin[]; + /** + * @default '\/**\/*.{vue,jsx,tsx}' + */ + include?: string | RegExp | (string | RegExp)[]; + /** + * @default '\/node_modules\/**\/*' + */ + exclude?: string | RegExp | (string | RegExp)[]; } export type ResolvedOptions = Required; diff --git a/test/fixtures/vue-skip/input.vue b/test/fixtures/vue-skip/input.vue deleted file mode 100644 index a1364ce..0000000 --- a/test/fixtures/vue-skip/input.vue +++ /dev/null @@ -1,3 +0,0 @@ - diff --git a/test/fixtures/vue-skip/options.ts b/test/fixtures/vue-skip/options.ts deleted file mode 100644 index 19d8e2b..0000000 --- a/test/fixtures/vue-skip/options.ts +++ /dev/null @@ -1,4 +0,0 @@ -export default { - root: process.cwd(), - sourceMap: false, -}; diff --git a/test/fixtures/vue-skip/output.vue b/test/fixtures/vue-skip/output.vue deleted file mode 100644 index a1364ce..0000000 --- a/test/fixtures/vue-skip/output.vue +++ /dev/null @@ -1,3 +0,0 @@ -