Skip to content

Commit

Permalink
Add source maps to node loader
Browse files Browse the repository at this point in the history
Closes GH-2458.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
remcohaszing committed Apr 4, 2024
1 parent 6c0f62d commit ceea80d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
14 changes: 12 additions & 2 deletions packages/node-loader/lib/index.js
Expand Up @@ -21,6 +21,7 @@
import fs from 'node:fs/promises'
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 {development as defaultDevelopment} from '#condition'

Expand All @@ -36,7 +37,8 @@ export function createLoader(options) {
const options_ = options || {}
const {extnames, process} = createFormatAwareProcessors({
development: defaultDevelopment,
...options_
...options_,
SourceMapGenerator
})
const regex = extnamesToRegex(extnames)

Expand All @@ -62,7 +64,15 @@ export function createLoader(options) {
const value = await fs.readFile(url)
const file = await process(new VFile({value, path: url}))

return {format: 'module', shortCircuit: true, source: String(file)}
return {
format: 'module',
shortCircuit: true,
source:
String(file) +
'\n//# sourceMappingURL=data:application/json;base64,' +
Buffer.from(JSON.stringify(file.map)).toString('base64') +
'\n'
}
}

return nextLoad(href, context)
Expand Down
3 changes: 2 additions & 1 deletion packages/node-loader/package.json
Expand Up @@ -44,12 +44,13 @@
],
"dependencies": {
"@mdx-js/mdx": "^3.0.0",
"source-map": "^0.7.0",
"vfile": "^6.0.0"
},
"devDependencies": {},
"scripts": {
"test": "npm run test-coverage",
"test-api": "node --conditions development --loader=@mdx-js/node-loader test/index.js",
"test-api": "node --conditions development --enable-source-maps --loader=@mdx-js/node-loader test/index.js",
"test-coverage": "c8 --100 --reporter lcov npm run test-api"
},
"xo": {
Expand Down
44 changes: 44 additions & 0 deletions packages/node-loader/test/index.js
Expand Up @@ -52,4 +52,48 @@ test('@mdx-js/node-loader', async function (t) {

await fs.rm(mdxUrl)
})

await t.test('supports source maps work', async function () {
const mdxUrl = new URL('crash.mdx', import.meta.url)

await fs.writeFile(
mdxUrl,
'<Throw />\nexport function Throw() { throw new Error("Boom") }\n'
)

/** @type {MDXModule} */
let result

try {
result = await import(mdxUrl.href)
} catch (error) {
const exception = /** @type {NodeJS.ErrnoException} */ (error)

if (exception.code === 'ERR_UNKNOWN_FILE_EXTENSION') {
await fs.rm(mdxUrl)

throw new Error(
'Please run Node with `--loader=@mdx-js/node-loader` to test the ESM loader'
)
}

throw error
}

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)
})
})

0 comments on commit ceea80d

Please sign in to comment.