Skip to content

Commit ceea80d

Browse files
authored
Add source maps to node loader
Closes GH-2458. Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 6c0f62d commit ceea80d

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

packages/node-loader/lib/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import fs from 'node:fs/promises'
2222
import {createFormatAwareProcessors} from '@mdx-js/mdx/internal-create-format-aware-processors'
2323
import {extnamesToRegex} from '@mdx-js/mdx/internal-extnames-to-regex'
24+
import {SourceMapGenerator} from 'source-map'
2425
import {VFile} from 'vfile'
2526
import {development as defaultDevelopment} from '#condition'
2627

@@ -36,7 +37,8 @@ export function createLoader(options) {
3637
const options_ = options || {}
3738
const {extnames, process} = createFormatAwareProcessors({
3839
development: defaultDevelopment,
39-
...options_
40+
...options_,
41+
SourceMapGenerator
4042
})
4143
const regex = extnamesToRegex(extnames)
4244

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

65-
return {format: 'module', shortCircuit: true, source: String(file)}
67+
return {
68+
format: 'module',
69+
shortCircuit: true,
70+
source:
71+
String(file) +
72+
'\n//# sourceMappingURL=data:application/json;base64,' +
73+
Buffer.from(JSON.stringify(file.map)).toString('base64') +
74+
'\n'
75+
}
6676
}
6777

6878
return nextLoad(href, context)

packages/node-loader/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@
4444
],
4545
"dependencies": {
4646
"@mdx-js/mdx": "^3.0.0",
47+
"source-map": "^0.7.0",
4748
"vfile": "^6.0.0"
4849
},
4950
"devDependencies": {},
5051
"scripts": {
5152
"test": "npm run test-coverage",
52-
"test-api": "node --conditions development --loader=@mdx-js/node-loader test/index.js",
53+
"test-api": "node --conditions development --enable-source-maps --loader=@mdx-js/node-loader test/index.js",
5354
"test-coverage": "c8 --100 --reporter lcov npm run test-api"
5455
},
5556
"xo": {

packages/node-loader/test/index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,48 @@ test('@mdx-js/node-loader', async function (t) {
5252

5353
await fs.rm(mdxUrl)
5454
})
55+
56+
await t.test('supports source maps work', async function () {
57+
const mdxUrl = new URL('crash.mdx', import.meta.url)
58+
59+
await fs.writeFile(
60+
mdxUrl,
61+
'<Throw />\nexport function Throw() { throw new Error("Boom") }\n'
62+
)
63+
64+
/** @type {MDXModule} */
65+
let result
66+
67+
try {
68+
result = await import(mdxUrl.href)
69+
} catch (error) {
70+
const exception = /** @type {NodeJS.ErrnoException} */ (error)
71+
72+
if (exception.code === 'ERR_UNKNOWN_FILE_EXTENSION') {
73+
await fs.rm(mdxUrl)
74+
75+
throw new Error(
76+
'Please run Node with `--loader=@mdx-js/node-loader` to test the ESM loader'
77+
)
78+
}
79+
80+
throw error
81+
}
82+
83+
const Content = result.default
84+
85+
assert.throws(
86+
() => renderToStaticMarkup(React.createElement(Content)),
87+
(error) => {
88+
assert(error instanceof Error)
89+
assert.equal(error.message, 'Boom')
90+
// Source maps are off.
91+
// The column should be 26, not 8.
92+
assert(error.stack?.includes('crash.mdx:2:8)'))
93+
return true
94+
}
95+
)
96+
97+
await fs.rm(mdxUrl)
98+
})
5599
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy