forked from babel/babel-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (101 loc) · 3.21 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var loaderUtils = require('loader-utils'),
babel = require('babel-core'),
crypto = require('crypto'),
fs = require('fs'),
path = require('path'),
os = require('os'),
zlib = require('zlib'),
version = require('./package').version,
toBoolean = function (val) {
if (val === 'true') { return true; }
if (val === 'false') { return false; }
return val;
};
module.exports = function (source, inputSourceMap) {
var options = loaderUtils.parseQuery(this.query),
callback = this.async(),
result, cacheDirectory;
if (this.cacheable) {
this.cacheable();
}
// Convert 'true'/'false' to true/false
options = Object.keys(options).reduce(function (accumulator, key) {
accumulator[key] = toBoolean(options[key]);
return accumulator;
}, {});
options.sourceMap = this.sourceMap;
options.inputSourceMap = inputSourceMap;
options.filename = loaderUtils.getRemainingRequest(this);
cacheDirectory = options.cacheDirectory;
delete options.cacheDirectory;
if (cacheDirectory === true) cacheDirectory = os.tmpdir();
if (cacheDirectory){
cachedTranspile(cacheDirectory, source, options, onResult);
} else {
onResult(null, transpile(source, options));
}
function onResult(err, result){
if (err) return callback(err);
callback(err, err ? null : result.code, err ? null : result.map);
}
};
function transpile(source, options){
var result = babel.transform(source, options);
var code = result.code;
var map = result.map;
if (map) {
map.sourcesContent = [source];
}
return {
code: code,
map: map
};
}
function cachedTranspile(cacheDirectory, source, options, callback){
var cacheFile = path.join(cacheDirectory, buildCachePath(cacheDirectory, source, options));
readCache(cacheFile, function(err, result){
if (err){
try {
result = transpile(source, options);
} catch (e){
return callback(e);
}
writeCache(cacheFile, result, function(err){
callback(err, result);
});
} else {
callback(null, result);
}
});
}
function readCache(cacheFile, callback){
fs.readFile(cacheFile, function(err, data){
if (err) return callback(err);
zlib.gunzip(data, function(err, content){
if (err) return callback(err);
try {
content = JSON.parse(content);
} catch (e){
return callback(e);
}
callback(null, content);
});
});
}
function writeCache(cacheFile, result, callback){
var content = JSON.stringify(result);
zlib.gzip(content, function(err, data){
if (err) return callback(err);
fs.writeFile(cacheFile, data, callback);
});
}
function buildCachePath(dir, source, options){
var hash = crypto.createHash('SHA1');
hash.end(JSON.stringify({
loaderVersion: version,
babelVersion: babel.version,
source: source,
options: options
}));
return 'babel-loader-cache-' + hash.read().toString('hex') + '.json.gzip';
}