-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
48 lines (40 loc) · 1.7 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
"use strict";
var fs = require('fs');
var espowerSource = require('espower-source');
var convert = require('convert-source-map');
var createPreprocessor = function(args, config, logger, helper) {
config = config || {};
var log = logger.create('preprocessor.espower');
var defaultOptions = {}; // see https://github.com/twada/espower-source#api
var options = helper.merge(defaultOptions, args.options || {}, config.options || {});
var transformPath = args.transformPath || config.transformPath || function(filePath) {
return filePath.replace(/\.js$/, '.espowered.js');
};
return function(content, file, done) {
log.debug('Processing "%s".', file.originalPath);
file.path = transformPath(file.originalPath);
var opts = helper.merge({}, options);
if (opts.ignoreUpstreamSourceMap && file.sourceMap) {
log.debug("detected upstream sourceMap info, but ignoring");
} else if (file.sourceMap) {
log.debug("detected upstream sourceMap info");
opts = helper.merge(opts, { sourceMap: file.sourceMap });
}
var modified = espowerSource(content, file.originalPath, opts);
if (opts.emitActualCode) {
log.debug("emit " + file.path);
fs.writeFileSync(file.path, modified);
}
var commented = convert.fromSource(modified);
if (commented) {
file.sourceMap = commented.toObject();
} else {
file.sourceMap = null;
}
done(null, modified);
};
};
createPreprocessor.$inject = ['args', 'config.espowerPreprocessor', 'logger', 'helper'];
module.exports = {
'preprocessor:espower': ['factory', createPreprocessor]
};