-
Notifications
You must be signed in to change notification settings - Fork 14
/
exorcise.js
74 lines (61 loc) · 2.02 KB
/
exorcise.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
/*
* grunt-exorcise
* https://github.com/mikefrey/grunt-exorcise
*
* Copyright (c) 2014 Mike Frey
* Licensed under the MIT license.
*/
'use strict';
var fs = require('fs')
var path = require('path')
var async = require('async')
var resumer = require('resumer')
var exorcist = require('exorcist')
var concat = require('concat-stream')
module.exports = function(grunt) {
grunt.registerMultiTask('exorcise', 'Move Browserify source maps to a separate file', function() {
var done = this.async()
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
url: null,
root: null,
base: null,
strict: false
});
// Iterate over all specified file groups.
async.eachSeries(this.files, function(file, cb) {
if (!file.src || !file.src[0])
return fail('Source file was not defined.', cb)
if (!grunt.file.exists(file.src[0]))
return fail('Source file "' + file.src[0].cyan + '" not found.', cb)
var src = file.src[0]
var dest = file.dest
grunt.log.writeln('Exorcising source map from %s', src.cyan)
// ensure that the dest directory exists
grunt.file.mkdir(path.dirname(dest))
// a bit of indirection happens here.
// We can't stream the file directly through exorcist and
// back to the file system without buffering it first because
// we end up with an empty file streaming to exorcist.
var data = fs.readFileSync(src, 'utf8')
var stream = resumer().queue(data).end()
var write = concat(function(data) {
var out = options.bundleDest || src
grunt.file.write(out, data)
cb()
})
// setup exorcist
var ex = exorcist(dest, options.url, options.root, options.base)
ex.on('missing-map', function(msg) {
grunt.log.warn(msg);
if (options.strict)
done(false)
})
stream.pipe(ex).pipe(write)
}, done)
})
function fail(msg, cb) {
grunt.log.warn(msg)
throw new Error(msg)
}
}