-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Cloudflare Worker environment in @emotion/cache, @emotion/styled, @emotion/react, and @emotion/server #2554
Comments
I wonder - in which file do you patch this variable? with |
Not sure if I understand the question entirely, but I made the changes directly in |
Ah, ok - this makes more sense now. I've thought that you patched your node_modules somehow and wasn't sure how you have made it work given the minimal changes that you have described. |
@tbrockman do you think you can provide the changes you've made? I'd love it if you could share them :) |
@dan-lee u could try removing the |
@Andarist thanks, that's good suggestion and actually seems to work! Unfortunately |
Hm, this might depend on the package manager. If you already have Emotion installed then having: "postinstall": "patch-package" should usually be enough |
Unfortunately, it doesn't work (with |
You might want to take a look at this: https://github.com/ds300/patch-package#yarn |
Update: I have a PR into preconstruct (which emotion uses to build its packages) to support "custom package exports conditionals" -- including support for a new "worker" conditional that can be used for Cloudflare worker style environments. I also have a PR to emotion (currently in draft mode) that uses this new version of precontruct to enable package exports on all the relevant packages, including the new "worker" conditional. Finally, I have a super simple Cloudflare Worker deployed using the updated emotion packages and it works! |
Exactly the same issue for me as well. Is it possible to disable emotion SSR? |
@chasoft @pavi2410 Hi, this is a repository of samples for running Remix and emotion with cloudflare workers. Please refer to this sample to get by until this issue is resolved. I hope this is helpful to you. |
Temporary patch to get remix + Cloudflare pages + emotion playing niceUsing @aiji42 fanastic solution for getting Cloudflare Workers + emotion. I have extended it for Heres how:
diff --git a/node_modules/@remix-run/dev/compiler.js b/node_modules/@remix-run/dev/compiler.js
index 1a6cabc..3cf652f 100644
--- a/node_modules/@remix-run/dev/compiler.js
+++ b/node_modules/@remix-run/dev/compiler.js
@@ -297,6 +297,7 @@ async function createBrowserBuild(config, options) {
platform: "browser",
format: "esm",
external: externals,
+ jsxFactory: "jsx",
inject: [reactShim],
loader: loaders.loaders,
bundle: true,
@@ -329,6 +330,7 @@ async function createServerBuild(config, options) {
format: config.serverModuleFormat,
mainFields: config.serverModuleFormat === "esm" ? ["module", "main"] : ["main", "module"],
target: options.target,
+ jsxFactory: "jsx",
inject: [reactShim],
loader: loaders.loaders,
bundle: true,
diff --git a/node_modules/@remix-run/dev/compiler/shims/react.ts b/node_modules/@remix-run/dev/compiler/shims/react.ts
index eb0f102..b86e5da 100644
--- a/node_modules/@remix-run/dev/compiler/shims/react.ts
+++ b/node_modules/@remix-run/dev/compiler/shims/react.ts
@@ -1,2 +1,3 @@
+import { jsx } from "@emotion/react";
import * as React from "react";
-export { React };
+export { jsx, React };
diff --git a/node_modules/wrangler/wrangler-dist/cli.js b/node_modules/wrangler/wrangler-dist/cli.js
index a4a2148..f297c4f 100644
--- a/node_modules/wrangler/wrangler-dist/cli.js
+++ b/node_modules/wrangler/wrangler-dist/cli.js
@@ -136277,6 +136277,7 @@ var import_node_path14 = require("node:path");
var import_node_url8 = require("node:url");
var import_chokidar2 = __toESM(require_chokidar());
var import_mime = __toESM(require_mime2());
+var alias = require('esbuild-plugin-alias');
// pages/functions/buildWorker.ts
init_import_meta_url();
@@ -136324,7 +136325,14 @@ function buildWorker({
}
});
}
- }
+ },
+ alias({
+ 'through': require.resolve('no-op'),
+ 'html-tokenize': require.resolve('no-op'),
+ 'multipipe': require.resolve('no-op'),
+ '@emotion/react': require.resolve('@emotion/react'),
+ '@emotion/cache': require.resolve('@emotion/cache'),
+ })
]
});
}
"postinstall": "remix setup cloudflare-pages && npx patch-package", |
Thank you @aiji42 for making and sharing this |
Thanks @aiji42 @dan-cooke However, I went with this patch as this was simple and doesn't change code much (I was afraid of the breaking changes that we may not notice). diff --git a/node_modules/@emotion/cache/package.json b/node_modules/@emotion/cache/package.json
index 437f0bf..c74881a 100644
--- a/node_modules/@emotion/cache/package.json
+++ b/node_modules/@emotion/cache/package.json
@@ -4,10 +4,6 @@
"description": "emotion's cache",
"main": "dist/emotion-cache.cjs.js",
"module": "dist/emotion-cache.esm.js",
- "browser": {
- "./dist/emotion-cache.cjs.js": "./dist/emotion-cache.browser.cjs.js",
- "./dist/emotion-cache.esm.js": "./dist/emotion-cache.browser.esm.js"
- },
"types": "types/index.d.ts",
"license": "MIT",
"repository": "https://github.com/emotion-js/emotion/tree/main/packages/cache", |
it works in dev env, and can be deploy to Cloudflare Pages... 21:29:13.758 | Executing user command: npm run build but i facing new runtime error Do I miss anything? How about your experience? |
Hi, I have the same issue. To @aiji42 @dan-cooke @pavi2410, can you please share more? Possibly it is dependent on some other files. |
It's works for me only partially .... It's fix all these errors but another one happened - [mf:err] ReferenceError: process is not defined as i see its call to process.argv - but i cannot find any source of it!! who put it into worker??? how to fix it???? |
@Andarist how would one use the patch above to make emotion work in this context? Thanks! |
Please prepare a runnable example of the problem with all required steps to setup everything. I will try to find the time to look into this if such is provided |
Some discussion of the issue in the emotion Slack channel.
Current behavior:
Attempting to render a React application using emotion as guided by the documentation will result in the Cloudflare Worker immediately crashing, as described here.
This is because
stream
is not present in a Worker environment.After removing the code in
@emotion/server
which importswhich indirectly importsstream
throughthrough
, the Worker will no longer immediate crash on startup, but will throw an error on rendering:This is due to the Worker bundler targeting the
browser
. Rollup (webpack untested) will optimize awayisBrowser
checks (as it should always be true), which still leaves references todocument
, which is undefined in the Worker environment. This issue is also described here. HardcodingisBrowser = false
results in successful rendering.To reproduce:
Clone this Github repository and follow the README.
Expected behavior:
Ideally,
emotion
+react
+ SSR should work within Workers.Environment information:
react
version: 17.0.2@emotion/react
version: 11.6.0@emotion/server
version: 11.4.0@emotion/cache
version: 11.6.0@emotion/styled
version: 11.6.0The text was updated successfully, but these errors were encountered: