Add Effect v4 support, Fast Refresh tooling, and revamped docs #56
@@ -1,9 +1,15 @@
|
|||||||
import path from "node:path"
|
import path from "node:path"
|
||||||
import type { Plugin, TransformPluginContext } from "vite"
|
import type { Plugin } from "vite"
|
||||||
import { describe, expect, it } from "vitest"
|
import { describe, expect, it } from "vitest"
|
||||||
import { effectViewPlugin } from "./index.js"
|
import { effectViewPlugin } from "./index.js"
|
||||||
|
|
||||||
|
|
||||||
|
type TransformHook = Extract<
|
||||||
|
NonNullable<Plugin["transform"]>,
|
||||||
|
(...args: never[]) => unknown
|
||||||
|
>
|
||||||
|
type TransformPluginContext = ThisParameterType<TransformHook>
|
||||||
|
|
||||||
const runTransform = async (
|
const runTransform = async (
|
||||||
plugin: Plugin,
|
plugin: Plugin,
|
||||||
code: string,
|
code: string,
|
||||||
@@ -19,7 +25,7 @@ const runTransform = async (
|
|||||||
|
|
||||||
if (!result)
|
if (!result)
|
||||||
return undefined
|
return undefined
|
||||||
return typeof result === "string" ? result : result.code
|
return typeof result === "string" ? result : result.code?.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
const transform = (
|
const transform = (
|
||||||
|
|||||||
@@ -8,6 +8,12 @@ describe("refresh runtime", () => {
|
|||||||
const component = <A extends object>(value: A): A & Component.Component.Any =>
|
const component = <A extends object>(value: A): A & Component.Component.Any =>
|
||||||
value as A & Component.Component.Any
|
value as A & Component.Component.Any
|
||||||
|
|
||||||
|
const refreshCell = (value: Component.Component.Any): Refreshable.Cell => {
|
||||||
|
if (!Refreshable.isRefreshable(value))
|
||||||
|
throw new Error("Expected a refreshable component")
|
||||||
|
return value[Refreshable.RefreshableTypeId]
|
||||||
|
}
|
||||||
|
|
||||||
it("retains a cell and notifies subscribers for compatible updates", async () => {
|
it("retains a cell and notifies subscribers for compatible updates", async () => {
|
||||||
const hot = {
|
const hot = {
|
||||||
data: {},
|
data: {},
|
||||||
@@ -15,12 +21,12 @@ describe("refresh runtime", () => {
|
|||||||
invalidate: vi.fn(),
|
invalidate: vi.fn(),
|
||||||
}
|
}
|
||||||
const first = register(component({ body: "first" }), hot, "module:View", "hooks")
|
const first = register(component({ body: "first" }), hot, "module:View", "hooks")
|
||||||
const cell = (first as Record<PropertyKey, unknown>)[Refreshable.RefreshableTypeId] as Refreshable.Cell
|
const cell = refreshCell(first)
|
||||||
const listener = vi.fn()
|
const listener = vi.fn()
|
||||||
cell.subscribe(listener)
|
cell.subscribe(listener)
|
||||||
|
|
||||||
const second = register(component({ body: "second" }), hot, "module:View", "hooks")
|
const second = register(component({ body: "second" }), hot, "module:View", "hooks")
|
||||||
const secondCell = (second as Record<PropertyKey, unknown>)[Refreshable.RefreshableTypeId]
|
const secondCell = refreshCell(second)
|
||||||
|
|
||||||
expect(secondCell).toBe(cell)
|
expect(secondCell).toBe(cell)
|
||||||
expect(cell.current).toBe(second)
|
expect(cell.current).toBe(second)
|
||||||
@@ -40,7 +46,7 @@ describe("refresh runtime", () => {
|
|||||||
invalidate: vi.fn(),
|
invalidate: vi.fn(),
|
||||||
}
|
}
|
||||||
const first = register(component({}), hot, "module:View", "one")
|
const first = register(component({}), hot, "module:View", "one")
|
||||||
const cell = (first as Record<PropertyKey, unknown>)[Refreshable.RefreshableTypeId] as Refreshable.Cell
|
const cell = refreshCell(first)
|
||||||
|
|
||||||
register(component({}), hot, "module:View", "two")
|
register(component({}), hot, "module:View", "two")
|
||||||
expect(cell.snapshot.resetRevision).toBe(1)
|
expect(cell.snapshot.resetRevision).toBe(1)
|
||||||
@@ -52,7 +58,7 @@ describe("refresh runtime", () => {
|
|||||||
it("is inert outside a Vite hot context", () => {
|
it("is inert outside a Vite hot context", () => {
|
||||||
const descriptor = component({})
|
const descriptor = component({})
|
||||||
expect(register(descriptor, undefined, "module:View", "hooks")).toBe(descriptor)
|
expect(register(descriptor, undefined, "module:View", "hooks")).toBe(descriptor)
|
||||||
expect((descriptor as Record<PropertyKey, unknown>)[Refreshable.RefreshableTypeId]).toBeUndefined()
|
expect(Refreshable.isRefreshable(descriptor)).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("invalidates when the module's View IDs change", () => {
|
it("invalidates when the module's View IDs change", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user