ben-rogerson / twin.macro

๐Ÿฆนโ€โ™‚๏ธ Twin blends the magic of Tailwind with the flexibility of css-in-js (emotion, styled-components, solid-styled-components, stitches and goober) at build time.
MIT License
7.89k stars 184 forks source link

ReferenceError: process is not defined when using with Cloudflare wrangler pages #789

Closed arielvieira closed 1 year ago

arielvieira commented 1 year ago

When using twin.macro with cloudflare wrangler cli (wrangler pages dev) it will have no access to the process and thus throwing this error when trying to use the library.

C:\Users\{path}\tmp-9348-M70raJy1uRBS\[[path]].js:14140
    var isWindows = process.platform === "win32", splitWindowsRe = /^(((?:[a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?[\\\/]?)(?:[^\\\/]*[\\\/])*)((\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))[\\\/]*$/, win32 = {};
                    ^
ReferenceError: process is not defined
ben-rogerson commented 1 year ago

Hey there, thanks for letting me know about this. I'm not sure where that process code lives, I don't believe it's within the twin codebase - do you have a repo or something I can use to test this?

arielvieira commented 1 year ago

@ben-rogerson I think this is coming from the chalk library or any sub dependency of a dependency.

Here's a working example of the issue: https://github.com/arielvieira/remix-cloudflare-pages-twin-macro

yarn install
yarn dev

In app\routes\index.tsx when importing import 'twin.macro' is throwing the error when running with the wrangler CLI.

import 'twin.macro'
ben-rogerson commented 1 year ago

After having a good crack at this I've concluded that twin isn't a good choice for styling within Remix.

Here are some reasons:

Sorry for the bad news here - perhaps you're better of with a vanilla tailwind styling solution.

rafaell-lycan commented 1 year ago

@arielvieira I had a similar issue when playing around with Remix. I also twin.macro is my tool of choice (with Emotion) I couldn't make it work either.

A solution I found "okay" was CVA which contains a similar Stitches API and works fine, you only need to learn how their API works. ๐Ÿ‘ป

@ben-rogerson thanks for going through it. After failing badly to make it run, I found this issue. ๐Ÿ‘

Hopefully twin.macro will evolve and become abstract enough to work on Remix/Astro/Whatever. ๐Ÿ™

dnnp2011 commented 1 year ago

I'm having this same error in a Laravel environment. I'm using Vite bundler (as is the default in Laravel 10). Vite doesn't support the process.env object. I have to use import.meta.env, which I would guess is where the problem lies.

import tw from 'twin.macro';
image
ben-rogerson commented 1 year ago

Closing this as we've found Cloudflare workers is an unsupported environment for babel macros to run.