@@ -87,6 +87,11 @@ function write(file, destPath, options, cb) {
8787 return cb ( new Error ( PLUGIN_NAME + '-write: Not a vinyl file' ) ) ;
8888 }
8989
90+ // Throw an error if the file doesn't have a sourcemap
91+ if ( ! file . sourceMap ) {
92+ return cb ( new Error ( PLUGIN_NAME + '-write: No sourcemap found' ) ) ;
93+ }
94+
9095 // return array with file & optionally sourcemap file
9196 var arr = [ ] ;
9297
@@ -103,14 +108,11 @@ function write(file, destPath, options, cb) {
103108
104109 var sourceMap = file . sourceMap ;
105110
106- // Throw an error if the file doesn't have a sourcemap
107- if ( ! file . sourceMap ) {
108- return cb ( new Error ( PLUGIN_NAME + '-write: No sourcemap found' ) ) ;
109- }
110-
111111 // fix paths if Windows style paths
112112 sourceMap . file = helpers . unixStylePath ( file . relative ) ;
113113
114+ // TODO: Need a way to handle resolve this before passing in
115+ // This module shouldn't be taking function options because they are normalized higher
114116 if ( options . mapSources && typeof options . mapSources === 'function' ) {
115117 sourceMap . sources = sourceMap . sources . map ( function ( filePath ) {
116118 return options . mapSources ( filePath ) ;
@@ -121,58 +123,46 @@ function write(file, destPath, options, cb) {
121123 return helpers . unixStylePath ( filePath ) ;
122124 } ) ;
123125
126+ // TODO: Remove function support for this option
124127 if ( typeof options . sourceRoot === 'function' ) {
125128 sourceMap . sourceRoot = options . sourceRoot ( file ) ;
126129 } else {
127130 sourceMap . sourceRoot = options . sourceRoot ;
128131 }
132+
133+ // TODO: support null-ish with ==
129134 if ( sourceMap . sourceRoot === null ) {
130135 sourceMap . sourceRoot = undefined ;
131136 }
132137
133- var includeContent = function ( callback ) {
138+ function includeContent ( callback ) {
134139 sourceMap . sourcesContent = sourceMap . sourcesContent || [ ] ;
135140
136- var loadCounter = 0 ;
137- var loadSourceAsync = function ( source , onLoaded ) {
138- var i = source [ 0 ] ,
139- sourcePath = source [ 1 ] ;
140- fs . readFile ( sourcePath , 'utf8' , function ( err , data ) {
141- if ( err ) {
142- if ( options . debug ) {
143- console . warn ( PLUGIN_NAME + '-write: source file not found: ' + sourcePath ) ;
144- }
145- return onLoaded ( ) ;
146- }
147- sourceMap . sourcesContent [ i ] = stripBom ( data ) ;
148- onLoaded ( ) ;
149- } ) ;
150- } ;
141+ function loadSources ( sourcePath , idx , cb ) {
142+ if ( sourceMap . sourcesContent [ idx ] ) {
143+ return cb ( ) ;
144+ }
145+
146+ var absPath = path . resolve ( sourceMap . sourceRoot || file . base , sourcePath ) ;
147+ // if (options.debug) {
148+ // console.log(PLUGIN_NAME + '-write: No source content for "' + sourceMap.sources[i] + '". Loading from file.');
149+ // }
150+ fs . readFile ( absPath , 'utf8' , onRead ) ;
151151
152- var sourcesToLoadAsync = file . sourceMap . sources . reduce ( function ( result , source , i ) {
153- if ( ! sourceMap . sourcesContent [ i ] ) {
154- var sourcePath = path . resolve ( sourceMap . sourceRoot || file . base , sourceMap . sources [ i ] ) ;
155- if ( options . debug ) {
156- console . log ( PLUGIN_NAME + '-write: No source content for "' + sourceMap . sources [ i ] + '". Loading from file.' ) ;
152+ function onRead ( err , data ) {
153+ if ( err ) {
154+ // if (options.debug) {
155+ // console.warn(PLUGIN_NAME + '-write: source file not found: ' + sourcePath);
156+ // }
157+ return cb ( ) ;
157158 }
158- result . push ( [ i , sourcePath ] ) ;
159+ sourceMap . sourcesContent [ idx ] = stripBom ( data ) ;
160+ cb ( ) ;
159161 }
160- return result ;
161- } , [ ] ) ;
162-
163- if ( sourcesToLoadAsync . length ) {
164- // load missing source content
165- sourcesToLoadAsync . forEach ( function ( source ) {
166- loadSourceAsync ( source , function onLoaded ( ) {
167- if ( ++ loadCounter === sourcesToLoadAsync . length ) {
168- callback ( ) ;
169- }
170- } ) ;
171- } ) ;
172- } else {
173- callback ( ) ;
174162 }
175- } ;
163+
164+ async . eachOf ( file . sourceMap . sources , loadSources , callback ) ;
165+ }
176166
177167 var contentIncluded = function ( callback ) {
178168
0 commit comments