Socket
Socket
Sign inDemoInstall

@vue/reactivity-transform

Package Overview
Dependencies
7
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @vue/reactivity-transform

@vue/reactivity-transform


Version published
Weekly downloads
2.1M
decreased by-3.52%
Maintainers
1
Install size
3.03 MB
Created
Weekly downloads
 

Package description

What is @vue/reactivity-transform?

The @vue/reactivity-transform package is part of the Vue.js ecosystem, designed to enhance the developer experience by providing syntactic sugar for the Vue Composition API. It allows developers to write reactive code in a more concise and readable manner by automatically wrapping reactive state and refs without the need for explicit calls to `ref()` or `reactive()` functions. This package leverages compile-time transformations to achieve this, making the code both cleaner and easier to understand.

What are @vue/reactivity-transform's main functionalities?

Automatic Ref Creation

Automatically creates a Vue ref (reactive reference) from a simple assignment. This eliminates the need to explicitly call `ref()` from Vue's Composition API, making the code cleaner and more intuitive.

let count = $ref(0);
function increment() {
  count++;
}

Reactive Declarations

Enables the creation of computed properties in a more concise way. By using `$computed`, it simplifies the syntax required to declare reactive computed properties, making the code more readable.

let doubled = $computed(() => count * 2);

Reactive Effects

Facilitates the creation of side effects that automatically track reactive dependencies. Using `$effect`, developers can easily define behavior that responds to changes in reactive data, without the boilerplate code typically associated with setting up watchers.

$effect(() => {
  console.log(`count is: ${count}`);
});

Other packages similar to @vue/reactivity-transform

Changelog

Source

3.3.13 (2023-12-19)

Bug Fixes

  • compiler-core: fix v-on with modifiers on inline expression of undefined (#9866) (bae79dd), closes #9865
  • runtime-dom: cache event handlers by key/modifiers (#9851) (04d2c05), closes #9849
  • types: extract properties from extended collections (#9854) (24b1c1d), closes #9852

Readme

Source

@vue/reactivity-transform

⚠️ This is experimental and the proposal has been dropped. The feature is now marked as deprecated and will be removed from Vue core in 3.4.

See reason for deprecation here.

Basic Rules

  • Ref-creating APIs have $-prefixed versions that create reactive variables instead. They also do not need to be explicitly imported. These include:
    • ref
    • computed
    • shallowRef
    • customRef
    • toRef
  • $() can be used to destructure an object into reactive variables, or turn existing refs into reactive variables
  • $$() to "escape" the transform, which allows access to underlying refs
import { watchEffect } from 'vue'

// bind ref as a variable
let count = $ref(0)

watchEffect(() => {
  // no need for .value
  console.log(count)
})

// assignments are reactive
count++

// get the actual ref
console.log($$(count)) // { value: 1 }

Macros can be optionally imported to make it more explicit:

// not necessary, but also works
import { $, $ref } from 'vue/macros'

let count = $ref(0)
const { x, y } = $(useMouse())

Global Types

To enable types for the macros globally, include the following in a .d.ts file:

/// <reference types="vue/macros-global" />

API

This package is the lower-level transform that can be used standalone. Higher-level tooling (e.g. @vitejs/plugin-vue and vue-loader) will provide integration via options.

shouldTransform

Can be used to do a cheap check to determine whether full transform should be performed.

import { shouldTransform } from '@vue/reactivity-transform'

shouldTransform(`let a = ref(0)`) // false
shouldTransform(`let a = $ref(0)`) // true

transform

import { transform } from '@vue/reactivity-transform'

const src = `let a = $ref(0); a++`
const {
  code, // import { ref as _ref } from 'vue'; let a = (ref(0)); a.value++"
  map
} = transform(src, {
  filename: 'foo.ts',
  sourceMap: true,

  // @babel/parser plugins to enable.
  // 'typescript' and 'jsx' will be auto-inferred from filename if provided,
  // so in most cases explicit parserPlugins are not necessary
  parserPlugins: [
    /* ... */
  ]
})

Options

interface RefTransformOptions {
  filename?: string
  sourceMap?: boolean // default: false
  parserPlugins?: ParserPlugin[]
  importHelpersFrom?: string // default: "vue"
}

transformAST

Transform with an existing Babel AST + MagicString instance. This is used internally by @vue/compiler-sfc to avoid double parse/transform cost.

import { transformAST } from '@vue/reactivity-transform'
import { parse } from '@babel/parser'
import MagicString from 'magic-string'

const src = `let a = $ref(0); a++`
const ast = parse(src, { sourceType: 'module' })
const s = new MagicString(src)

const {
  rootRefs, // ['a']
  importedHelpers // ['ref']
} = transformAST(ast, s)

console.log(s.toString()) // let a = _ref(0); a.value++

Keywords

FAQs

Last updated on 19 Dec 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc