From 13c9f2d767fa61d5705b0b2ecc2dc719874a45e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Sun, 1 Oct 2023 22:41:49 +0800 Subject: [PATCH] refactor: remove reactivity transform --- .eslintrc.cjs | 4 +- package.json | 2 +- .../__snapshots__/compileScript.spec.ts.snap | 18 +- .../__tests__/compileScript.spec.ts | 24 +- .../reactivityTransform.spec.ts.snap | 113 --- .../compileScript/reactivityTransform.spec.ts | 193 ----- packages/compiler-sfc/package.json | 1 - packages/compiler-sfc/src/compileScript.ts | 60 +- packages/compiler-sfc/src/index.ts | 7 - .../src/script/definePropsDestructure.ts | 4 +- .../compiler-sfc/src/script/normalScript.ts | 31 +- packages/dts-test/reactivityMacros.test-d.ts | 110 --- packages/reactivity-transform/LICENSE | 21 - packages/reactivity-transform/README.md | 123 --- .../reactivityTransform.spec.ts.snap | 292 ------- .../__tests__/reactivityTransform.spec.ts | 564 ------------- packages/reactivity-transform/package.json | 41 - .../reactivity-transform/src/babelPlugin.ts | 3 - packages/reactivity-transform/src/index.ts | 1 - .../src/reactivityTransform.ts | 794 ------------------ packages/sfc-playground/src/App.vue | 2 +- packages/vue/macros-global.d.ts | 19 - packages/vue/macros.d.ts | 112 --- packages/vue/package.json | 10 +- packages/vue/ref-macros.d.ts | 2 - pnpm-lock.yaml | 28 - scripts/pre-dev-sfc.js | 1 - 27 files changed, 43 insertions(+), 2537 deletions(-) delete mode 100644 packages/compiler-sfc/__tests__/compileScript/__snapshots__/reactivityTransform.spec.ts.snap delete mode 100644 packages/compiler-sfc/__tests__/compileScript/reactivityTransform.spec.ts delete mode 100644 packages/dts-test/reactivityMacros.test-d.ts delete mode 100644 packages/reactivity-transform/LICENSE delete mode 100644 packages/reactivity-transform/README.md delete mode 100644 packages/reactivity-transform/__tests__/__snapshots__/reactivityTransform.spec.ts.snap delete mode 100644 packages/reactivity-transform/__tests__/reactivityTransform.spec.ts delete mode 100644 packages/reactivity-transform/package.json delete mode 100644 packages/reactivity-transform/src/babelPlugin.ts delete mode 100644 packages/reactivity-transform/src/index.ts delete mode 100644 packages/reactivity-transform/src/reactivityTransform.ts delete mode 100644 packages/vue/macros-global.d.ts delete mode 100644 packages/vue/macros.d.ts delete mode 100644 packages/vue/ref-macros.d.ts diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 04ecf049ca9..b46a41b6617 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -56,9 +56,7 @@ module.exports = { }, // Packages targeting Node { - files: [ - 'packages/{compiler-sfc,compiler-ssr,server-renderer,reactivity-transform}/**' - ], + files: ['packages/{compiler-sfc,compiler-ssr,server-renderer}/**'], rules: { 'no-restricted-globals': ['error', ...DOMGlobals], 'no-restricted-syntax': 'off' diff --git a/package.json b/package.json index e92b0c91c00..d163821d319 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "serve": "serve", "open": "open http://localhost:3000/packages/template-explorer/local.html", "build-sfc-playground": "run-s build-compiler-cjs build-runtime-esm build-ssr-esm build-sfc-playground-self", - "build-compiler-cjs": "node scripts/build.js compiler reactivity-transform shared -af cjs", + "build-compiler-cjs": "node scripts/build.js compiler shared -af cjs", "build-runtime-esm": "node scripts/build.js runtime reactivity shared -af esm-bundler && node scripts/build.js vue -f esm-bundler-runtime && node scripts/build.js vue -f esm-browser-runtime", "build-ssr-esm": "node scripts/build.js compiler-sfc server-renderer -f esm-browser", "build-sfc-playground-self": "cd packages/sfc-playground && npm run build", diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 949c9946d9f..815fe446926 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -515,20 +515,20 @@ return { } `; exports[`SFC compile - `, - { reactivityTransform: true } + + + ` ) assertCode(content) - expect(content).toMatch(`import { ref } from 'vue'`) + expect(content).toMatch( + `import { useCssVars as _useCssVars, unref as _unref } from 'vue'` + ) + expect(content).toMatch(`import { useCssVars, ref } from 'vue'`) }) test('import dedupe between `, { - reactivityTransform: true - }) + const { content } = compile(``) if (shouldAsync) { expect(content).toMatch(`let __temp, __restore`) } @@ -910,7 +916,7 @@ describe('SFC compile `) - expect(content).not.toMatch(`$(ref())`) - expect(content).not.toMatch(`$(ref(1))`) - expect(content).not.toMatch(`$(shallowRef({`) - expect(content).toMatch(`let foo = (ref())`) - expect(content).toMatch(`let a = (ref(1))`) - expect(content).toMatch(` - let b = (shallowRef({ - count: 0 - })) - `) - // normal declarations left untouched - expect(content).toMatch(`let c = () => {}`) - expect(content).toMatch(`let d`) - expect(content).toMatch( - `return { foo, a, b, get c() { return c }, set c(v) { c = v }, ` + - `get d() { return d }, set d(v) { d = v }, ref, shallowRef }` - ) - assertCode(content) - expect(bindings).toStrictEqual({ - foo: BindingTypes.SETUP_REF, - a: BindingTypes.SETUP_REF, - b: BindingTypes.SETUP_REF, - c: BindingTypes.SETUP_LET, - d: BindingTypes.SETUP_LET, - ref: BindingTypes.SETUP_CONST, - shallowRef: BindingTypes.SETUP_CONST - }) - }) - - test('$ref & $shallowRef declarations', () => { - const { content, bindings } = compileWithReactivityTransform(``) - expect(content).toMatch( - `import { ref as _ref, shallowRef as _shallowRef } from 'vue'` - ) - expect(content).not.toMatch(`$ref()`) - expect(content).not.toMatch(`$ref(1)`) - expect(content).not.toMatch(`$shallowRef({`) - expect(content).toMatch(`let foo = _ref()`) - expect(content).toMatch(`let a = _ref(1)`) - expect(content).toMatch(` - let b = _shallowRef({ - count: 0 - }) - `) - // normal declarations left untouched - expect(content).toMatch(`let c = () => {}`) - expect(content).toMatch(`let d`) - assertCode(content) - expect(bindings).toStrictEqual({ - foo: BindingTypes.SETUP_REF, - a: BindingTypes.SETUP_REF, - b: BindingTypes.SETUP_REF, - c: BindingTypes.SETUP_LET, - d: BindingTypes.SETUP_LET - }) - }) - - test('usage in normal `) - expect(content).not.toMatch(`$ref(0)`) - expect(content).toMatch(`import { ref as _ref } from 'vue'`) - expect(content).toMatch(`let count = _ref(0)`) - expect(content).toMatch(`count.value++`) - expect(content).toMatch(`return ({ count })`) - assertCode(content) - }) - - test('usage /w typescript', () => { - const { content } = compileWithReactivityTransform(` - - `) - expect(content).toMatch(`import { ref as _ref`) - expect(content).toMatch(`let msg = _ref('foo')`) - expect(content).toMatch(`let bar = _ref ('bar')`) - assertCode(content) - }) - - test('usage with normal - `) - // should dedupe helper imports - expect(content).toMatch(`import { ref as _ref } from 'vue'`) - - expect(content).toMatch(`let a = _ref(0)`) - expect(content).toMatch(`let b = _ref(0)`) - - // root level ref binding declared in - - `) - expect(content).toMatch(`console.log(data.value)`) - assertCode(content) - }) - - describe('errors', () => { - test('defineProps/Emit() referencing ref declarations', () => { - expect(() => - compile( - ``, - { reactivityTransform: true } - ) - ).toThrow(`cannot reference locally declared variables`) - - expect(() => - compile( - ``, - { reactivityTransform: true } - ) - ).toThrow(`cannot reference locally declared variables`) - }) - }) -}) diff --git a/packages/compiler-sfc/package.json b/packages/compiler-sfc/package.json index a985b60315d..9f305e9db4e 100644 --- a/packages/compiler-sfc/package.json +++ b/packages/compiler-sfc/package.json @@ -36,7 +36,6 @@ "@vue/compiler-core": "3.3.4", "@vue/compiler-dom": "3.3.4", "@vue/compiler-ssr": "3.3.4", - "@vue/reactivity-transform": "3.3.4", "@vue/shared": "3.3.4", "estree-walker": "^2.0.2", "magic-string": "^0.30.3", diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index cfcc607c72d..b861cdc7abe 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -26,7 +26,6 @@ import { import { CSS_VARS_HELPER, genCssVarsCode } from './style/cssVars' import { compileTemplate, SFCTemplateCompileOptions } from './compileTemplate' import { warnOnce } from './warn' -import { shouldTransform, transformAST } from '@vue/reactivity-transform' import { transformDestructuredProps } from './script/definePropsDestructure' import { ScriptCompileContext } from './script/context' import { @@ -122,14 +121,6 @@ export interface SFCScriptCompileOptions { fileExists(file: string): boolean readFile(file: string): string | undefined } - /** - * (Experimental) Enable syntax transform for using refs without `.value` and - * using destructured props with reactivity - * @deprecated the Reactivity Transform proposal has been dropped. This - * feature will be removed from Vue core in 3.4. If you intend to continue - * using it, disable this and switch to the [Vue Macros implementation](https://vue-macros.sxzz.moe/features/reactivity-transform.html). - */ - reactivityTransform?: boolean } export interface ImportBinding { @@ -165,8 +156,6 @@ export function compileScript( const scriptLang = script && script.lang const scriptSetupLang = scriptSetup && scriptSetup.lang - // TODO remove in 3.4 - const enableReactivityTransform = !!options.reactivityTransform let refBindings: string[] | undefined if (!scriptSetup) { @@ -477,20 +466,6 @@ export function compileScript( } } - // apply reactivity transform - // TODO remove in 3.4 - if (enableReactivityTransform && shouldTransform(script.content)) { - const { rootRefs, importedHelpers } = transformAST( - scriptAst, - ctx.s, - scriptStartOffset! - ) - refBindings = rootRefs - for (const h of importedHelpers) { - ctx.helperImports.add(h) - } - } - //