Skip to content

Commit

Permalink
t push
Browse files Browse the repository at this point in the history
Merge branch 'master' of github.com:babel/babel-loader
  • Loading branch information
sebmck committed Apr 3, 2015
2 parents a681fdd + e3e9057 commit 95d0b87
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 9 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ loaders: [

See the `babel` [options](http://babeljs.io/docs/usage/options/)

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.

Note: The `sourceMap` option is ignored, instead sourceMaps are automatically enabled when webpack is configured to use them (via the `devtool` config option).

## License

MIT © Luis Couto
99 changes: 93 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
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; }
Expand All @@ -9,7 +15,8 @@ var loaderUtils = require('loader-utils'),
module.exports = function (source, inputSourceMap) {

var options = loaderUtils.parseQuery(this.query),
result, code, map;
callback = this.async(),
result, cacheDirectory;

if (this.cacheable) {
this.cacheable();
Expand All @@ -25,14 +32,94 @@ module.exports = function (source, inputSourceMap) {
options.inputSourceMap = inputSourceMap;
options.filename = loaderUtils.getRemainingRequest(this);

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) {
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';
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "babel-loader",
"version": "4.1.0",
"version": "5.0.0",
"description": "babel module loader for webpack",
"main": "index.js",
"dependencies": {
"babel-core": "^5.0.0",
"loader-utils": "^0.2.5"
},
"peerDependencies": {
"babel-core": "^4.7.0",
"webpack": "^1.4.5"
"babel-core": "*",
"webpack": "*"
},
"devDependencies": {
"mocha": "^2.0.1",
Expand Down

0 comments on commit 95d0b87

Please sign in to comment.