File tree Expand file tree Collapse file tree 3 files changed +58
-3
lines changed Expand file tree Collapse file tree 3 files changed +58
-3
lines changed Original file line number Diff line number Diff line change 2121import fs from 'node:fs/promises'
2222import { createFormatAwareProcessors } from '@mdx-js/mdx/internal-create-format-aware-processors'
2323import { extnamesToRegex } from '@mdx-js/mdx/internal-extnames-to-regex'
24+ import { SourceMapGenerator } from 'source-map'
2425import { VFile } from 'vfile'
2526import { 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 )
Original file line number Diff line number Diff line change 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" : {
Original file line number Diff line number Diff 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} )
You can’t perform that action at this time.
0 commit comments