Socket
Socket
Sign inDemoInstall

babel-loader

Package Overview
Dependencies
Maintainers
2
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-loader - npm Package Compare versions

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';
}

2

package.json
{
"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
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc