@@ -7,6 +7,8 @@ import path from 'path';
7
7
8
8
import validateOptions from 'schema-utils' ;
9
9
import async from 'neo-async' ;
10
+ import parseDataURL from 'data-urls' ;
11
+ import { labelToName , decode } from 'whatwg-encoding' ;
10
12
import { getOptions , urlToRequest } from 'loader-utils' ;
11
13
12
14
import schema from './options.json' ;
@@ -18,8 +20,6 @@ const baseRegex =
18
20
const regex1 = new RegExp ( `/\\*${ baseRegex } \\s*\\*/` ) ;
19
21
// Matches // .... comments
20
22
const regex2 = new RegExp ( `//${ baseRegex } ($|\n|\r\n?)` ) ;
21
- // Matches DataUrls
22
- const regexDataUrl = / d a t a : [ ^ ; \n ] + (?: ; c h a r s e t = [ ^ ; \n ] + ) ? ; b a s e 6 4 , ( [ a - z A - Z 0 - 9 + / ] + = { 0 , 2 } ) / ;
23
23
24
24
export default function loader ( input , inputMap ) {
25
25
const options = getOptions ( this ) ;
@@ -39,20 +39,23 @@ export default function loader(input, inputMap) {
39
39
}
40
40
41
41
const [ , url ] = match ;
42
- const dataUrlMatch = regexDataUrl . exec ( url ) ;
43
- const { context, resolve, addDependency, emitWarning } = this ;
44
42
45
- if ( dataUrlMatch ) {
46
- const [ , mapBase64 ] = dataUrlMatch ;
47
- const mapStr = new Buffer ( mapBase64 , 'base64' ) . toString ( ) ;
43
+ const dataURL = parseDataURL ( url ) ;
44
+
45
+ const { context , resolve , addDependency , emitWarning } = this ;
48
46
47
+ if ( dataURL ) {
49
48
let map ;
50
49
51
50
try {
52
- map = JSON . parse ( mapStr ) ;
51
+ dataURL . encodingName =
52
+ labelToName ( dataURL . mimeType . parameters . get ( 'charset' ) ) || 'UTF-8' ;
53
+
54
+ map = decode ( dataURL . body , dataURL . encodingName ) ;
55
+ map = JSON . parse ( map ) ;
53
56
} catch ( error ) {
54
57
emitWarning (
55
- `Cannot parse inline SourceMap ' ${ mapBase64 . substr ( 0 , 50 ) } ' : ${ error } `
58
+ `Cannot parse inline SourceMap with Charset ${ dataURL . encodingName } : ${ error } `
56
59
) ;
57
60
58
61
callback ( null , input , inputMap ) ;
@@ -65,6 +68,14 @@ export default function loader(input, inputMap) {
65
68
return ;
66
69
}
67
70
71
+ if ( url . toLowerCase ( ) . indexOf ( 'data:' ) === 0 ) {
72
+ emitWarning ( `Cannot parse inline SourceMap: ${ url } ` ) ;
73
+
74
+ callback ( null , input , inputMap ) ;
75
+
76
+ return ;
77
+ }
78
+
68
79
resolve ( context , urlToRequest ( url , true ) , ( resolveError , result ) => {
69
80
if ( resolveError ) {
70
81
emitWarning ( `Cannot find SourceMap '${ url } ': ${ resolveError } ` ) ;
0 commit comments