@@ -11,6 +11,8 @@ import parseDataURL from 'data-urls';
11
11
import { labelToName , decode } from 'whatwg-encoding' ;
12
12
import { getOptions , urlToRequest } from 'loader-utils' ;
13
13
14
+ import flattenSourceMap from './utils/flatten' ;
15
+
14
16
import schema from './options.json' ;
15
17
16
18
// Matches only the last occurrence of sourceMappingURL
@@ -113,77 +115,76 @@ export default function loader(input, inputMap) {
113
115
} ) ;
114
116
115
117
// eslint-disable-next-line no-shadow
116
- function processMap ( map , context , callback ) {
117
- if ( ! map . sourcesContent || map . sourcesContent . length < map . sources . length ) {
118
- const sourcePrefix = map . sourceRoot ? `${ map . sourceRoot } /` : '' ;
119
-
118
+ async function processMap ( map , context , callback ) {
119
+ if ( map . sections ) {
120
120
// eslint-disable-next-line no-param-reassign
121
- map . sources = map . sources . map ( ( s ) => sourcePrefix + s ) ;
121
+ map = await flattenSourceMap ( map ) ;
122
+ }
122
123
123
- // eslint-disable-next-line no-param-reassign
124
- delete map . sourceRoot ;
124
+ if ( map . sourcesContent && map . sourcesContent . length >= map . sources . length ) {
125
+ callback ( null , input . replace ( match [ 0 ] , '' ) , map ) ;
125
126
126
- const missingSources = map . sourcesContent
127
- ? map . sources . slice ( map . sourcesContent . length )
128
- : map . sources ;
127
+ return ;
128
+ }
129
129
130
- async . map (
131
- missingSources ,
132
- // eslint-disable-next-line no-shadow
133
- ( source , callback ) => {
134
- resolve (
135
- context ,
136
- urlToRequest ( source , true ) ,
137
- ( resolveError , result ) => {
138
- if ( resolveError ) {
139
- emitWarning (
140
- `Cannot find source file '${ source } ': ${ resolveError } `
141
- ) ;
130
+ const sourcePrefix = map . sourceRoot ? `${ map . sourceRoot } ${ path . sep } ` : '' ;
142
131
143
- callback ( null , null ) ;
132
+ // eslint-disable-next-line no-param-reassign
133
+ map . sources = map . sources . map ( ( s ) => sourcePrefix + s ) ;
144
134
145
- return ;
146
- }
135
+ // eslint-disable-next-line no-param-reassign
136
+ delete map . sourceRoot ;
147
137
148
- addDependency ( result ) ;
138
+ const missingSources = map . sourcesContent
139
+ ? map . sources . slice ( map . sourcesContent . length )
140
+ : map . sources ;
149
141
150
- fs . readFile ( result , 'utf-8' , ( readFileError , content ) => {
151
- if ( readFileError ) {
152
- emitWarning (
153
- `Cannot open source file '${ result } ': ${ readFileError } `
154
- ) ;
142
+ async . map (
143
+ missingSources ,
144
+ // eslint-disable-next-line no-shadow
145
+ ( source , callback ) => {
146
+ resolve ( context , urlToRequest ( source , true ) , ( resolveError , result ) => {
147
+ if ( resolveError ) {
148
+ emitWarning ( `Cannot find source file '${ source } ': ${ resolveError } ` ) ;
155
149
156
- callback ( null , null ) ;
150
+ callback ( null , null ) ;
157
151
158
- return ;
159
- }
152
+ return ;
153
+ }
160
154
161
- callback ( null , { source : result , content } ) ;
162
- } ) ;
163
- }
164
- ) ;
165
- } ,
166
- ( err , info ) => {
167
- // eslint-disable-next-line no-param-reassign
168
- map . sourcesContent = map . sourcesContent || [ ] ;
169
-
170
- info . forEach ( ( res ) => {
171
- if ( res ) {
172
- // eslint-disable-next-line no-param-reassign
173
- map . sources [ map . sourcesContent . length ] = res . source ;
174
- map . sourcesContent . push ( res . content ) ;
175
- } else {
176
- map . sourcesContent . push ( null ) ;
177
- }
178
- } ) ;
155
+ addDependency ( result ) ;
179
156
180
- processMap ( map , context , callback ) ;
181
- }
182
- ) ;
157
+ fs . readFile ( result , 'utf-8' , ( readFileError , content ) => {
158
+ if ( readFileError ) {
159
+ emitWarning (
160
+ `Cannot open source file '${ result } ': ${ readFileError } `
161
+ ) ;
183
162
184
- return ;
185
- }
163
+ callback ( null , null ) ;
186
164
187
- callback ( null , input . replace ( match [ 0 ] , '' ) , map ) ;
165
+ return ;
166
+ }
167
+
168
+ callback ( null , { source : result , content } ) ;
169
+ } ) ;
170
+ } ) ;
171
+ } ,
172
+ ( err , info ) => {
173
+ // eslint-disable-next-line no-param-reassign
174
+ map . sourcesContent = map . sourcesContent || [ ] ;
175
+
176
+ info . forEach ( ( res ) => {
177
+ if ( res ) {
178
+ // eslint-disable-next-line no-param-reassign
179
+ map . sources [ map . sourcesContent . length ] = res . source ;
180
+ map . sourcesContent . push ( res . content ) ;
181
+ } else {
182
+ map . sourcesContent . push ( null ) ;
183
+ }
184
+ } ) ;
185
+
186
+ processMap ( map , context , callback ) ;
187
+ }
188
+ ) ;
188
189
}
189
190
}
0 commit comments