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 node loader #2458

Merged
merged 4 commits into from Apr 4, 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
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)
})
})