Custom resolve module content
English | 简体中文
- Compatible Browser, Node.js and Electron
- You can think of this as the implementation of the official tutorial 👉 Virtual Modules Convention
npm i vite-plugin-resolve -D
import { defineConfig } from 'vite'
import resolve from 'vite-plugin-resolve'
export default defineConfig({
plugins: [
resolve({
// Resolve custom module content
// This like Vite external plugin
vue: `const vue = window.Vue; export { vue as default }`,
}),
]
})
resolve({
// Support nested module id
// Support return Promise
'@scope/name': () => require('fs/promises').readFile('path', 'utf-8'),
})
resolve({
// Resolve Electron ipcRenderer in Renderer-process
electron: `const { ipcRenderer } = require('electron'); export { ipcRenderer };`,
})
entries
{
[moduleId: string]:
| ReturnType<Plugin['load']>
| ((...args: Parameters<Plugin['load']>) => ReturnType<Plugin['load']>)
}
You can see the return value type definition here rollup/types.d.ts#L272
There are two main differences
- Bypass the builtin
vite:resolve
plugin - Reasonably avoid Pre-Bundling