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

image

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

Most upvoted comments

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 highlight module is not present in the server bundle.

All you have to do now is edit the vite.config.js:

import { sveltekit } from '@sveltejs/kit/vite';

/** @type {import('vite').UserConfig} */
const config = {
	plugins: [sveltekit()],
	define: {
            __ENABLE_CARTA_SSR_HIGHLIGHTER__   : false
	}
};

export default config;

Good job everyone. Have a nice day!

PR#84 adds the changes derived from test 006.

I built this version of carta-md and copied its dist/ into the same minimal SvelteKit project used for testing. When compiled with adapter-cloudflare, the resulting _worker.js is still only 693 KB, and the resulting build runs as expected in wrangler.

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 MarkdownEditor component, 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 a Markdown (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.js file 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.js file 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-bundle branch 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. image 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 when allowSSR is 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

export default defineConfig({
  plugins: [sveltekit()],

  define: {
    __ENABLE_CARTA_SSR_ASYNC_PLUGINS__ : 'true',
    __ENABLE_CARTA_SSR_HIGHLIGHTER__   : 'false'
  }
});
  • __ENABLE_CARTA_SSR_ASYNC_PLUGINS__ allows async plugins to be run during SSR by a direct call to carta.render(markdown)
    • there’s no penalty for enabling this flag, even if this feature is not used
  • __ENABLE_CARTA_SSR_HIGHLIGHTER__ allows the async plugin plugin-code to be run during SSR
    • this is only meaningful when __ENABLE_CARTA_SSR_ASYNC_PLUGINS__ is true
    • enabling this flag causes shiki to be included in the server bundle… increasing its size to 10 MB
    • disabling this flag causes the async plugin plugin-code to noop when attempted to be run during SSR

I’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:

  • when __ENABLE_CARTA_SSR_HIGHLIGHTER__ is false,
    .svelte-kit/cloudflare/_worker.js is 693 KB
  • when __ENABLE_CARTA_SSR_HIGHLIGHTER__ is true,
    .svelte-kit/cloudflare/_worker.js is 10 MB

To be fair:

  • when a completely empty SvelteKit project that doesn’t include carta is compiled,
    .svelte-kit/cloudflare/_worker.js is 139 KB

I’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:

  "dependencies": {
    "@cartamd/plugin-code": "^4.0.5",
    "carta-md": "^4.2.1"
  }

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:

  1412, "// node_modules/bail/index.js"
  1423, "// node_modules/extend/index.js"
  1514, "// node_modules/devlop/lib/default.js"
  1522, "// node_modules/is-plain-obj/index.js"
  1535, "// node_modules/trough/lib/index.js"
  1623, "// node_modules/trough/index.js"
  1630, "// node_modules/unist-util-stringify-position/lib/index.js"
  1660, "// node_modules/unist-util-stringify-position/index.js"
  1667, "// node_modules/vfile-message/lib/index.js"
  1809, "// node_modules/vfile-message/index.js"
  1816, "// node_modules/vfile/lib/minpath.browser.js"
  2034, "// node_modules/vfile/lib/minproc.browser.js"
  2045, "// node_modules/vfile/lib/minurl.shared.js"
  2057, "// node_modules/vfile/lib/minurl.browser.js"
  2106, "// node_modules/vfile/lib/index.js"
  2569, "// node_modules/vfile/index.js"
  2576, "// node_modules/unified/lib/callable-instance.js"
  2608, "// node_modules/unified/lib/index.js"
  3256, "// node_modules/unified/index.js"
  3263, "// node_modules/mdast-util-to-string/lib/index.js"
  3305, "// node_modules/mdast-util-to-string/index.js"
  3312, "// node_modules/character-entities/index.js"
  5446, "// node_modules/decode-named-character-reference/index.js"
  5458, "// node_modules/micromark-util-chunked/index.js"
  5497, "// node_modules/micromark-util-combine-extensions/index.js"
  5544, "// node_modules/micromark-util-decode-numeric-character-reference/index.js"
  5566, "// node_modules/micromark-util-normalize-identifier/index.js"
  5575, "// node_modules/micromark-util-character/index.js"
  5612, "// node_modules/micromark-util-sanitize-uri/index.js"
  5656, "// node_modules/micromark-factory-space/index.js"
  5683, "// node_modules/micromark/lib/initialize/content.js"
  5744, "// node_modules/micromark/lib/initialize/document.js"
  5963, "// node_modules/micromark-util-classify-character/index.js"
  5978, "// node_modules/micromark-util-resolve-all/index.js"
  5996, "// node_modules/micromark-core-commonmark/lib/attention.js"
  6115, "// node_modules/micromark-core-commonmark/lib/autolink.js"
  6222, "// node_modules/micromark-core-commonmark/lib/blank-line.js"
  6244, "// node_modules/micromark-core-commonmark/lib/block-quote.js"
  6309, "// node_modules/micromark-core-commonmark/lib/character-escape.js"
  6341, "// node_modules/micromark-core-commonmark/lib/character-reference.js"
  6413, "// node_modules/micromark-core-commonmark/lib/code-fenced.js"
  6603, "// node_modules/micromark-core-commonmark/lib/code-indented.js"
  6674, "// node_modules/micromark-core-commonmark/lib/code-text.js"
  6795, "// node_modules/micromark-util-subtokenize/lib/splice-buffer.js"
  6996, "// node_modules/micromark-util-subtokenize/index.js"
  7146, "// node_modules/micromark-core-commonmark/lib/content.js"
  7225, "// node_modules/micromark-factory-destination/index.js"
  7322, "// node_modules/micromark-factory-label/index.js"
  7389, "// node_modules/micromark-factory-title/index.js"
  7457, "// node_modules/micromark-factory-whitespace/index.js"
  7486, "// node_modules/micromark-core-commonmark/lib/definition.js"
  7584, "// node_modules/micromark-core-commonmark/lib/hard-break-escape.js"
  7611, "// node_modules/micromark-core-commonmark/lib/heading-atx.js"
  7710, "// node_modules/micromark-util-html-tag-name/index.js"
  7782, "// node_modules/micromark-core-commonmark/lib/html-flow.js"
  8169, "// node_modules/micromark-core-commonmark/lib/html-text.js"
  8482, "// node_modules/micromark-core-commonmark/lib/label-end.js"
  8704, "// node_modules/micromark-core-commonmark/lib/label-start-image.js"
  8741, "// node_modules/micromark-core-commonmark/lib/label-start-link.js"
  8769, "// node_modules/micromark-core-commonmark/lib/line-ending.js"
  8790, "// node_modules/micromark-core-commonmark/lib/thematic-break.js"
  8836, "// node_modules/micromark-core-commonmark/lib/list.js"
  8975, "// node_modules/micromark-core-commonmark/lib/setext-underline.js"
  9068, "// node_modules/micromark-core-commonmark/index.js"
  9096, "// node_modules/micromark/lib/initialize/flow.js"
  9153, "// node_modules/micromark/lib/initialize/text.js"
  9298, "// node_modules/micromark/lib/create-tokenizer.js"
  9624, "// node_modules/micromark/lib/constructs.js"
  9706, "// node_modules/micromark/lib/parse.js"
  9743, "// node_modules/micromark/lib/postprocess.js"
  9755, "// node_modules/micromark/lib/preprocess.js"
  9842, "// node_modules/micromark/index.js"
  9851, "// node_modules/micromark-util-decode-string/index.js"
  9876, "// node_modules/mdast-util-from-markdown/lib/index.js"
  10601, "// node_modules/mdast-util-from-markdown/index.js"
  10608, "// node_modules/remark-parse/lib/index.js"
  10630, "// node_modules/remark-parse/index.js"
  10637, "// node_modules/ccount/index.js"
  10656, "// node_modules/mdast-util-find-and-replace/node_modules/escape-string-regexp/index.js"
  10668, "// node_modules/unist-util-is/lib/index.js"
  10765, "// node_modules/unist-util-is/index.js"
  10772, "// node_modules/unist-util-visit-parents/lib/color.js"
  10781, "// node_modules/unist-util-visit-parents/lib/index.js"
  10865, "// node_modules/unist-util-visit-parents/index.js"
  10872, "// node_modules/mdast-util-find-and-replace/lib/index.js"
  10984, "// node_modules/mdast-util-find-and-replace/index.js"
  10991, "// node_modules/mdast-util-gfm-autolink-literal/lib/index.js"
  11150, "// node_modules/mdast-util-gfm-autolink-literal/index.js"
  11157, "// node_modules/mdast-util-gfm-footnote/lib/index.js"
  11277, "// node_modules/mdast-util-gfm-footnote/index.js"
  11284, "// node_modules/mdast-util-gfm-strikethrough/lib/index.js"
  11341, "// node_modules/mdast-util-gfm-strikethrough/index.js"
  11348, "// node_modules/markdown-table/index.js"
  11492, "// node_modules/zwitch/index.js"
  11518, "// node_modules/mdast-util-to-markdown/lib/handle/blockquote.js"
  11539, "// node_modules/mdast-util-to-markdown/lib/util/pattern-in-scope.js"
  11563, "// node_modules/mdast-util-to-markdown/lib/handle/break.js"
  11579, "// node_modules/longest-streak/index.js"
  11607, "// node_modules/mdast-util-to-markdown/lib/util/format-code-as-indented.js"
  11621, "// node_modules/mdast-util-to-markdown/lib/util/check-fence.js"
  11636, "// node_modules/mdast-util-to-markdown/lib/handle/code.js"
  11695, "// node_modules/mdast-util-to-markdown/lib/util/check-quote.js"
  11710, "// node_modules/mdast-util-to-markdown/lib/handle/definition.js"
  11771, "// node_modules/mdast-util-to-markdown/lib/util/check-emphasis.js"
  11786, "// node_modules/mdast-util-to-markdown/lib/handle/emphasis.js"
  11813, "// node_modules/unist-util-visit/lib/index.js"
  11841, "// node_modules/unist-util-visit/index.js"
  11848, "// node_modules/mdast-util-to-markdown/lib/util/format-heading-as-setext.js"
  11868, "// node_modules/mdast-util-to-markdown/lib/handle/heading.js"
  11915, "// node_modules/mdast-util-to-markdown/lib/handle/html.js"
  11928, "// node_modules/mdast-util-to-markdown/lib/handle/image.js"
  11990, "// node_modules/mdast-util-to-markdown/lib/handle/image-reference.js"
  12033, "// node_modules/mdast-util-to-markdown/lib/handle/inline-code.js"
  12069, "// node_modules/mdast-util-to-markdown/lib/util/format-link-as-autolink.js"
  12089, "// node_modules/mdast-util-to-markdown/lib/handle/link.js"
  12175, "// node_modules/mdast-util-to-markdown/lib/handle/link-reference.js"
  12218, "// node_modules/mdast-util-to-markdown/lib/util/check-bullet.js"
  12233, "// node_modules/mdast-util-to-markdown/lib/util/check-bullet-other.js"
  12258, "// node_modules/mdast-util-to-markdown/lib/util/check-bullet-ordered.js"
  12273, "// node_modules/mdast-util-to-markdown/lib/util/check-rule.js"
  12288, "// node_modules/mdast-util-to-markdown/lib/handle/list.js"
  12336, "// node_modules/mdast-util-to-markdown/lib/util/check-list-item-indent.js"
  12351, "// node_modules/mdast-util-to-markdown/lib/handle/list-item.js"
  12386, "// node_modules/mdast-util-to-markdown/lib/handle/paragraph.js"
  12400, "// node_modules/mdast-util-phrasing/lib/index.js"
  12432, "// node_modules/mdast-util-phrasing/index.js"
  12439, "// node_modules/mdast-util-to-markdown/lib/handle/root.js"
  12453, "// node_modules/mdast-util-to-markdown/lib/util/check-strong.js"
  12468, "// node_modules/mdast-util-to-markdown/lib/handle/strong.js"
  12495, "// node_modules/mdast-util-to-markdown/lib/handle/text.js"
  12504, "// node_modules/mdast-util-to-markdown/lib/util/check-rule-repetition.js"
  12519, "// node_modules/mdast-util-to-markdown/lib/handle/thematic-break.js"
  12531, "// node_modules/mdast-util-to-markdown/lib/handle/index.js"
  12579, "// node_modules/mdast-util-to-markdown/index.js"
  12586, "// node_modules/mdast-util-gfm-table/lib/index.js"
  12746, "// node_modules/mdast-util-gfm-table/index.js"
  12753, "// node_modules/mdast-util-gfm-task-list-item/lib/index.js"
  12832, "// node_modules/mdast-util-gfm-task-list-item/index.js"
  12839, "// node_modules/mdast-util-gfm/lib/index.js"
  12870, "// node_modules/mdast-util-gfm/index.js"
  12877, "// node_modules/micromark-extension-gfm-autolink-literal/lib/syntax.js"
  13232, "// node_modules/micromark-extension-gfm-autolink-literal/index.js"
  13239, "// node_modules/micromark-extension-gfm-footnote/lib/syntax.js"
  13532, "// node_modules/micromark-extension-gfm-footnote/index.js"
  13539, "// node_modules/micromark-extension-gfm-strikethrough/lib/syntax.js"
  13656, "// node_modules/micromark-extension-gfm-strikethrough/index.js"
  13663, "// node_modules/micromark-extension-gfm-table/lib/edit-map.js"
  13748, "// node_modules/micromark-extension-gfm-table/lib/infer.js"
  13781, "// node_modules/micromark-extension-gfm-table/lib/syntax.js"
  14202, "// node_modules/micromark-extension-gfm-table/index.js"
  14209, "// node_modules/micromark-extension-gfm-task-list-item/lib/syntax.js"
  14293, "// node_modules/micromark-extension-gfm-task-list-item/index.js"
  14300, "// node_modules/micromark-extension-gfm/index.js"
  14321, "// node_modules/remark-gfm/lib/index.js"
  14345, "// node_modules/remark-gfm/index.js"
  14352, "// node_modules/mdast-util-to-hast/lib/handlers/blockquote.js"
  14369, "// node_modules/mdast-util-to-hast/lib/handlers/break.js"
  14381, "// node_modules/mdast-util-to-hast/lib/handlers/code.js"
  14409, "// node_modules/mdast-util-to-hast/lib/handlers/delete.js"
  14426, "// node_modules/mdast-util-to-hast/lib/handlers/emphasis.js"
  14443, "// node_modules/mdast-util-to-hast/lib/handlers/footnote-reference.js"
  14487, "// node_modules/mdast-util-to-hast/lib/handlers/heading.js"
  14504, "// node_modules/mdast-util-to-hast/lib/handlers/html.js"
  14519, "// node_modules/mdast-util-to-hast/lib/revert.js"
  14552, "// node_modules/mdast-util-to-hast/lib/handlers/image-reference.js"
  14574, "// node_modules/mdast-util-to-hast/lib/handlers/image.js"
  14593, "// node_modules/mdast-util-to-hast/lib/handlers/inline-code.js"
  14612, "// node_modules/mdast-util-to-hast/lib/handlers/link-reference.js"
  14639, "// node_modules/mdast-util-to-hast/lib/handlers/link.js"
  14660, "// node_modules/mdast-util-to-hast/lib/handlers/list-item.js"
  14728, "// node_modules/mdast-util-to-hast/lib/handlers/list.js"
  14758, "// node_modules/mdast-util-to-hast/lib/handlers/paragraph.js"
  14775, "// node_modules/mdast-util-to-hast/lib/handlers/root.js"
  14787, "// node_modules/mdast-util-to-hast/lib/handlers/strong.js"
  14804, "// node_modules/unist-util-position/lib/index.js"
  14833, "// node_modules/unist-util-position/index.js"
  14840, "// node_modules/mdast-util-to-hast/lib/handlers/table.js"
  14883, "// node_modules/mdast-util-to-hast/lib/handlers/table-row.js"
  14922, "// node_modules/mdast-util-to-hast/lib/handlers/table-cell.js"
  14940, "// node_modules/trim-lines/index.js"
  14985, "// node_modules/mdast-util-to-hast/lib/handlers/text.js"
  14997, "// node_modules/mdast-util-to-hast/lib/handlers/thematic-break.js"
  15014, "// node_modules/mdast-util-to-hast/lib/handlers/index.js"
  15077, "// node_modules/@ungap/structured-clone/esm/types.js"
  15094, "// node_modules/@ungap/structured-clone/esm/deserialize.js"
  15160, "// node_modules/@ungap/structured-clone/esm/serialize.js"
  15290, "// node_modules/@ungap/structured-clone/esm/index.js"
  15303, "// node_modules/mdast-util-to-hast/lib/footer.js"
  15419, "// node_modules/mdast-util-to-hast/lib/state.js"
  15572, "// node_modules/mdast-util-to-hast/lib/index.js"
  15592, "// node_modules/mdast-util-to-hast/index.js"
  15599, "// node_modules/remark-rehype/lib/index.js"
  15623, "// node_modules/remark-rehype/index.js"
  15630, "// node_modules/html-void-elements/index.js"
  15659, "// node_modules/property-information/lib/util/schema.js"
  15684, "// node_modules/property-information/lib/util/merge.js"
  15701, "// node_modules/property-information/lib/normalize.js"
  15710, "// node_modules/property-information/lib/util/info.js"
  15738, "// node_modules/property-information/lib/util/types.js"
  15766, "// node_modules/property-information/lib/util/defined-info.js"
  15802, "// node_modules/property-information/lib/util/create.js"
  15836, "// node_modules/property-information/lib/xlink.js"
  15859, "// node_modules/property-information/lib/xml.js"
  15874, "// node_modules/property-information/lib/util/case-sensitive-transform.js"
  15883, "// node_modules/property-information/lib/util/case-insensitive-transform.js"
  15893, "// node_modules/property-information/lib/xmlns.js"
  15908, "// node_modules/property-information/lib/aria.js"
  15973, "// node_modules/property-information/lib/html.js"
  16345, "// node_modules/property-information/lib/svg.js"
  16915, "// node_modules/property-information/lib/find.js"
  16959, "// node_modules/property-information/index.js"
  16976, "// node_modules/stringify-entities/lib/core.js"
  17029, "// node_modules/stringify-entities/lib/util/to-hexadecimal.js"
  17041, "// node_modules/stringify-entities/lib/util/to-decimal.js"
  17053, "// node_modules/character-entities-legacy/index.js"
  17168, "// node_modules/character-entities-html4/index.js"
  17429, "// node_modules/stringify-entities/lib/constant/dangerous.js"
  17446, "// node_modules/stringify-entities/lib/util/to-named.js"
  17476, "// node_modules/stringify-entities/lib/util/format-smart.js"
  17504, "// node_modules/stringify-entities/lib/index.js"
  17515, "// node_modules/stringify-entities/index.js"
  17522, "// node_modules/hast-util-to-html/lib/handle/comment.js"
  17549, "// node_modules/hast-util-to-html/lib/handle/doctype.js"
  17559, "// node_modules/comma-separated-tokens/index.js"
  17572, "// node_modules/space-separated-tokens/index.js"
  17581, "// node_modules/hast-util-whitespace/lib/index.js"
  17595, "// node_modules/hast-util-whitespace/index.js"
  17602, "// node_modules/hast-util-to-html/lib/omission/util/siblings.js"
  17628, "// node_modules/hast-util-to-html/lib/omission/omission.js"
  17642, "// node_modules/hast-util-to-html/lib/omission/closing.js"
  17737, "// node_modules/hast-util-to-html/lib/omission/opening.js"
  17793, "// node_modules/hast-util-to-html/lib/handle/element.js"
  17936, "// node_modules/hast-util-to-html/lib/handle/text.js"
  17953, "// node_modules/hast-util-to-html/lib/handle/raw.js"
  17963, "// node_modules/hast-util-to-html/lib/handle/root.js"
  17973, "// node_modules/hast-util-to-html/lib/handle/index.js"
  18002, "// node_modules/hast-util-to-html/lib/index.js"
  18066, "// node_modules/hast-util-to-html/index.js"
  18073, "// node_modules/rehype-stringify/lib/index.js"
  18088, "// node_modules/rehype-stringify/index.js"
  18095, "// node_modules/hast-util-to-string/lib/index.js"
  18121, "// node_modules/hast-util-to-string/index.js"
  18128, "// node_modules/@shikijs/rehype/dist/core.mjs"
  18206, "// node_modules/@cartamd/plugin-code/dist/index.js"

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:

  1. It makes the changes optional (so that the highlighter is available by default), and still allows carta.render to work server-side;
  2. Does not use $browser.

I’ll happily proceed with that.

meta.import.env might 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" package is because you have not rebuilt the package; I added the exports for those entrypoints and it was working for me

  1. PR #85 test at 4621830 w/ bundle: web
    • +page.svelte:
         <script lang="ts">
           import { Carta, MarkdownEditor } from 'carta-md'
           import { code } from '@cartamd/plugin-code'
      
           import 'carta-md/default.css'
      
           const carta = new Carta({
             extensions: [
               code({
                 onError: (e) => {console.log(e.message)}
               })
             ],
             shikiOptions: {
               bundle: () => import('carta-md/bundle/web')
             }
           })
      
           let value
         </script>
      
         <MarkdownEditor mode="split" bind:value {carta} />
      
    • results:
      • error during build:
        [commonjs--resolver] Missing "./bundle/web" specifier in "carta-md" package
    • note:
      • package.json needs to add an export for these new bundles
    • for example:
         "exports": {
           ".": {
             "types": "./dist/index.d.ts",
             "svelte": "./dist/index.js",
             "import": "./dist/index.js"
           },
           "./default.css": "./dist/default.css",
           "./bundle/full": "./dist/bundle/full.js",
           "./bundle/web": "./dist/bundle/web.js"
         }
      
    • results (after updating package.json):
      • .svelte-kit/cloudflare/_worker.js:
        • size: 10.0 MB

Hello, 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 (getHighlighterCore instead of getHighlighter). I would have to add some sort of check to make sure plugin-code does not try to load a language that is not bundled.

Replying to @warren-bank

please correct me if this is a crazy or bad idea (I’m still very new to Svelte)… but wouldn’t it be possible to wrap all of the places that initialize Shiki in a conditional: if (browser)… and completely tree shake Shiki from the server-side bundle?

related:

* [doc](https://kit.svelte.dev/docs/faq#how-do-i-use-x-with-sveltekit-how-do-i-use-a-client-side-only-library-that-depends-on-document-or-window)

* [issue](https://github.com/sveltejs/kit/issues/2468)

doing a quick grep of the source…

* `highlighter.ts`
  
  * imports `getHighlighter` from `shiki`
  * exports `loadHighlighter`, which is the one and only place that calls `getHighlighter`

* `carta.ts`
  
  * has a public method `highlighter()`, which is the one and only place that calls `loadHighlighter`

* `Input.svelte`
  
  * calls `carta.highlighter()` in: `highlight()` and `highlightNestedLanguages()`
  * neither of which are export
  * called only from:
    
    * `onMount()`
      
      * which isn't called server-side and in the process of tree shaking gets replaced by a no-op
    * `$: value; ...`
      
      * which could easily be updated to: `if (browser) onValueChange(value)`

* `carta.ts`
  
  * has a public method `render()`, which calls `carta.highlighter()`

* `Renderer.svelte`
  
  * calls `carta.render()` from `debouncedRenderer`
  * which is not exported
  * called only from:
    
    * `$: value; ...`
      
      * which could easily be updated to: `if (browser) onValueChange(value)`

* `Markdown.svelte`
  
  * calls `carta.render()`
  * called only from:
    
    * `onMount()`
      
      * which isn't called server-side and in the process of tree shaking gets replaced by a no-op

…and that’s it, although index.ts would probably also need to remove: export * from '$lib/internal/highlight'

so… in theory, a few minor tweaks and Shiki should get shook… from the server bundle. any thoughts?

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 .wasm doesn’t work without a little workaround.

deeper dive..

Although, I should probably point out that this workaround still involves importing a .wasm file that is 455 KB:

update: conflicting info