Skip to content

create official "use cache" plugin #859

@hi-ogawa

Description

@hi-ogawa

Not sure what this means but we can somehow polish and generalize this example, so it's usable on Waku, React router, etc.

function vitePluginUseCache(): Plugin[] {
return [
{
name: 'use-cache',
async transform(code) {
if (!code.includes('use cache')) return
const ast = await parseAstAsync(code)
// @ts-ignore for rolldown-vite ci estree/oxc mismatch
const result = transformHoistInlineDirective(code, ast, {
runtime: (value) => `__vite_rsc_cache(${value})`,
directive: 'use cache',
rejectNonAsyncFunction: true,
noExport: true,
})
if (!result.output.hasChanged()) return
result.output.prepend(
`import __vite_rsc_cache from "/src/framework/use-cache-runtime";`,
)
return {
code: result.output.toString(),
map: result.output.generateMap({ hires: 'boundary' }),
}
},
},
]
}

See also hi-ogawa/vite-plugins#902 for more context.

(Also I'm feeling stacking together transformHoistInlineDirective for "use cache" and internally for "use server" can break something. We want to investigate that.)


Or we can use externally maintained:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions