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

Add source maps to esbuild plugin #2464

Merged
merged 1 commit into from Apr 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions packages/esbuild/lib/index.js
Expand Up @@ -43,6 +43,7 @@ import fs from 'node:fs/promises'
import path from 'node:path'
import {createFormatAwareProcessors} from '@mdx-js/mdx/internal-create-format-aware-processors'
import {extnamesToRegex} from '@mdx-js/mdx/internal-extnames-to-regex'
import {SourceMapGenerator} from 'source-map'
import {VFile} from 'vfile'
import {VFileMessage} from 'vfile-message'

Expand All @@ -65,7 +66,10 @@ const name = '@mdx-js/esbuild'
* Plugin.
*/
export function esbuild(options) {
const {extnames, process} = createFormatAwareProcessors(options || {})
const {extnames, process} = createFormatAwareProcessors({
...options,
SourceMapGenerator
})

return {name, setup}

Expand Down Expand Up @@ -96,7 +100,7 @@ export function esbuild(options) {
/** @type {State} */
const state = {doc: document, name, path: data.path}
let file = new VFile({path: data.path, value: document})
/** @type {Value | undefined} */
/** @type {string | undefined} */
let value
/** @type {Array<VFileMessage>} */
let messages = []
Expand All @@ -107,7 +111,11 @@ export function esbuild(options) {

try {
file = await process(file)
value = file.value
value =
String(file.value) +
'\n//# sourceMappingURL=data:application/json;base64,' +
Buffer.from(JSON.stringify(file.map)).toString('base64') +
'\n'
messages = file.messages
} catch (error_) {
const cause = /** @type {VFileMessage | Error} */ (error_)
Expand Down
3 changes: 2 additions & 1 deletion packages/esbuild/package.json
Expand Up @@ -40,6 +40,7 @@
"dependencies": {
"@mdx-js/mdx": "^3.0.0",
"@types/unist": "^3.0.0",
"source-map": "^0.7.0",
"vfile": "^6.0.0",
"vfile-message": "^4.0.0"
},
Expand All @@ -48,7 +49,7 @@
},
"scripts": {
"test": "npm run test-coverage",
"test-api": "node --conditions development test/index.js",
"test-api": "node --conditions development --enable-source-maps test/index.js",
"test-coverage": "c8 --100 --reporter lcov npm run test-api"
},
"xo": {
Expand Down
38 changes: 38 additions & 0 deletions packages/esbuild/test/index.js
Expand Up @@ -539,6 +539,44 @@ test('@mdx-js/esbuild', async function (t) {
await fs.rm(jsUrl)
}
)

await t.test('should support source maps', async () => {
const mdxUrl = new URL('crash.mdx', import.meta.url)
const jsUrl = new URL('crash.js', import.meta.url)
await fs.writeFile(
mdxUrl,
'<Throw />\nexport function Throw() { throw new Error("Boom") }\n'
)

await esbuild.build({
entryPoints: [fileURLToPath(mdxUrl)],
outfile: fileURLToPath(jsUrl),
plugins: [esbuildMdx()],
define: {'process.env.NODE_ENV': '"development"'},
format: 'esm',
sourcemap: true,
bundle: true
})

/** @type {MDXModule} */
const result = await import(jsUrl.href)
const Content = result.default

assert.throws(
() => renderToStaticMarkup(React.createElement(Content)),
(error) => {
assert(error instanceof Error)
assert.equal(error.message, 'Boom')
// Source maps are off.
// The column should be 26, not 8.
assert(error.stack?.includes('crash.mdx:2:8)'))
return true
}
)

await fs.rm(mdxUrl)
await fs.rm(jsUrl)
})
})

/**
Expand Down