Skip to content
DeFUCC edited this page Feb 12, 2021 · 8 revisions

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/libs 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'),
  ],
}

This wiki is where all the GUN website documentation comes from.

You can read it here or on the website, but the website has some special features like rendering some markdown extensions to create interactive coding tutorials.

Please feel free to improve the docs itself, we need contributions!

Clone this wiki locally