lxhom / skad-cf-extra

SvelteKit Adapter for Cloudflare (Pages & Workers) with a few extra features, mainly better Node.js polyfill support.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

adapter-cloudflare

Adapter for building SvelteKit applications on Cloudflare Pages with Workers integration.

Options

From the Upstream docs

The routes option allows you to customise the _routes.json file generated by adapter-cloudflare.

  • include defines routes that will invoke a function, and defaults to ['/*']
  • exclude defines routes that will not invoke a function — this is a faster and cheaper way to serve your app's static assets. This array can include the following special values:
    • <build> contains your app's build artifacts (the files generated by Vite)
    • <files> contains the contents of your static directory
    • <prerendered> contains a list of prerendered pages
    • <all> (the default) contains all of the above

You can have up to 100 include and exclude rules combined. Generally you can omit the routes options, but if (for example) your <prerendered> paths exceed that limit, you may find it helpful to manually create an exclude list that includes '/articles/*' instead of the auto-generated ['/articles/foo', '/articles/bar', '/articles/baz', ...].

Added options

esBuildOptions: Adds additional options to the esbuild process. See esbuild.BuildOptions for more information.

Why?

Node.js modules.

If you had this error when trying to use a node module in your SvelteKit application:

> Using @sveltejs/adapter-cloudflare
 [ERROR] Could not resolve "path"

    node_modules/bcrypt/bcrypt.js:4:19:
      4  var path = require('path');
                            ~~~~~~

  The package "path" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

then you can just use an ESBuild plugin to polyfill the Node.js modules.

import {NodeModulesPolyfillPlugin} from '@esbuild-plugins/node-modules-polyfill'
import {NodeGlobalsPolyfillPlugin} from '@esbuild-plugins/node-globals-polyfill'

export default {
    kit: {
        adapter: adapter({
            // See below for an explanation of these options
            routes: {
                include: ['/*'],
                exclude: ['<all>']
            },
            esBuildOptions: {
                plugins: [
                    NodeModulesPolyfillPlugin(),
                    NodeGlobalsPolyfillPlugin({
                        process: true,
                        buffer: true
                    })
                ]
            }
        })
    }
};

Those few lines of code will allow you to use Node.js modules in your SvelteKit application, even when deployed to Cloudflare Pages.

Changelog

Downstream: Check commit history.

Upstream: The Changelog for this package is available on GitHub.

About

SvelteKit Adapter for Cloudflare (Pages & Workers) with a few extra features, mainly better Node.js polyfill support.


Languages

Language:JavaScript 97.5%Language:TypeScript 2.5%