Skip to content

Commit

Permalink
Always use source maps in @mdx-js/node-loader
Browse files Browse the repository at this point in the history
There’s not really a reason not to.
  • Loading branch information
remcohaszing committed Apr 4, 2024
1 parent f648f2d commit 85c53fb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/node-loader/lib/index.js
Original file line number Diff line number Diff line change
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 Down
3 changes: 2 additions & 1 deletion packages/node-loader/package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 85c53fb

Please sign in to comment.