-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix: fix error with <Prism /> component in Cloudflare Workers
#15723
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
Changes from 20 commits
9e180c5
8b7157c
0a5445d
bf6b547
d57df04
fe8ec4f
da6244c
86564b3
9e098d4
e334379
8efd4b2
79d01a3
3b1a04a
37b971f
68f5b01
df376c0
368adda
782794c
8994f97
eabe7ed
d187c79
946c307
f63e1cf
ea86b2b
1e1d716
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@astrojs/markdoc': patch | ||
| '@astrojs/markdown-remark': patch | ||
| --- | ||
|
|
||
| Updates internal type usage from `@astrojs/prism`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@astrojs/prism': patch | ||
| '@astrojs/cloudflare': patch | ||
| --- | ||
|
|
||
| Fixes an issue where the `<Prism />` component failed to work in Cloudflare Workers. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import prismLoadLanguages from 'prismjs/components/index.js'; | ||
|
|
||
| export async function loadLanguages(languages: string | string[]) { | ||
| return prismLoadLanguages(languages); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // This implementation was based from: https://github.com/PrismJS/prism/blob/76dde18a575831c91491895193f56081ac08b0c5/components/index.js | ||
| import Prism from 'prismjs'; | ||
| import components from 'prismjs/components.js'; | ||
| import getLoader, { type LoadChainer } from 'prismjs/dependencies.js'; | ||
| import { bundledLanguages } from 'virtual:astro-cloudflare:prism'; | ||
|
|
||
| // This `loadChainer` is required when working with Promises in Prism's loader. | ||
| // ref: https://github.com/PrismJS/prism/blob/76dde18a575831c91491895193f56081ac08b0c5/dependencies.js#L346-L360 | ||
| const loadChainer: LoadChainer<Promise<void>> = { | ||
| series: async (before, after) => { | ||
| await before; | ||
| await after(); | ||
| }, | ||
| parallel: async (values) => { | ||
| await Promise.all(values); | ||
| }, | ||
| }; | ||
|
|
||
| // Since Prism language files are written assuming the Prism instance is defined | ||
| // as a global variable, we will temporarily set the Prism instance globally. | ||
| function setPrismAsGlobal() { | ||
| globalThis.Prism = Prism; | ||
|
|
||
| return () => { | ||
| // @ts-expect-error globalThis type | ||
| delete globalThis.Prism; | ||
| }; | ||
| } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested this with the preview build and it mostly works fine. But I occasionally encountered the error I believe the cause is that after I also tried verifying the behavior with the node adapter, I read I have two proposals in mind:
For now, I'd like to go with option 2, prioritizing keeping |
||
|
|
||
| /** | ||
| * The set of all languages which have been loaded using the below function. | ||
| * | ||
| * @type {Set<string>} | ||
| */ | ||
| const loadedLanguages = new Set<string>(); | ||
|
|
||
| /** | ||
| * Loads the given languages and adds them to the current Prism instance. | ||
| * | ||
| * If no languages are provided, __all__ Prism languages will be loaded. | ||
| * | ||
| * @param {string|string[]} [languages] | ||
| * @returns {Promise<void>} | ||
| */ | ||
| export async function loadLanguages(languages: string | string[]) { | ||
| const cleanUp = setPrismAsGlobal(); | ||
|
|
||
| if (languages === undefined) { | ||
| languages = Object.keys(components.languages).filter((l) => l !== 'meta'); | ||
| } else if (!Array.isArray(languages)) { | ||
| languages = [languages]; | ||
| } | ||
|
|
||
| // the user might have loaded languages via some other way or used `prism.js` which already includes some | ||
| // we don't need to validate the ids because `getLoader` will ignore invalid ones | ||
| const loaded = [...loadedLanguages, ...Object.keys(Prism.languages)]; | ||
|
|
||
| await getLoader(components, languages, loaded).load(async (lang: string) => { | ||
| if (!(lang in components.languages)) { | ||
| if (!loadLanguages.silent) { | ||
| console.warn('Language does not exist: ' + lang); | ||
|
ematipico marked this conversation as resolved.
|
||
| } | ||
| return; | ||
| } | ||
|
|
||
| // remove from Prism | ||
| delete Prism.languages[lang]; | ||
|
|
||
| if (Object.hasOwn(bundledLanguages, lang)) { | ||
| await bundledLanguages[lang](); | ||
| } | ||
|
|
||
| loadedLanguages.add(lang); | ||
| }, loadChainer); | ||
|
|
||
| cleanUp(); | ||
| } | ||
|
|
||
| /** | ||
| * Set this to `true` to prevent all warning messages `loadLanguages` logs. | ||
| */ | ||
| loadLanguages.silent = false; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| declare module 'virtual:astro-cloudflare:prism' { | ||
| export const bundledLanguages: Record<string, () => Promise<void>>; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { fileURLToPath } from 'node:url'; | ||
| import type { Plugin } from 'vite'; | ||
| import components from 'prismjs/components.js'; | ||
|
|
||
| const MODULE_ID = 'virtual:astro-cloudflare:prism'; | ||
| const RESOLVED_MODULE_ID = '\0' + MODULE_ID; | ||
|
|
||
| const languages = Object.keys(components.languages).filter((l) => l !== 'meta'); | ||
|
|
||
| export default function cfPrismPlugin(): Plugin { | ||
| return { | ||
| name: '@astrojs/cloudflare:prism', | ||
| configEnvironment(environmentName) { | ||
| if (environmentName === 'ssr') { | ||
| return { | ||
| // Because this virtual module adds a large number of dynamic import statements, | ||
| // Vite’s logs will consequently display the message “new dependencies optimized” for all languages. | ||
| // To avoid this, we explicitly specify that the module should be optimized in advance. | ||
| optimizeDeps: { | ||
| include: ['prismjs/components/prism-*.js'], | ||
| }, | ||
| }; | ||
| } | ||
| }, | ||
| resolveId: { | ||
| filter: { | ||
| id: new RegExp(`^${MODULE_ID}$`), | ||
| }, | ||
| handler() { | ||
| return RESOLVED_MODULE_ID; | ||
| }, | ||
| }, | ||
| load: { | ||
| filter: { | ||
| id: new RegExp(`^${RESOLVED_MODULE_ID}$`), | ||
| }, | ||
| async handler() { | ||
| const importerPath = fileURLToPath(import.meta.url); | ||
|
|
||
| const resolvedModules = await Promise.all( | ||
| languages.map(async (lang) => { | ||
| const resolvedId = await this.resolve( | ||
| `prismjs/components/prism-${lang}.js`, | ||
| importerPath, | ||
| ); | ||
|
|
||
| return { resolvedId: resolvedId?.id, lang }; | ||
| }), | ||
| ); | ||
| const prismBundledLanguages = resolvedModules | ||
| .filter(({ resolvedId }) => resolvedId !== undefined) | ||
| .map( | ||
| ({ resolvedId, lang }) => | ||
| `${JSON.stringify(lang)}: () => import(${JSON.stringify(resolvedId)})`, | ||
| ); | ||
|
|
||
| return ` | ||
| export const bundledLanguages = { ${prismBundledLanguages.join(',')} }; | ||
| `; | ||
| }, | ||
| }, | ||
| }; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how does this work? What causes this to be loaded?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is due to this feature.
In PR #15565 as well, a similar approach was taken to address an issue that only occurs in the workerd environment.
https://developers.cloudflare.com/workers/wrangler/bundling/#conditional-exports