babel-loader
Advanced tools
Comparing version 4.1.0 to 4.2.0
99
index.js
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) { | ||
@@ -12,3 +18,4 @@ if (val === 'true') { return true; } | ||
var options = loaderUtils.parseQuery(this.query), | ||
result, code, map; | ||
callback = this.async(), | ||
result, cacheDirectory; | ||
@@ -29,6 +36,25 @@ if (this.cacheable) { | ||
result = babel.transform(source, options); | ||
code = result.code; | ||
cacheDirectory = options.cacheDirectory; | ||
delete options.cacheDirectory; | ||
map = result.map; | ||
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) { | ||
@@ -38,4 +64,65 @@ map.sourcesContent = [source]; | ||
this.callback(null, code, map); | ||
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'; | ||
} |
{ | ||
"name": "babel-loader", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "babel module loader for webpack", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -79,4 +79,11 @@ # babel-loader | ||
This loader also supports the following loader-specific option: | ||
* `cacheDirectory`: When set, the given directory will be used to cache the results of the loader. | ||
Future webpack builds will attempt to read from the cache to avoid needing to run the potentially | ||
expensive Babel recompilation process on each run. A value of `true` will cause the loader to | ||
use the default OS temporary file directory. | ||
## License | ||
MIT © Luis Couto |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7203
125
89
1
1