vue3-yuanma/packages/ref-transform
dependabot[bot] 305883a12f
build(deps-dev): bump typescript from 4.3.5 to 4.4.2 (#4482)
* build(deps-dev): bump typescript from 4.3.5 to 4.4.2

Bumps [typescript](https://github.com/Microsoft/TypeScript) from 4.3.5 to 4.4.2.
- [Release notes](https://github.com/Microsoft/TypeScript/releases)
- [Commits](https://github.com/Microsoft/TypeScript/compare/v4.3.5...v4.4.2)

---
updated-dependencies:
- dependency-name: typescript
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* build(deps-dev): bump typescript from 4.3.5 to 4.4.2

* test: fix nodeOps types

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Carlos Rodrigues <carlos@hypermob.co.uk>
Co-authored-by: Evan You <yyx990803@gmail.com>
2021-09-02 09:53:57 -04:00
..
__tests__ build(deps-dev): bump typescript from 4.3.5 to 4.4.2 (#4482) 2021-09-02 09:53:57 -04:00
src fix(ref-transform): should transform $ref when used with generic arguments (#4446) 2021-09-01 16:55:30 -04:00
api-extractor.json feat(experimental): standalone ref transform 2021-08-22 22:21:42 -04:00
package.json release: v3.2.7 2021-09-01 18:05:16 -04:00
README.md feat(ref-transform): auto infer parser plugins 2021-08-23 12:20:53 -04:00

@vue/ref-transform

⚠️ This is experimental and currently only provided for testing and feedback. It may break during patches or even be removed. Use at your own risk!

Follow https://github.com/vuejs/rfcs/discussions/369 for details and updates.

Basic Rules

  • $() to turn refs into reative variables
  • $$() to access the original refs from reative variables
import { ref, watch } from 'vue'

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

// no need for .value
console.log(count)

// get the actual ref
watch($$(count), c => console.log(`count changed to ${c}`))

// assignments are reactive
count++

Shorthands

A few commonly used APIs have shorthands (which also removes the need to import them):

  • $(ref(0)) -> $ref(0)
  • $(computed(() => 123)) -> $computed(() => 123)
  • $(shallowRef({})) -> $shallowRef({})

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/ref-transform'

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

transform

import { transform } from '@vue/ref-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/ref-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 {
  rootVars, // ['a']
  importedHelpers // ['ref']
} = transformAST(ast, s)

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