@@ -20,15 +20,20 @@ const { isAbsolute, join } = require("../fs/fs");
2020 * @typedef {object } SourceMappingURL
2121 * @property {string } sourceMappingURL
2222 * @property {string } replacementString
23+ * @property {number } replacementStart
2324 */
2425
2526// Matches only the last occurrence of sourceMappingURL
2627const innerRegex = / \s * [ # @ ] \s * s o u r c e M a p p i n g U R L \s * = \s * ( [ ^ \s ' " ] * ) \s * / ;
2728
2829const validProtocolPattern = / ^ [ a - z ] [ a - z 0 - 9 + . - ] * : / i;
30+ const whitespaceRegex = / \s / ;
31+ const lineBreakRegex = / [ \r \n \u2028 \u2029 ] / ;
32+ const ambiguousCommentRegex = / [ ' " ` ] | \* \/ / ;
33+ const ambiguousBlockCommentRegex = / [ ' " ` \] ] | \* \/ (? ! $ ) / ;
2934
3035const sourceMappingURLRegex = new RegExp (
31- "(?:" +
36+ "^ (?:" +
3237 "/\\*" +
3338 "(?:\\s*\r?\n(?://)?)?" +
3439 `(?:${ innerRegex . source } )` +
@@ -37,7 +42,7 @@ const sourceMappingURLRegex = new RegExp(
3742 "|" +
3843 `//(?:${ innerRegex . source } )` +
3944 ")" +
40- "\\s*"
45+ "\\s*$ "
4146) ;
4247
4348/**
@@ -46,24 +51,67 @@ const sourceMappingURLRegex = new RegExp(
4651 * @returns {SourceMappingURL } source mapping information
4752 */
4853function getSourceMappingURL ( code ) {
49- const lines = code . split ( / ^ / m) ;
50- /** @type {RegExpMatchArray | null | undefined } */
51- let match ;
52-
53- for ( let i = lines . length - 1 ; i >= 0 ; i -- ) {
54- match = lines [ i ] . match ( sourceMappingURLRegex ) ;
55- if ( match ) {
54+ let end = code . length ;
55+ let lineStart = end ;
56+ // ECMA-426's non-parsing extraction stops at code or ambiguous comments.
57+ // Block comments also support CSS and existing JavaScript annotations.
58+ while ( end > 0 ) {
59+ while ( end > 0 && whitespaceRegex . test ( code [ end - 1 ] ) ) end -- ;
60+ if ( end === 0 ) break ;
61+ // Consecutive block comments can share a long line of generated code.
62+ if ( lineStart >= end ) {
63+ lineStart = end ;
64+ while ( lineStart > 0 && ! lineBreakRegex . test ( code [ lineStart - 1 ] ) ) {
65+ lineStart -- ;
66+ }
67+ while ( lineStart < end && whitespaceRegex . test ( code [ lineStart ] ) ) {
68+ lineStart ++ ;
69+ }
70+ }
71+ let start = lineStart ;
72+ let comment ;
73+ if ( code . startsWith ( "//" , start ) ) {
74+ comment = code . slice ( start , end ) ;
75+ if ( ambiguousCommentRegex . test ( comment ) ) break ;
76+ } else if ( code . endsWith ( "*/" , end ) ) {
77+ start = code . lastIndexOf ( "/*" , end - 2 ) ;
78+ if ( start < 0 ) break ;
79+ comment = code . slice ( start , end ) ;
80+ // An escaped opener or a character class can belong to a trailing RegExp.
81+ if (
82+ code [ start - 1 ] === "\\" ||
83+ ambiguousBlockCommentRegex . test ( comment )
84+ ) {
85+ break ;
86+ }
87+ // Comments cannot nest; an inner opener may only be comment text.
88+ if (
89+ start > 0 &&
90+ code . lastIndexOf ( "/*" , start - 1 ) > code . lastIndexOf ( "*/" , start - 1 )
91+ ) {
92+ break ;
93+ }
94+ } else {
5695 break ;
5796 }
97+ const match = sourceMappingURLRegex . exec ( comment ) ;
98+ if ( match ) {
99+ const sourceMappingURL = match [ 1 ] || match [ 2 ] || "" ;
100+ return {
101+ sourceMappingURL : sourceMappingURL
102+ ? decodeURI ( sourceMappingURL )
103+ : sourceMappingURL ,
104+ replacementString : comment ,
105+ replacementStart : start
106+ } ;
107+ }
108+ end = start ;
58109 }
59110
60- const sourceMappingURL = match ? match [ 1 ] || match [ 2 ] || "" : "" ;
61-
62111 return {
63- sourceMappingURL : sourceMappingURL
64- ? decodeURI ( sourceMappingURL )
65- : sourceMappingURL ,
66- replacementString : match ? match [ 0 ] : ""
112+ sourceMappingURL : "" ,
113+ replacementString : "" ,
114+ replacementStart : 0
67115 } ;
68116}
69117
@@ -229,7 +277,8 @@ async function extractSourceMap(stringOrBuffer, resourcePath, readResource) {
229277 source : stringOrBuffer ,
230278 sourceMap : inputSourceMap
231279 } ;
232- const { sourceMappingURL, replacementString } = getSourceMappingURL ( input ) ;
280+ const { sourceMappingURL, replacementString, replacementStart } =
281+ getSourceMappingURL ( input ) ;
233282
234283 if ( ! sourceMappingURL ) {
235284 return output ;
@@ -310,7 +359,9 @@ async function extractSourceMap(stringOrBuffer, resourcePath, readResource) {
310359 }
311360
312361 return {
313- source : input . replace ( replacementString , "" ) ,
362+ source :
363+ input . slice ( 0 , replacementStart ) +
364+ input . slice ( replacementStart + replacementString . length ) ,
314365 sourceMap : /** @type {RawSourceMap } */ ( newMap )
315366 } ;
316367}
0 commit comments