@@ -4,9 +4,7 @@ import { DecodedMap } from './sourcemap/decoder'
44import type { ResolvedResult } from './types'
55import { decodeBase64 } from './utils'
66
7- const MODULE_RUNNER_SOURCEMAPPING_REGEXP = new RegExp (
8- `//# ${ SOURCEMAPPING_URL } =data:application/json;base64,(.+)` ,
9- )
7+ const MODULE_RUNNER_SOURCEMAPPING_PREFIX = `//# ${ SOURCEMAPPING_URL } =data:application/json;base64,`
108
119export class EvaluatedModuleNode {
1210 public importers : Set < string > = new Set ( )
@@ -110,13 +108,7 @@ export class EvaluatedModules {
110108 if ( mod . map ) return mod . map
111109 if ( ! mod . meta || ! ( 'code' in mod . meta ) ) return null
112110
113- const pattern = `//# ${ SOURCEMAPPING_URL } =data:application/json;base64,`
114- const lastIndex = mod . meta . code . lastIndexOf ( pattern )
115- if ( lastIndex === - 1 ) return null
116-
117- const mapString = MODULE_RUNNER_SOURCEMAPPING_REGEXP . exec (
118- mod . meta . code . slice ( lastIndex ) ,
119- ) ?. [ 1 ]
111+ const mapString = extractInlineSourceMap ( mod . meta . code )
120112 if ( ! mapString ) return null
121113 mod . map = new DecodedMap ( JSON . parse ( decodeBase64 ( mapString ) ) , mod . file )
122114 return mod . map
@@ -129,6 +121,18 @@ export class EvaluatedModules {
129121 }
130122}
131123
124+ // Plain string search rather than a regexp: it's about twice as fast, which
125+ // adds up on modules with large inline maps
126+ function extractInlineSourceMap ( code : string ) : string | undefined {
127+ const start = code . lastIndexOf ( MODULE_RUNNER_SOURCEMAPPING_PREFIX )
128+ if ( start === - 1 ) return
129+ const dataStart = start + MODULE_RUNNER_SOURCEMAPPING_PREFIX . length
130+ let dataEnd = code . indexOf ( '\n' , dataStart )
131+ if ( dataEnd === - 1 ) dataEnd = code . length
132+ if ( code . charCodeAt ( dataEnd - 1 ) === 13 /* \r */ ) dataEnd --
133+ return code . slice ( dataStart , dataEnd )
134+ }
135+
132136// unique id that is not available as "$bare_import" like "test"
133137// https://nodejs.org/api/modules.html#built-in-modules-with-mandatory-node-prefix
134138const prefixedBuiltins = new Set ( [
0 commit comments