From dca943550b1468c1cc840963847cffc142d84589 Mon Sep 17 00:00:00 2001 From: jaw <2135326728@qq.com> Date: Tue, 13 Jun 2023 12:26:24 +0800 Subject: [PATCH 01/12] fix: avoid HTML elements as component --- package.json | 1 + pnpm-lock.yaml | 12 +++++----- src/core/transformScriptSetup.ts | 3 +++ test/__snapshots__/transform.test.ts.snap | 27 +++++++++++++++++++++++ test/fixtures/HtmlTag.vue | 13 +++++++++++ 5 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/HtmlTag.vue diff --git a/package.json b/package.json index 996dbf7..6017e8e 100644 --- a/package.json +++ b/package.json @@ -93,6 +93,7 @@ "@vue/reactivity-transform": "^3.3.4", "@vue/shared": "^3.3.4", "defu": "^6.1.2", + "html-tags": "^3.3.1", "magic-string": "^0.30.0", "unplugin": "^1.3.1" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5a6cad3..44447f9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,4 +1,4 @@ -lockfileVersion: '6.1' +lockfileVersion: '6.0' settings: autoInstallPeers: true @@ -47,6 +47,9 @@ importers: defu: specifier: ^6.1.2 version: 6.1.2 + html-tags: + specifier: ^3.3.1 + version: 3.3.1 magic-string: specifier: ^0.30.0 version: 0.30.0 @@ -2400,7 +2403,7 @@ packages: '@babel/types': 7.22.4 '@vue/babel-helper-vue-transform-on': 1.0.2 camelcase: 6.2.0 - html-tags: 3.1.0 + html-tags: 3.3.1 svg-tags: 1.0.0 transitivePeerDependencies: - '@babel/core' @@ -5999,10 +6002,9 @@ packages: engines: {node: '>=4'} dev: true - /html-tags@3.1.0: - resolution: {integrity: sha512-1qYz89hW3lFDEazhjW0yVAV87lw8lVkrJocr72XmBkMKsoSVJCQx3W8BXsC7hO2qAt8BoVjYjtAcZ9perqGnNg==} + /html-tags@3.3.1: + resolution: {integrity: sha512-ztqyC3kLto0e9WbNp0aeP+M3kTt+nbaIveGmUxAtZa+8iFgKLUOD4YKM5j+f3QD89bra7UeumolZHKuOXnTmeQ==} engines: {node: '>=8'} - dev: true /html-webpack-plugin@5.5.0(webpack@5.69.1): resolution: {integrity: sha512-sy88PC2cRTVxvETRgUHFrL4No3UxvcH8G1NepGhqaTT+GXN2kTamqasot0inS5hXeg1cMbFDt27zzo9p35lZVw==} diff --git a/src/core/transformScriptSetup.ts b/src/core/transformScriptSetup.ts index c1da0d9..925cbbb 100644 --- a/src/core/transformScriptSetup.ts +++ b/src/core/transformScriptSetup.ts @@ -1,6 +1,7 @@ import { capitalize } from '@vue/shared' import type { Node, ObjectExpression, Statement } from '@babel/types' import { notNullish, partition, uniq } from '@antfu/utils' +import htmlTags from 'html-tags' import type { ParsedSFC, ScriptSetupTransformOptions } from '../types' import { applyMacros } from './macros' import { getIdentifierDeclarations } from './identifiers' @@ -54,7 +55,9 @@ export function transformScriptSetup( return t.objectProperty(id, id, false, true) }) + const pascalizeHtmlTags = htmlTags.map(pascalize) const components = Array.from(template.components) + .filter(component => !pascalizeHtmlTags.includes(component)) .map( component => declarationArray.find(declare => declare === component) diff --git a/test/__snapshots__/transform.test.ts.snap b/test/__snapshots__/transform.test.ts.snap index a0dc316..96d3b54 100644 --- a/test/__snapshots__/transform.test.ts.snap +++ b/test/__snapshots__/transform.test.ts.snap @@ -330,6 +330,33 @@ export default __sfc_main; " `; +exports[`transform > fixtures > test/fixtures/HtmlTag.vue 1`] = ` +" + + +" +`; + exports[`transform > fixtures > test/fixtures/JSLongComment.vue 1`] = ` " + + From 8d18abd6d6e8edb3c82eef92670a4e313173e2f2 Mon Sep 17 00:00:00 2001 From: jaw <2135326728@qq.com> Date: Tue, 13 Jun 2023 16:49:19 +0800 Subject: [PATCH 02/12] test: add more test cases --- package.json | 1 - pnpm-lock.yaml | 6 ++-- src/core/transformScriptSetup.ts | 5 ++- test/__snapshots__/transform.test.ts.snap | 43 +++++++++++++++++++++-- test/fixtures/HtmlTag.vue | 11 ++++-- test/fixtures/HtmlTag2.vue | 20 +++++++++++ test/identifiers.test.ts | 2 ++ 7 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/HtmlTag2.vue diff --git a/package.json b/package.json index 6017e8e..996dbf7 100644 --- a/package.json +++ b/package.json @@ -93,7 +93,6 @@ "@vue/reactivity-transform": "^3.3.4", "@vue/shared": "^3.3.4", "defu": "^6.1.2", - "html-tags": "^3.3.1", "magic-string": "^0.30.0", "unplugin": "^1.3.1" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 44447f9..baf156d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,4 +1,4 @@ -lockfileVersion: '6.0' +lockfileVersion: '6.1' settings: autoInstallPeers: true @@ -47,9 +47,6 @@ importers: defu: specifier: ^6.1.2 version: 6.1.2 - html-tags: - specifier: ^3.3.1 - version: 3.3.1 magic-string: specifier: ^0.30.0 version: 0.30.0 @@ -6005,6 +6002,7 @@ packages: /html-tags@3.3.1: resolution: {integrity: sha512-ztqyC3kLto0e9WbNp0aeP+M3kTt+nbaIveGmUxAtZa+8iFgKLUOD4YKM5j+f3QD89bra7UeumolZHKuOXnTmeQ==} engines: {node: '>=8'} + dev: true /html-webpack-plugin@5.5.0(webpack@5.69.1): resolution: {integrity: sha512-sy88PC2cRTVxvETRgUHFrL4No3UxvcH8G1NepGhqaTT+GXN2kTamqasot0inS5hXeg1cMbFDt27zzo9p35lZVw==} diff --git a/src/core/transformScriptSetup.ts b/src/core/transformScriptSetup.ts index 925cbbb..7fb5576 100644 --- a/src/core/transformScriptSetup.ts +++ b/src/core/transformScriptSetup.ts @@ -1,7 +1,7 @@ import { capitalize } from '@vue/shared' import type { Node, ObjectExpression, Statement } from '@babel/types' import { notNullish, partition, uniq } from '@antfu/utils' -import htmlTags from 'html-tags' +import { parserOptions } from '@vue/compiler-dom' import type { ParsedSFC, ScriptSetupTransformOptions } from '../types' import { applyMacros } from './macros' import { getIdentifierDeclarations } from './identifiers' @@ -55,9 +55,8 @@ export function transformScriptSetup( return t.objectProperty(id, id, false, true) }) - const pascalizeHtmlTags = htmlTags.map(pascalize) const components = Array.from(template.components) - .filter(component => !pascalizeHtmlTags.includes(component)) + .filter(component => !parserOptions.isNativeTag!(component.toLowerCase())) .map( component => declarationArray.find(declare => declare === component) diff --git a/test/__snapshots__/transform.test.ts.snap b/test/__snapshots__/transform.test.ts.snap index 96d3b54..ba1412c 100644 --- a/test/__snapshots__/transform.test.ts.snap +++ b/test/__snapshots__/transform.test.ts.snap @@ -337,9 +337,14 @@ import { ref } from '@vue/composition-api'; const __sfc_main = {}; __sfc_main.setup = (__props, __ctx) => { let p = \\"hello word\\"; - let div = ref(\\"hello word\\"); + let Div = ref(\\"hello word\\"); + let h3 = 'test'; + let H3 = ''; return { - p + p, + Div, + h3, + H3 }; }; __sfc_main.components = Object.assign({ @@ -351,7 +356,41 @@ export default __sfc_main; +" +`; + +exports[`transform > fixtures > test/fixtures/HtmlTag2.vue 1`] = ` +" + + " diff --git a/test/fixtures/HtmlTag.vue b/test/fixtures/HtmlTag.vue index f47864a..df8ec48 100644 --- a/test/fixtures/HtmlTag.vue +++ b/test/fixtures/HtmlTag.vue @@ -1,13 +1,20 @@ diff --git a/test/fixtures/HtmlTag2.vue b/test/fixtures/HtmlTag2.vue new file mode 100644 index 0000000..455cc3d --- /dev/null +++ b/test/fixtures/HtmlTag2.vue @@ -0,0 +1,20 @@ + + + diff --git a/test/identifiers.test.ts b/test/identifiers.test.ts index 61e3047..d6973c4 100644 --- a/test/identifiers.test.ts +++ b/test/identifiers.test.ts @@ -9,6 +9,8 @@ describe('identifiers', () => { ['var a = 1', ['a']], ['import { foo, t as bar } from "z"', ['foo', 'bar']], ['import foo from "z"', ['foo']], + ['import Button from \'./DynamicStyle.vue\'', ['Button']], + ['import button from \'./DynamicStyle.vue\'', ['button']], ['import * as foo from "z"', ['foo']], ['function foo(bar) {const a = z}', ['foo']], ['console.log(foo)', []], From 46f677c81f9adb93d8e6d2da1231438c9b03baa5 Mon Sep 17 00:00:00 2001 From: jaw <2135326728@qq.com> Date: Tue, 13 Jun 2023 17:11:12 +0800 Subject: [PATCH 03/12] test: add more --- test/nativeTag.test.ts | 65 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 test/nativeTag.test.ts diff --git a/test/nativeTag.test.ts b/test/nativeTag.test.ts new file mode 100644 index 0000000..11f7aac --- /dev/null +++ b/test/nativeTag.test.ts @@ -0,0 +1,65 @@ +import { describe, expect, it } from 'vitest' +import { transform } from '../src' + +describe('filter native tags as vue components', () => { + describe('no components output', () => { + const cases: string[] = [ + ` + + + + `, + ` + + + + `, + ` + + + + `, + ] + + for (const input of cases) { + it(input, async () => { + const result = await transform(input, 'Lang.vue', { reactivityTransform: true }) + expect(result?.code.includes('__sfc_main.components')).toEqual(false) + }) + } + }) + + it('keep non-native components', async () => { + const input = ` + + + + ` + const result = await transform(input, 'Lang.vue', { reactivityTransform: true }) + expect(result?.code.includes('__sfc_main.components = Object.assign({\n DynamicStyle\n}, __sfc_main.components);')).toEqual(true) + }) +}) From a703fb611bcee2fbdd26dc7ee1291598a2cd119f Mon Sep 17 00:00:00 2001 From: jaw <2135326728@qq.com> Date: Tue, 13 Jun 2023 17:15:21 +0800 Subject: [PATCH 04/12] test: add svg --- test/nativeTag.test.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/nativeTag.test.ts b/test/nativeTag.test.ts index 11f7aac..c0d5478 100644 --- a/test/nativeTag.test.ts +++ b/test/nativeTag.test.ts @@ -35,6 +35,27 @@ describe('filter native tags as vue components', () => { `, + ` + + + + `, ] for (const input of cases) { From 4c192125a1c04d674634fad0c356c55dc46744a0 Mon Sep 17 00:00:00 2001 From: jaw <2135326728@qq.com> Date: Tue, 13 Jun 2023 17:41:37 +0800 Subject: [PATCH 05/12] chore: update --- pnpm-lock.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index baf156d..5a6cad3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2400,7 +2400,7 @@ packages: '@babel/types': 7.22.4 '@vue/babel-helper-vue-transform-on': 1.0.2 camelcase: 6.2.0 - html-tags: 3.3.1 + html-tags: 3.1.0 svg-tags: 1.0.0 transitivePeerDependencies: - '@babel/core' @@ -5999,8 +5999,8 @@ packages: engines: {node: '>=4'} dev: true - /html-tags@3.3.1: - resolution: {integrity: sha512-ztqyC3kLto0e9WbNp0aeP+M3kTt+nbaIveGmUxAtZa+8iFgKLUOD4YKM5j+f3QD89bra7UeumolZHKuOXnTmeQ==} + /html-tags@3.1.0: + resolution: {integrity: sha512-1qYz89hW3lFDEazhjW0yVAV87lw8lVkrJocr72XmBkMKsoSVJCQx3W8BXsC7hO2qAt8BoVjYjtAcZ9perqGnNg==} engines: {node: '>=8'} dev: true From f65a2534cda5bc83c8f0d509133751f6316bb97a Mon Sep 17 00:00:00 2001 From: jaw <2135326728@qq.com> Date: Mon, 19 Jun 2023 14:09:53 +0800 Subject: [PATCH 06/12] fix: case mode correction --- playground/src/App.vue | 4 +- playground/src/ButtonTest.vue | 11 + playground/src/Foo.vue | 11 +- src/core/parseSFC.ts | 803 +++++++++++----------- src/core/transformScriptSetup.ts | 461 ++++++------- src/types.ts | 127 ++-- test/__snapshots__/transform.test.ts.snap | 3 + test/nativeTag.test.ts | 25 +- 8 files changed, 737 insertions(+), 708 deletions(-) create mode 100644 playground/src/ButtonTest.vue diff --git a/playground/src/App.vue b/playground/src/App.vue index 05165e5..c464a30 100644 --- a/playground/src/App.vue +++ b/playground/src/App.vue @@ -1,6 +1,6 @@