'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 1:[1]

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

Solution 2:[2]

The error during deployment may be due to a problem with the dfx version. Projects made with older versions of dfx may not be compatible with newer versions of dfx.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chovy
Solution 2 Zheng Nash