-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Vite
If you're trying to use GUN with vite build tool, you can get a couple of errors. One is that vite pre-bundles the dependencies, so you can't import something from inside of them. That's exactly what we do when we import SEA from 'gun/sea'
and all the gun/lib
s too. So you need to add a list of included optimized dependencies to vite.config.js
for vite to know that you'll need their insides.
Second - the problem with vite including the 'text-encoding' node polyfill that is required in SEA.js
and is not needed in modern browsers. It unnecessary bumps up the file size of your bundle +700kB. We don't want that. So first step is to exclude the package from the build. This works, but you'll encounter an error of a wrong import. So here comes the solution noted by Evan You himself. We just create a custom plugin, that properly removes the unnecessary extension.
With the help of @jojobyte in Gun discord channel we figured out a rather universal function to do the job. So here's the final vite.config.js
that makes Gun working nice with vite:
const moduleExclude = match => {
const m = id => id.indexOf(match) > -1
return {
name: `exclude-${match}`,
resolveId(id) {
if (m(id)) return id
},
load(id) {
if (m(id)) return `export default {}`
},
}
}
export default {
optimizeDeps: {
include: [
'gun',
'gun/gun',
'gun/sea',
'gun/sea.js',
'gun/lib/then',
'gun/lib/webrtc',
'gun/lib/radix',
'gun/lib/radisk',
'gun/lib/store',
'gun/lib/rindexed',
],
},
plugins: [
moduleExclude('text-encoding'),
],
}