Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow plugins to overwrite the cachedRead function #402

Closed
SimonSiefke opened this issue Jun 17, 2020 · 1 comment
Closed

Allow plugins to overwrite the cachedRead function #402

SimonSiefke opened this issue Jun 17, 2020 · 1 comment

Comments

@SimonSiefke
Copy link

Is your feature request related to a problem? Please describe.
I would like to overwrite the cachedRead function for a VSCode plugin for Vite https://github.com/SimonSiefke/vscode-vite. The idea is to not serve the files from the filesystem but directly from VSCode so that one can see the page update while typing. The prototype is already working but I basically needed to fork Vite only to modify the readFile behaviour. That makes it difficult to pull in upstream changes from Vite.

Describe the solution you'd like
A possible solution would be to put the cachedRead function into the context object:

// node/server/index.ts
import { cachedRead } from '../utils'

const context: Context = {
    root,
    app,
    server,
    watcher,
    resolver,
    config,
    read: cachedRead // put cachedRead onto the context
}

// serverPluginEsBuild.ts
if (ctx.path === vueJsxPublicPath) {
  await ctx.read(ctx, vueJsxFilePath) // use ctx.read instead of cachedRead
}

// serverPluginServeStatic.ts
const filePath = resolver.requestToFile(ctx.path)
if (
  filePath !== ctx.path &&
  fs.existsSync(filePath) &&
  fs.statSync(filePath).isFile()
   ) {
  await ctx.read(ctx, filePath) // use ctx.read instead of cachedRead
}

// serverPluginVue.ts
const importer = ctx.path
const importee = cleanUrl(resolveImport(root, importer, block.src!, resolver))
const filePath = resolver.requestToFile(importee)
await ctx.read(ctx, filePath) // use ctx.read instead of cachedRead
block.content = ctx.body

And plugins will be able to overwrite the read function:

const myPlugin: ServerPlugin = (ctx) => {
  ctx.read = newRead
}

Additional context

vscode-vite

@yyx990803
Copy link
Member

Cool idea! ctx.read sounds reasonable. Open to a PR.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants