Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@kdujs/reactivity-transform-canary

Package Overview
Dependencies
Maintainers
0
Versions
92
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kdujs/reactivity-transform-canary

@kdujs/reactivity-transform

  • 3.20240805.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
113
decreased by-48.4%
Maintainers
0
Weekly downloads
 
Created
Source

@kdujs/reactivity-transform

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

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 'kdu'

// 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 'kdu/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="kdu/macros-global" />

API

This package is the lower-level transform that can be used standalone. Higher-level tooling (e.g. @lahmjs/plugin-kdu and kdu-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 '@kdujs/reactivity-transform'

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

transform

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

const src = `let a = $ref(0); a++`
const {
  code, // import { ref as _ref } from 'kdu'; 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: "kdu"
}

transformAST

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

import { transformAST } from '@kdujs/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

Package last updated on 05 Aug 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc