A vite plugin deals with require.resolve() assets, for node-browser mixed-context environment like nw.js etc.
npm install vite-plugin-require-resolve -D
import vitePluginRequireResolve from 'vite-plugin-require-resolve'
export default defineConfig({
plugins: [
vitePluginRequireResolve(),
...
],
...
})
// in .ts file
fs.readFileSync(require.resolve('../myUpperDir/Resources/name.ext'))
// will be transform to, 'abcdefgh' will be the first 8 characters of file md5 string
fs.readFileSync(require.resolve('./your-vite-assets-dir/name-abcdefgh.ext'))
- path inside require.resolve() must be relative.
- output .js files must be flat with vite assets folder.
MIT