Search code examples
sveltesveltekitdfinity

How do I deploy a sveltekit app to a dfinity container?


Difinity is a blockchain container. I need a rock solid example on how to deploy a standard sveltekit app to it.

Their web page doesn’t cover sveltekit https://dfinity.org/svelte/

Here is my dfx.json file:

{
    "canisters": {
        "assets": {
            "dependencies": [],
            "frontend": {
                "entrypoint": "build/index.html"
            },
            "source": ["build"],
            "type": "assets"
        }
    },
    "defaults": {
        "build": {
            "output": "canisters",
            "packtool": ""
        }
    },
    "dfx": "0.9.3",
    "networks": {
        "local": {
            "bind": "127.0.0.1:8000",
            "type": "ephemeral"
        },
        "ic": {
            "providers": ["https://mainnet.dfinity.network"],
            "type": "persistent"
        }
    },
    "version": 1
}

The command npm run build will build a static version of my sveltekit app in ./build


Solution

  • We fixed the issue by adding some polyfills:

    import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill';
    
    ....
    
    
    
            vite: {
                optimizeDeps: {
                    esbuildOptions: {
                        // Node.js global to browser globalThis
                        define: {
                            global: 'globalThis'
                        },
                        // Enable esbuild polyfill plugins
                        plugins: [
                            NodeGlobalsPolyfillPlugin({
                                buffer: true,
                                global: true,
                                process: true,
                                url: true,
                                assert: true,
                                crypto: true,
                                http: true,
                                https: true,
                                os: true,
                                stream: true
                            })
                        ]
                    }
                },
    ...
    }
    

    in svelte.config.js