-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmap.js
222 lines (174 loc) · 5.89 KB
/
map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { extname, join, resolve } from 'path';
import * as chalk from 'chalk';
import Queue from '../queue/Queue.js';
import { lsr, readFile, symlinkOrCopy, writeFile, Promise } from 'sander';
import assign from '../utils/assign';
import config from '../config/index.js';
import extractLocationInfo from '../utils/extractLocationInfo';
import { isRegExp } from '../utils/is';
import { ABORTED } from '../utils/signals';
import { getSourcemapComment, SOURCEMAP_COMMENT } from '../utils/sourcemap';
export default function map ( inputdir, outputdir, options ) {
let changed = {};
this.changes.forEach( change => {
if ( !change.removed ) {
changed[ change.file ] = true;
}
});
return new Promise( ( fulfil, reject ) => {
const queue = new Queue();
queue.once( 'error', reject );
lsr( inputdir ).then( files => {
const promises = files.map( filename => {
if ( this.aborted ) return;
const ext = extname( filename );
// change extension if necessary, e.g. foo.coffee -> foo.js
const destname = ( options.ext && ~options.accept.indexOf( ext ) ) ? filename.substr( 0, filename.length - ext.length ) + options.ext : filename;
const src = join( inputdir, filename );
const dest = join( outputdir, destname );
// If this mapper only accepts certain extensions, and this isn't
// one of them, just copy the file
if ( shouldSkip( options, ext, filename ) ) {
return symlinkOrCopy( src ).to( dest );
}
// If this file *does* fall within this transformer's remit, but
// hasn't changed, we just copy the cached file
if ( !changed[ filename ] && options.cache.hasOwnProperty( filename ) ) {
return symlinkOrCopy( options.cache[ filename ] ).to( dest );
}
// Otherwise, we queue up a transformation
return queue.add( ( fulfil, reject ) => {
if ( this.aborted ) {
return reject( ABORTED );
}
// Create context object - this will be passed to transformers
const context = {
log: this.log,
env: config.env,
src, dest, filename
};
const transformOptions = assign( {}, options.fn.defaults, options.userOptions );
delete transformOptions.accept;
delete transformOptions.ext;
return readFile( src )
.then( buffer => buffer.toString( transformOptions.sourceEncoding ) )
.then( data => {
if ( this.aborted ) return reject( ABORTED );
let result;
try {
result = options.fn.call( context, data, transformOptions );
} catch ( e ) {
let err = createTransformError( e, src, filename, this.node );
return reject( err );
}
const codepath = resolve( this.cachedir, filename );
const { code, map } = processResult( result, data, src, dest, codepath );
writeToCacheDir( code, map, codepath, dest )
.then( () => symlinkOrCopy( codepath ).to( dest ) )
.then( () => options.cache[ filename ] = codepath )
.then( fulfil );
})
.catch( reject );
}).catch( err => {
queue.abort();
throw err;
});
});
return Promise.all( promises );
}).then( () => {
queue.off( 'error', reject );
fulfil();
}, reject );
});
}
function processResult ( result, original, src, dest, codepath ) {
if ( typeof result === 'object' && 'code' in result ) {
// if a sourcemap was returned, use it
if ( result.map ) {
return {
code: result.code.replace( SOURCEMAP_COMMENT, '' ) + getSourcemapComment( encodeURI( codepath + '.map' ), extname( codepath ) ),
map: processSourcemap( result.map, src, dest, original )
};
}
// otherwise we might have an inline sourcemap
else {
return processInlineSourceMap( result.code, src, dest, original, codepath );
}
}
if ( typeof result === 'string' ) {
return processInlineSourceMap( result, src, dest, original, codepath );
}
return { code: result, map: null };
}
function isDataURI ( str ) {
return /^data:/.test( str ); // TODO beef this up
}
function processInlineSourceMap ( code, src, dest, original, codepath ) {
// if there's an inline sourcemap, process it
let match = SOURCEMAP_COMMENT.exec( code );
let map = null;
if ( match && isDataURI( match[1] ) ) {
match = /base64,(.+)$/.exec( match[1] );
if ( !match ) {
throw new Error( 'sourceMappingURL is not base64-encoded' );
}
let json = atob( match[1] );
map = processSourcemap( json, src, dest, original );
code = code.replace( SOURCEMAP_COMMENT, '' ) + getSourcemapComment( encodeURI( codepath + '.map' ), extname( codepath ) );
}
return { code, map };
}
function writeToCacheDir ( code, map, codepath ) {
if ( map ) {
return Promise.all([
writeFile( codepath, code ),
writeFile( codepath + '.map', JSON.stringify( map ) )
]);
} else {
return writeFile( codepath, code );
}
}
function createTransformError ( original, src, filename, node ) {
const err = typeof original === 'string' ? new Error( original ) : original;
let message = 'An error occurred while processing ' + chalk.magenta( src );
let creator;
if ( creator = node.input._findCreator( filename ) ) {
message += ` (this file was created by the ${creator.id} transformation)`;
}
const { line, column } = extractLocationInfo( err );
err.file = src;
err.line = line;
err.column = column;
return err;
}
function processSourcemap ( map, src, dest, data ) {
if ( typeof map === 'string' ) {
map = JSON.parse( map );
}
if ( !map ) {
return null;
}
map.file = dest;
map.sources = [ src ];
map.sourcesContent = [ data ];
return map;
}
function shouldSkip ( options, ext, filename ) {
let filter;
if ( filter = options.accept ) {
let i;
for ( i=0; i<filter.length; i++ ) {
const flt = filter[i];
if ( typeof flt === 'string' && flt === ext ) {
return false;
} else if ( isRegExp( flt ) && flt.test( filename ) ) {
return false;
}
}
return true;
}
return false;
}
function atob ( base64 ) {
return new Buffer( base64, 'base64' ).toString( 'utf8' );
}