Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
3 changes: 2 additions & 1 deletion packages/babel-plugin-jsx/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export const isDirective = (src: string): boolean => src.startsWith('v-')
* @param tag string
* @returns boolean
*/
export const shouldTransformedToSlots = (tag: string) => !(tag.endsWith(FRAGMENT) || tag === KEEP_ALIVE);
// if _Fragment is already imported, it will end with number
export const shouldTransformedToSlots = (tag: string) => !(tag.match(RegExp(`^_?${FRAGMENT}\\d*$`)) || tag === KEEP_ALIVE);

/**
* Check if a Node is a component
Expand Down
13 changes: 13 additions & 0 deletions packages/babel-plugin-jsx/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
Transition,
} from 'vue';
import { shallowMount, mount, VueWrapper } from '@vue/test-utils';
import { transformSync } from '@babel/core';
import pluginJsx from '../src/index';

const patchFlagExpect = (
wrapper: VueWrapper<ComponentPublicInstance>,
Expand Down Expand Up @@ -97,6 +99,17 @@ describe('Transform JSX', () => {
expect(wrapper.html()).toBe('<div>123</div><div>456</div>');
});

test('_Fragment already imported', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packages/babel-plugin-jsx/test/snapshot.test.ts 整到这个文件里面吧

Copy link
Author

@beicause beicause Oct 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原来可以这样。

const code = `
import _Fragment from 'example'
const Root1=() => <>root1</>
const Root2=() => <Fragment>root2</Fragment>
`;
const result = transformSync(code, { plugins: [pluginJsx], babelrc: false, configFile: false });
expect(result?.code).toMatch('const Root1 = () => _createVNode(_Fragment2, null, [_createTextVNode("root1")]);');
expect(result?.code).toMatch('const Root2 = () => _createVNode(_Fragment2, null, [_createTextVNode("root2")]);');
});

test('nested component', () => {
const A = {
B: defineComponent({
Expand Down