-
-
Notifications
You must be signed in to change notification settings - Fork 334
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 Cloudflare Workers example using esbuild #217
Comments
Is there an official blog post or docs about this? I've seen Clouflare Pages's recent announcement for worker support, but I've not heard of Cloudflare Workers deprecating webpack. As for esbuild, users have been able to bundle vite-plugin-ssr apps with esbuild before; I believe they used other flag settings than the one you are using. Both Vitedge and Rakkas use esbuild to deploy to Cloudflare Workers, so you may find the right flags there.
PR welcome :-). |
Well, technically they're not deprecating it, but rather recommending to specify a custom build section, as noted in the docs for the
Thanks for the hint, I will be checking out Vitedge first (haven't found anything concerning Cloudflare Workers in the Rakkas docs at first sight), although the handling under the hood seems to be different than the said |
👍 I'm up for esbulid.
Not sure, could very well be enough. Keep me updated. |
Since I couldn't find any suitable solution for The main difference between Long story short; the webpack configuration below makes the worker files compile, but // webpack@^5.0.0 config file
const { resolve } = require('path');
module.exports = {
mode: 'production',
entry: './worker/index.js',
target: 'webworker',
resolve: {
fallback: {
// needed polyfills, need to be installed manually via npm or yarn.
buffer: require.resolve('buffer'),
path: require.resolve('path-browserify'),
stream: require.resolve('stream-browserify'),
util: require.resolve('util'),
},
mainFields: ['main', 'module'],
alias: {
// needed, because webpack does not understand exports: { '.': { ... } } in the respective package.json files
'vite-plugin-ssr': require.resolve('vite-plugin-ssr'),
'@brillout/json-s': require.resolve('@brillout/json-s'),
'@brillout/libassert': require.resolve('@brillout/libassert'),
},
},
output: {
path: resolve(__dirname, 'dist/worker'),
filename: 'worker.js',
},
}; So far, I see the biggest hurdles in |
Did you try the We got pretty far here: https://discord.com/channels/815937377888632913/815937377888632916/911370582447235153 |
Actually, we may remove/replace all Node.js dependencies from vite-plugin-ssr's Node.js runtime. I expect this to be farily easy to achieve. |
Yeah, I tried that as well, but since Cloudflare Workers demand a browser environment (since they claim they are technically a Web Worker), I received the following error after executing Error: Something went wrong with the request to Cloudflare...
Uncaught ReferenceError: global is not defined
at line 965 in assertNotAlreadyLoaded
at line 898 in node_modules/vite-plugin-ssr/dist/cjs/shared/getPageFiles.js
at line 16 in __require2
at line 1047 in node_modules/vite-plugin-ssr/dist/cjs/node/page-files/setup.js
at line 16 in __require2
at line 4038 in node_modules/vite-plugin-ssr/dist/cjs/node/index.js
at line 16 in __require2
at line 11947
[API code: 10021] So it's not quite similar to the error mentioned in the Discord channel, but anyhow, I feel like targeting the What could work though is an intermediate build step - in a way like the following:
I don't want to be the smartypants here, I'm sure that I'm not the only one with an abstract idea like this, but that's what came to my mind in regards to this issue. |
I just checked, we only need a shim for Provide me with a shim for both and I'll release a version that has no Node.js dependency.
No probs :-). |
Unfortunately Then there's |
We don't need a Node.js stream polyfill as Cloudflare Workers uses Web Streams. We can dynamicallly load the Node.js stream module only when needed. This circumvents the problem altogether.
We only really need an implementation for We can assume all paths to be posix (Vite normalizes all Windows path to Posix). function isPosixPath(path: string) {
return !path.includes('\\')
}
function isAbsolute(path: string) {
assert(isPosixPath(path))
return path.startsWith('/')
}
function resolve(...paths: string[]) {
/* TODO */
}
That shouldn't be a problem. |
I've tried a few things throughout the last few hours, but it seems conditional import and/or tree-shaking doesn't work too well in That means we need a custom implementation for the following
(Therefore I think it's safer to rely on the browserify's As for the Other than I think I'm stuck here. |
As long as you have
These are used by pre-rendering, the runtime only needs So seems like we can skip all dependencies to Node.js when bundling for Cloudflare Workers. I've limited capacity to work on this until I release Telefunc (an RPC implementation I'm working on). So either wait until I realease Telefunc or PR welcome. In the meantime you can use Cloudflare Worker's webpack bundler right? |
I understand there's no external dependency for the
I totally understand and can't wait to try out Telefunc.
Yup, it should fit for now - I just thought there might be a more streamlined approach using |
@saschazar21 Thinking of creating a |
It's definitely an interesting topic, especially when looking at the latest announcements for Wrangler v2 and Cloudflare full-stack - (also already discussed at frandiox/vitedge#68). However, I'm not sure about creating a new plugin per se — like what would be the purpose? Should it be an enhancement for In terms of Cloudflare Workers, I like the approach of So as far as I'm concerned, an 'adapter'-like mechanism could serve I'd be glad to support, although I have little to no knowledge about Vite's SSR platform, so starting something all on my own without a rough roadmap will probably end in nothing too serious. |
Standalone plugin that can be used with any Vite app (with or without FYI vitejs/vite#5936 in case you're up for making a Vite contribution. Also, I will remove the Node.js dependencies from the runtime tomorrow. We'll then be able to resume our progress on this. |
Hi @brillout - thanks for your effort on this topic and sorry for not being that responsive during the past days. I will take a look how different it integrates with my current setup asap. Afterwards I will happily report on the outcome. I've read your new issue concerning the |
I've tried it again using a few more different settings. I think the main issue with I'm not entirely sure, but it seems to me, that due to the CommonJS format of the Since |
Actually simply setting Let me know how it went. There is also https://tsup.egoist.sh/ but I'm not familiar with it and whether it supports what we need. |
I tried with I haven't had time to dig deeper into tsup, but after a first look it seems like |
I will be removing all Node.js dependencies (/ making them optional) next week. I'll keep you updated. |
FYI cloudflare/wrangler-legacy#2158. Btw. There is https://github.com/calvinmetcalf/rollup-plugin-node-builtins. So
But none of them seem to be using |
Thanks for the update. I'll be taking a look asap |
Since Cloudflare is deprecating their preconfigured
webpack
&rust
build environments in favor of custom build scripts by settingtype = "javascript"
inwrangler.toml
, I tried bundling the workers code usingesbuild
, since it's already part of Vite.I followed the tutorial in the vite-plugin-ssr docs using the provided
examples/cloudflare-workers
directory in this repo. For the build step, I tried to executeesbuild
using the following command:Unfortunately, esbuild is complaining that it could not resolve a number of modules, including
vite-plugin-ssr
in thedist/server/importBuild.js
file, as well as of course Node.js-native modules, such aspath
&stream
.I received similar errors when using
webpack@^5.0.0
and the providedwebpack.config.js
from the cloudflare workers examples.Is it possible to update and/or add an example for Cloudflare Workers using
esbuild
as bundler?SvelteKit's adapter-cloudflare works quite flawlessly, but I haven't found a way to port the functionality to
vite-plugin-ssr
yet...The text was updated successfully, but these errors were encountered: