Skip to content
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

fix(plugin-playground): manually load babel.min.js to avoid conflict with monaco #898

Merged
merged 4 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thin-ads-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rspress/plugin-playground": patch
---

fix(plugin-playground): manually load babel.min.js to avoid conflict with monaco
30 changes: 10 additions & 20 deletions packages/plugin-playground/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function pluginPlayground(
include,
defaultDirection = 'horizontal',
editorPosition = 'left',
babelUrl = '',
babelUrl = DEFAULT_BABEL_URL,
monacoLoader = {},
monacoOptions = {},
defaultRenderMode = 'playground',
Expand Down Expand Up @@ -208,30 +208,20 @@ export function pluginPlayground(
__PLAYGROUND_DIRECTION__: JSON.stringify(defaultDirection),
__PLAYGROUND_MONACO_LOADER__: JSON.stringify(monacoLoader),
__PLAYGROUND_MONACO_OPTIONS__: JSON.stringify(monacoOptions),
__PLAYGROUND_BABEL_URL__: JSON.stringify(babelUrl),
},
include: [join(__dirname, '..', '..', '..')],
},
html: {
tags: [
...preloads.map(url => ({
tag: 'link',
head: true,
attrs: {
rel: 'preload',
href: url,
as: 'script',
},
})),
{
tag: 'script',
head: true,
attrs: {
id: 'rspress-playground-babel',
async: true,
src: babelUrl || DEFAULT_BABEL_URL,
},
tags: preloads.map(url => ({
tag: 'link',
head: true,
attrs: {
rel: 'preload',
href: url,
as: 'script',
},
],
})),
},
tools: {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/prefer-ts-expect-error
Expand Down
47 changes: 38 additions & 9 deletions packages/plugin-playground/src/web/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,50 @@ declare global {
interface Window {
Babel: Babel;
}
const __PLAYGROUND_BABEL_URL__: string;
}

function getBabel(): Babel | Promise<Babel> {
// see https://github.com/web-infra-dev/rspress/issues/876
async function loadUmdBabelModule(): Promise<Babel> {
const data = await fetch(__PLAYGROUND_BABEL_URL__);

const umdSourceCode = await data.text();

const run = new Function(
'exports',
'module',
'require',
`with(exports, module, require) {${umdSourceCode}}`,
);

const exports: Babel = {} as unknown as Babel;
const module = { exports };
const require = () => {};

run(exports, module, require);

return exports;
}

let loadBabelPromise: null | Promise<Babel> = null;

async function getBabel(): Promise<Babel> {
if (window.Babel) {
return window.Babel;
}
const el = document.getElementById('rspress-playground-babel');
if (!el) {
throw new Error('Babel not found');
if (loadBabelPromise) {
return loadBabelPromise;
}

loadBabelPromise = loadUmdBabelModule();
try {
const Babel = await loadBabelPromise;
window.Babel = Babel;
return Babel;
} catch (e) {
loadBabelPromise = null;
throw e;
}
return new Promise(resolve => {
el.addEventListener('load', () => {
resolve(window.Babel);
});
});
}

export { getBabel };
Loading