MCPcopy Index your code
hub / github.com/antfu/vite-plugin-vue-server-ref

github.com/antfu/vite-plugin-vue-server-ref @v1.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.0.0 ↗ · + Follow
37 symbols 110 edges 20 files 0 documented · 0% 1 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

vite-plugin-vue-server-ref

NPM version

Share state between clients and Vite server.

Install

npm i -D vite-plugin-vue-server-ref

Add plugin to your vite.config.ts:

// vite.config.ts
import ServerRef from 'vite-plugin-vue-server-ref'

export default {
  plugins: [
    ServerRef({
      state: {
        /* Your custom initial state */
        foo: 'bar',
        object: {
          count: 0
        }
      }
    })
  ]
}

Use import it in your modules (server-ref:[key])

import foo from 'server-ref:foo'

console.log(foo.value) // bar

foo.value = 'foobar'

// same as other modules / clients imported the server ref with same key
// or even refresh the pages
console.log(foo.value) // foobar

Or working with reactive object (server-reactive:[key])

import object from 'server-reactive:object'

console.log(object.count) // 0

Type Support

As server import can't infer the type correctly (by default it's ServerRef<any>), you can using as to specify the type.

import type { ServerReactive, ServerRef } from 'vite-plugin-vue-server-ref/client'
import _foo from 'server-ref:foo'
import _object from 'server-ref:object'

const foo = _foo as ServerRef<string>
const object = _object as ServerReactive<{ count: number }>

foo.value // string
object.count // number

Controls

import foo from 'server-ref:foo'

foo.$syncUp = false // make it download only

foo.value = 'foobar' // won't send to server or other clients
import foo from 'server-ref:foo'

foo.$syncDown = false // make it upload only

// changes from other clients won't be received
import foo from 'server-ref:foo'

// listen to server change
foo.$onSet((value) => {
  console.log(`Changes from server: ${value}`)
})

Diffing

When working with reactive objects, you can add ?diff to make the syncing incremental (deep diff).

import object from 'server-ref:object?diff'

console.log(object) // { foo: ..., bar: ... }

object.foo.nested = 'bar'
// the patch will be sent as '{ foo: { nested: 'bar' }}}'
// instead of the entire object

Sponsors

License

MIT License © 2021 Anthony Fu

Extension points exported contracts — how you extend this code

ParsedId (Interface)
(no doc)
src/utils.ts
ServerRefOptions (Interface)
(no doc)
src/types.ts
ServerRefExtend (Interface)
(no doc)
src/types.ts

Core symbols most depended-on inside this repo

define
called by 28
src/client.ts
parseId
called by 6
src/utils.ts
stringify
called by 5
src/client.ts
genCode
called by 5
src/generate.ts
apply
called by 5
src/diff/apply.ts
randId
called by 4
src/client.ts
get
called by 3
src/utils.ts
post
called by 2
test/output/generate-reactive-foo.js

Shape

Function 34
Interface 3

Languages

TypeScript100%

Modules by API surface

src/client.ts6 symbols
test/output/generate-reactive-diff.js5 symbols
src/utils.ts5 symbols
src/index.ts5 symbols
test/output/generate-ref-foo.js3 symbols
test/output/generate-ref-foo-bar.js3 symbols
test/output/generate-reactive-foo.js3 symbols
src/types.ts2 symbols
src/diff/diff.ts2 symbols
src/options.ts1 symbols
src/generate.ts1 symbols
src/diff/apply.ts1 symbols

Used by 1 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add vite-plugin-vue-server-ref \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact