-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
108 lines (85 loc) · 3.58 KB
/
index.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
var sander = require( 'sander' ),
path = require( 'path' ),
mapSeries = require( 'promise-map-series' ),
minimatch = require( 'minimatch' );
var SourceNode = require( 'source-map' ).SourceNode;
var SourceMapConsumer = require( 'source-map' ).SourceMapConsumer;
var sourceMapRegExp = new RegExp(/(?:\/\/#|\/\/@|\/\*#)\s*sourceMappingURL=(.*?)\s*(?:\*\/\s*)?$/);
var extensionsRegExp = new RegExp(/(\.js|\.css)$/);
module.exports = function concat ( inputdir, outputdir, options ) {
if ( !options.dest ) {
throw new Error( 'You must pass a \'dest\' option to gobble-concat' );
}
if ( options.writeSourcemap === undefined ) {
options.writeSourcemap = !!options.dest.match( extensionsRegExp );
}
return sander.lsr( inputdir ).then( function ( allFiles ) {
var patterns = options.files,
alreadySeen = {},
nodes = [];
if ( !patterns ) {
// use all files
return processFiles( allFiles.sort( options.sort ) ).then( writeResult );
}
if ( typeof patterns === 'string' ) {
patterns = [ patterns ];
}
return mapSeries( patterns, function ( pattern ) {
var filtered = allFiles.filter( function ( filename ) {
var shouldInclude = !alreadySeen[ filename ] && minimatch( filename, pattern );
if ( shouldInclude ) {
alreadySeen[ filename ] = true;
}
return shouldInclude;
});
return processFiles( filtered.sort( options.sort ) );
}).then( writeResult );
function processFiles ( filenames ) {
return mapSeries( filenames.sort( options.sort ), function ( filename ) {
return sander.readFile( inputdir, filename ).then( function ( fileContents ) {
/// Run a regexp against the code to check for source mappings.
var match = sourceMapRegExp.exec(fileContents);
if (!match) {
// if (options.verbose) console.log('Creating ident sourcemap for ', filename);
var newNode = new SourceNode(1, 1, filename, fileContents.toString());
newNode.setSourceContent(filename, fileContents.toString());
nodes.push( newNode );
} else {
var sourcemapFilename = match[1];
return sander.readFile( inputdir, path.dirname(filename), sourcemapFilename ).then( function ( mapContents ) {
// Sourcemap exists
var parsedMap = new SourceMapConsumer( mapContents.toString() );
nodes.push( SourceNode.fromStringWithSourceMap( fileContents.toString(), parsedMap ) );
// if (options.verbose) console.log('Loaded sourcemap for ', filename + ': ' + sourcemapFilename + "(" + mapContents.length + " bytes)");
}, function(err) {
throw new Error('File ' + inputdir + ' / ' + filename + ' refers to a non-existing sourcemap at ' + sourcemapFilename + ' ' + err);
});
}
});
});
}
function writeResult () {
if (!nodes[0]) {
// Degenerate case: no matched files
return sander.writeFile( outputdir, options.dest, '' );
}
var separatorNode = new SourceNode(null, null, null, options.separator || '\n\n');
var dest = new SourceNode(null, null, null, '');
dest.add(nodes[0]);
for (var i=1; i<nodes.length; i++) {
dest.add(separatorNode);
dest.add(nodes[i]);
}
var generated = dest.toStringWithSourceMap();
if ( options.writeSourcemap ) {
var sourceMapLocation = '\n\n//# sourceMappingURL=' + path.join(outputdir, options.dest + '.map') + '\n'
return sander.Promise.all([
sander.writeFile( outputdir, options.dest, generated.code + sourceMapLocation ),
sander.writeFile( outputdir, options.dest + '.map', generated.map.toString() )
]);
} else {
return sander.writeFile( outputdir, options.dest, generated.code);
}
}
});
};