carta: Cannot run with Cloudflare Pages
I’m unable to use this package with Cloudflare Pages since the bundle size becomes too large, this is probably due to shiki taking up a large amount of space.
I’d like to mention that I found some documentation that might be helpful, according to the shiki docs it’s possible to use a “fine-grained bundle” to reduce the bundle size: shiki.style/guide/install#fine-grained-bundle
I’m using SvelteKit along with the latest version of carta (4.2.1), I’ve included some error messages along with some info on the bundle size below.
Output of rollup-plugin-visualizer
Vite output
.svelte-kit/output/client/_app/immutable/chunks/wasm.CsTmP73Z.js 622.30 kB │ gzip: 231.14 kB
.svelte-kit/output/client/_app/immutable/chunks/cpp.CiNIqgXH.js 628.27 kB │ gzip: 48.41 kB
.svelte-kit/output/client/_app/immutable/chunks/emacs-lisp.BEjL32p1.js 807.64 kB │ gzip: 203.35 kB
(!) Some chunks are larger than 500 kB after minification. Consider:
- Using dynamic import() to code-split the application
- Use build.rollupOptions.output.manualChunks to improve chunking: https://rollupjs.org/configuration-options/#output-manualchunks
- Adjust chunk size limit for this warning via build.chunkSizeWarningLimit.
Cloudflare
Error: Failed to publish your Function. Got error: Your Functions script is over the 1 MiB size limit (workers.api.error.script_too_large)
Thank you in advance.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 42 (35 by maintainers)
Commits related to this issue
- remove shiki from server bundle fix #78 by reducing size of '.svelte-kit/cloudflare/_worker.js' when using either: * 'adapter-cloudflare' * 'adapter-auto' with the environment variable: `CF_PAGES... — committed to warren-bank/fork-js-svelte-carta by warren-bank 2 years ago
- remove shiki from server bundle fix #78 by reducing size of '.svelte-kit/cloudflare/_worker.js' when using either: * 'adapter-cloudflare' * 'adapter-auto' with the environment variable: `CF_PAGES... — committed to BearToCode/carta by warren-bank 2 years ago
You’re right, this is becoming more complex than it need to be. I closed my PR and worked a bit on yours: I cleaned it up a bit (I also think your intellisense does not work correctly), I removed the
__ENABLE_CARTA_SSR_ASYNC_PLUGINS_, as it didn’t seem really necessary.I tested it locally and it seems to work perfectly! The
highlightmodule is not present in the server bundle.All you have to do now is edit the
vite.config.js:Good job everyone. Have a nice day!
PR#84 adds the changes derived from
test 006.I built this version of
carta-mdand copied itsdist/into the same minimal SvelteKit project used for testing. When compiled withadapter-cloudflare, the resulting_worker.jsis still only693 KB, and the resulting build runs as expected inwrangler.update: I created a free Cloudflare account and uploaded a demo: here. It’s super minimal, but it does include a server-side component… just to test that it works. The page starts with a
MarkdownEditorcomponent, and when the markdown value is non-empty, a submit button is visible. When clicked, the content of the markdown editor is POST to the backend, which is then made available to the frontend. When the frontend sees that markdown has been made available by the backend, it displays its content in aMarkdown(readonly) component.@KyleFontenot , thanks for the clarification. Knowing that the size of this one file is the bottleneck, I’ll do some experimentation with building a minimal SvelteKit project that uses carta… and watch the effects on its size.
Hey there. Yes the 1MB limit only pertains to the
_workers.jsfile specifically. (After compression. And it may have bumped up to 1.5MB)So if you want to try externalizing the WASM with Vite as an asset, might be worth trying. Not sure if the adapter will bundle it anyways because its a module import.
As it is right now, if we can import the shiki wasm source from a separate Cloudflare Worker, or perhaps import it as an asset, and then remove most of the bundled languages, it’d work. When I first pushed to Cloudflare Pages yesterday, my
_workers.jsfile was 11MB. It took completely removing the wasm, all the languages and nulling some of Carta’s highlighting references, to be able to build and deploy successfully.Hi, I can confirm that I was doing the testing with the
custom-bundlebranch that’s part of #85, I tried my best to build the package myself and used it in my project (this was my first time using pnpm)Here’s a map of what’s taking up space, it refers to a worker file that’s 1.7MiB.
Looks like shiki is still taking up most of the space, at least with my project.
I’d be curious to know what idea you have in mind.
As you indicated, I’ve made a few new commits.
Based on my tests, the only way to tree shake shiki is if there’s absolutely no path of execution by which the server code could possibly use highlighter. It doesn’t matter if there is a path of execution and the project doesn’t use it. For example, my first attempt was to add a 2nd parameter to
carta.render(markdown, allowSSR). The idea was that if a project never tries to render async plugins on the server, then the path of execution that is followed whenallowSSRis passed a truthy value isn’t used… and it would be eliminated by the compiler. However, that didn’t happen.The only solution that I could think of was to define global constants at compile time. Code paths of execution that cannot be reached because of these contant values are successfully removed from the bundle. As such, I defined two. Their values are defined in the file: vite.config.js
__ENABLE_CARTA_SSR_ASYNC_PLUGINS__allows async plugins to be run during SSR by a direct call tocarta.render(markdown)__ENABLE_CARTA_SSR_HIGHLIGHTER__allows the async pluginplugin-codeto be run during SSR__ENABLE_CARTA_SSR_ASYNC_PLUGINS__is trueshikito be included in the server bundle… increasing its size to10 MBplugin-codeto noop when attempted to be run during SSRI’d agree with you… that requiring additional configuration is annoying. But on the other hand, it’s only 4 lines… and gives the user very fine grain control. Also, strictly speaking, unless a user actually attempts to call
carta.render()from SSR… nothing bad would happen if these flags were left undefined… though I wouldn’t recommend it.To be clear:
__ENABLE_CARTA_SSR_HIGHLIGHTER__is false,.svelte-kit/cloudflare/_worker.js is
693 KB__ENABLE_CARTA_SSR_HIGHLIGHTER__is true,.svelte-kit/cloudflare/_worker.js is
10 MBTo be fair:
.svelte-kit/cloudflare/_worker.js is
139 KBI’d be curious to see whether this size could be reduced farther, possibly with the use of additional globally constant boolean flags.
content of
693 KB_worker.js file..for a project:
grep’ing _worker.js content for:
// node_modules/and removing the few modules that are also included in an empty template project…
to produce a pseudo table-of-contents w/ leading line numbers:
ohhh, ok… reading docs, it’s such a crazy idea that it might just work
reminds me of my early days in perl… where my answer to nearly every noob question was: RTFM
Well, I guess that’s as much as I could do leaving shiki in the package.
If you update you PR so that:
carta.renderto work server-side;$browser.I’ll happily proceed with that.
meta.import.envmight be interesting: it looks to be tree-shakable. (Edit: it might be removed during build, it’s better to check that. In that case, maybe some vite/rollup plugin may be useful)PS: I think the error
[commonjs--resolver] Missing "./bundle/web" specifier in "carta-md" packageis because you have not rebuilt the package; I added the exports for those entrypoints and it was working for me4621830w/ bundle: web[commonjs--resolver] Missing "./bundle/web" specifier in "carta-md" package10.0 MBHello, thanks everyone for the help.
I’ll be quite busy for a couple of days, but then I could roll out a fix that would allow using an custom bundle (
getHighlighterCoreinstead ofgetHighlighter). I would have to add some sort of check to make sureplugin-codedoes not try to load a language that is not bundled.Replying to @warren-bank
I’m pretty sure this won’t work, as even if the languages are only used in the client, they would still have to be bundled, so that the client can request them when needed.Edit: this should fix the issue with the large bundle size, but we’ll have to see about the wasm import support, as I don’t know how we could approach that
PS: although the intention of my previous comment is to prevent including Shiki in the server-side bundle that gets uploaded to and executed by Cloudflare Workers, because (I believe that…) it isn’t even used when performing SSR’ing… the following is info that I found regarding the correct way to do so:
Shiki has a document that specifically addresses this.
Apparently, the way it imports
.wasmdoesn’t work without a little workaround.deeper dive..
shikijs/core/src
shikijs/shiki/src
shikijs/shiki/src/index.ts
shikijs/shiki/src/bundle-full.ts
shikijs/shiki/src/bundle-full.ts
shikijs/shiki/src/wasm-dynamic.ts
shikijs/shiki/src/wasm.ts
shikijs/core/src/wasm-inlined.ts
cloudflare: issue
Although, I should probably point out that this workaround still involves importing a
.wasmfile that is 455 KB:update: conflicting info