-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.js
96 lines (79 loc) · 3.18 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
'use strict';
var mold = require('mold-source-map')
, path = require('path')
, fs = require('fs')
, mkdirp = require('mkdirp')
, isStream = require('is-stream')
, fs = require('fs');
function separate(src, url, root, base) {
src.sourceRoot(root || src.sourcemap.getProperty('sourceRoot') || '');
if (base) {
src.mapSources(mold.mapPathRelativeTo(base));
}
var json = src.toJSON(2);
var comment = '';
var commentRx = /^\s*\/(\/|\*)[@#]\s+sourceMappingURL/mg;
var commentMatch = commentRx.exec(src.source);
var commentBlock = (commentMatch && commentMatch[1] === '*');
if (commentBlock) {
comment = '/*# sourceMappingURL=' + url + ' */';
} else {
comment = '//# sourceMappingURL=' + url;
}
return { json: json, comment: comment }
}
var go = module.exports =
/**
*
* Externalizes the source map of the file streamed in.
*
* The source map is written as JSON to `file`, and the original file is streamed out with its
* `sourceMappingURL` set to the path of `file` (or to the value of `url`).
*
* #### Events (in addition to stream events)
*
* - `missing-map` emitted if no map was found in the stream and errorOnMissing is falsey
* (the src is still piped through in this case, but no map file is written)
*
* @name exorcist
* @function
* @param {String|Object} input file path or writable stream where the source map will be written
* @param {String=} url full URL to the map file, set as `sourceMappingURL` in the streaming output (default: file)
* @param {String=} root root URL for loading relative source paths, set as `sourceRoot` in the source map (default: '')
* @param {String=} base base path for calculating relative source paths (default: use absolute paths)
* @param {Boolean=} errorOnMissing when truthy, causes 'error' to be emitted instead of 'missing-map' if no map was found in the stream (default: falsey)
* @return {TransformStream} transform stream into which to pipe the code containing the source map
*/
function exorcist(input, url, root, base, errorOnMissing) {
var missingMapMsg = "The code that you piped into exorcist contains no source map!";
var stream = mold.transform(function(src, write) {
if (!src.sourcemap) {
if (errorOnMissing) return stream.emit('error', new Error(missingMapMsg));
stream.emit(
'missing-map'
, missingMapMsg + '\n'
+ 'Therefore it was piped through as is and no external map file generated.'
);
return write(src.source);
}
if (isStream(input) && isStream.writable(stream)) {
return stream.emit('error', new Error('Must provide a writable stream'));
}
if (isStream(input) && typeof url !== 'string') {
return stream.emit('error', new Error('map file URL is required when using stream output'));
}
url = url || path.basename(input)
var separated = separate(src, url, root, base);
if (isStream(input)) {
return input.end(separated.json, 'utf8', done);
}
mkdirp(path.dirname(input))
.then(() => fs.writeFile(input, separated.json, 'utf8', done))
.catch(done);
function done(err) {
if (err) return stream.emit('error', err);
write(separated.comment);
}
});
return stream;
}