@@ -11,6 +11,7 @@ import {
11
11
import { transformRequest } from '../server/transformRequest'
12
12
import type { InternalResolveOptionsWithOverrideConditions } from '../plugins/resolve'
13
13
import { tryNodeResolve } from '../plugins/resolve'
14
+ import { genSourceMapUrl } from '../server/sourcemap'
14
15
import {
15
16
ssrDynamicImportKey ,
16
17
ssrExportAllKey ,
@@ -26,6 +27,16 @@ interface SSRContext {
26
27
27
28
type SSRModule = Record < string , any >
28
29
30
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
31
+ const AsyncFunction = async function ( ) { } . constructor as typeof Function
32
+ let fnDeclarationLineCount = 0
33
+ {
34
+ const body = '/*code*/'
35
+ const source = new AsyncFunction ( 'a' , 'b' , body ) . toString ( )
36
+ fnDeclarationLineCount =
37
+ source . slice ( 0 , source . indexOf ( body ) ) . split ( '\n' ) . length - 1
38
+ }
39
+
29
40
const pendingModules = new Map < string , Promise < SSRModule > > ( )
30
41
const pendingImports = new Map < string , string [ ] > ( )
31
42
const importErrors = new WeakMap < Error , { importee : string } > ( )
@@ -190,17 +201,28 @@ async function instantiateModule(
190
201
}
191
202
}
192
203
204
+ let sourceMapSuffix = ''
205
+ if ( result . map ) {
206
+ const moduleSourceMap = Object . assign ( { } , result . map , {
207
+ // currently we need to offset the line
208
+ // https://github.com/nodejs/node/issues/43047#issuecomment-1180632750
209
+ mappings : ';' . repeat ( fnDeclarationLineCount ) + result . map . mappings ,
210
+ } )
211
+ sourceMapSuffix =
212
+ '\n//# sourceMappingURL=' + genSourceMapUrl ( moduleSourceMap )
213
+ }
214
+
193
215
try {
194
- // eslint-disable-next-line @typescript-eslint/no-empty-function
195
- const AsyncFunction = async function ( ) { } . constructor as typeof Function
196
216
const initModule = new AsyncFunction (
197
217
`global` ,
198
218
ssrModuleExportsKey ,
199
219
ssrImportMetaKey ,
200
220
ssrImportKey ,
201
221
ssrDynamicImportKey ,
202
222
ssrExportAllKey ,
203
- '"use strict";' + result . code + `\n//# sourceURL=${ mod . url } ` ,
223
+ '"use strict";' +
224
+ result . code +
225
+ `\n//# sourceURL=${ mod . url } ${ sourceMapSuffix } ` ,
204
226
)
205
227
await initModule (
206
228
context . global ,
0 commit comments