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: remove empty export from transpiled ts file #1471

Merged
merged 2 commits into from Mar 29, 2024

Conversation

John98Zakaria
Copy link
Contributor

@John98Zakaria John98Zakaria commented Mar 26, 2024

When doing an import, the typescript compiler automatically decides "well this file must be a module".

const { transpile } = tsModule ??= await import('typescript')
code = transpile(code, {
module: tsModule.ModuleKind.ESNext,
target: tsModule.ScriptTarget.ES2022,
})
return await context.run(code, 'javascript')

import type {UnionToIntersection} from 'type-fest' -> export {};

However, since the compiled TS code getts embedded in a function, the empty export code doesn't fit into the body of a function.

try {
const safeJS = `return async (console) => {${sanitizeJS(code)}}`
// eslint-disable-next-line no-new-func
await (new Function(safeJS)())(vmConsole)

Instead you get ERROR: SyntaxError: Unexpected token 'export'

See https://stackblitz.com/edit/github-qj6z8s?file=slides.md

This PR sanitizes the JS before running by removing the empty export {}; produced by typescript.

Copy link

netlify bot commented Mar 26, 2024

Deploy Preview for slidev ready!

Name Link
🔨 Latest commit 3abbd8b
🔍 Latest deploy log https://app.netlify.com/sites/slidev/deploys/66028072b26a22000852a49b
😎 Deploy Preview https://deploy-preview-1471--slidev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@John98Zakaria John98Zakaria changed the title Remove empty export from monaco ts-runner Fix: Remove empty export from monaco ts-runner Mar 26, 2024
@John98Zakaria John98Zakaria changed the title Fix: Remove empty export from monaco ts-runner fix: Remove empty export from monaco ts-runner Mar 26, 2024
@KermanX KermanX changed the title fix: Remove empty export from monaco ts-runner fix: remove empty export from transpiled ts file Mar 27, 2024
@antfu antfu merged commit 536c665 into slidevjs:main Mar 29, 2024
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants