npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-cloudflare

v0.4.1

Published

Vite-plugin-cloudflare is a plugin for transforming & bundling cloudflare workers with shimming [modern node polyfills](https://github.com/Aslemammad/modern-node-polyfills) like `process`, `os`, `stream` and other node global functions and modules using *

Downloads

239

Readme

vite-plugin-cloudflare 🔥

Vite-plugin-cloudflare is a plugin for transforming & bundling cloudflare workers with shimming modern node polyfills like process, os, stream and other node global functions and modules using Esbuild and Vite!

  • Universal Vite plugin
  • Lightning builds
  • Workers compatible build using shimming
  • Fast development and HMR compatible reloads
  • Builtin Miniflare support

Install

npm i --save-dev vite-plugin-cloudflare esbuild@latest

Plugin

// vite.config.js
import { defineConfig } from "vite";
import vpc from "vite-plugin-cloudflare";

export default defineConfig({
  plugins: [vpc({ scriptPath: "./worker/index.ts" })],
});

The plugin gets an options object with this type signature.

export type Options = {
  // miniflare specific options for development (optional)
  miniflare?: Omit<MiniflareOptions, "script" | "watch">;
  // the worker file (required)
  scriptPath: string;
  // customize globals that need to polyfilled (process, setTimeout, ...)
  polyfilledGlobals?: PolyfilledGlobals;
  // customize mods (node's builtinModules) that need to polyfilled (utils, http, ...)
  polyfilledModules?: PolyfilledModules;
  // a fast-glob pattern for files who's changes should reload the worker (optional)
  workerFilesPattern?: string | string[];
  // enable modules (esm)
  modules?: boolean;
};

Since this plugin works with Esbuild, options passed to the esbuild field of your vite plugin will affect the worker result, unless they are not compatible with the BuildOptions type of Esbuild.

Development

You can start your Vite dev server and continue developing your applications. As previously mentioned, this plugin integrates Miniflare with Vite, so you'd have a nice experience writing your workers.

 vite dev

Build

When building, the plugin is going to start bundling your worker at the end of the vite bundling phase and generates it into the config.outDir with the worker.js file name.

 vite build

Output:

vite v3.0.4 building for production...
✓ 6 modules transformed.
dist/assets/typescript.f6ead1af.svg   1.40 KiB
dist/index.html                       0.44 KiB
dist/assets/index.2547d205.js         1.41 KiB / gzip: 0.72 KiB
dist/assets/index.d0964974.css        1.19 KiB / gzip: 0.62 KiB
🔥 [cloudflare] bundled worker file in 'dist/worker.js'

Wrangler

Update your wrangler config to be compatible with the build, for instance, here's a config that uses the dist/worker.js bundled worker file generated by vite-plugin-cloudflare and serves the assets from the vite build:

# wrangler.toml
name = "vite-ssr-worker"
main = "./dist/worker.js"
compatibility_date = "2022-08-10"

[site]
bucket = "./dist/client"

The values may change based on your build

Skip Requests

Vite has some builtin middlewares that handle different types of requests from the client, and in a Vite plugin, we can inject our middlewares along vite ones.

Vite-plugin-cloudflare injects a middleware, that is responsible for handling the worker, So every request from the client (browser) may come to your worker first, before vite native middlewares. These requests can be assets, transforms and other types of vite-related requests that should not be handled by vite-plugin-cloudflare and instead, they should be handled by vite.

This concern only occurs in dev mode, so no worries when building for production

Here's how we handle these type of requests in vite-plugin-cloudflare.

addEventListener("fetch", (event) => {
  const { pathname } = new URL(url);
  if (pathname.startsWith("/api")) {
    event.respondWith(handleFetchEvent(event));
    return;
  }
  event.respondWith(
    new Response("", {
      headers: {
        "x-skip-request": "",
      },
    })
  );
});

The x-skip-request header enforces vite-plugin-cloudflare to skip the response of the worker and passes the request to the next vite middleware, so Vite would handle the request instead.

Authors

| Mohammad Bagher | | ------------------------------------------------------------------------------------------------------------------------------------------------ |

Contributing

Feel free to create issues/discussions and then PRs for the project!

Sponsors

Your sponsorship can make a huge difference in continuing our work in open source!