styled-components / styled-components

Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅
https://styled-components.com
MIT License
40.11k stars 2.48k forks source link

Use the `node:` protocol in ServerStyleSheet.tsx #4259

Open hawkup opened 2 months ago

hawkup commented 2 months ago

Use Case

I'm trying to use ServerStyleSheet to render styles on Cloudflare Workers with styled-components. ServerStyleSheet relies on stream’s built-in Node.js modules, which aren't provided by default in the Cloudflare Workers' Node.js runtime API. To polyfill this API, I need to set compatibility flags. However, the Node.js runtime is accessible only under the node: prefix. It's unable to import from "stream" directly; instead, we need to use the node: prefix to make it available.

From the Cloudflare Worker documentation (https://developers.cloudflare.com/workers/runtime-apis/nodejs/)

Node.js APIs are available under the node: prefix, and this prefix must be used when importing modules, both in your code and the npm packages you depend on.

// Do this:
import { Buffer } from 'node:buffer';

// Do not do this:
import { Buffer } from 'buffer';

And the benefits of using the node: protocol.

ref: https://github.com/nodejs/node/issues/38343#issue-864698633

Solution

Add the node: prefix to all import/require stream modules in packages/styled-components/src/models/ServerStyleSheet.tsx.

Concern

Additional

If you agree to add node: prefix, could you please consider releasing the same changes under styled-components version 5? I'm currently using primer/react, which still relies on styled-component version 5. And I'd be happy to contribute this if you want