diff --git a/lib/fs-cache.js b/lib/fs-cache.js index e7e34372..5578c271 100644 --- a/lib/fs-cache.js +++ b/lib/fs-cache.js @@ -11,6 +11,7 @@ */ var crypto = require('crypto'); var mkdirp = require('mkdirp'); +var findCacheDir = require('find-cache-dir'); var fs = require('fs'); var os = require('os'); var path = require('path'); @@ -126,7 +127,7 @@ var cache = module.exports = function(params, callback) { var identifier = params.identifier; var directory = (typeof params.directory === 'string') ? params.directory : - os.tmpdir(); + findCacheDir({ name: 'babel-loader' }); var file = path.join(directory, filename(source, identifier, options)); // Make sure the directory exists. diff --git a/package.json b/package.json index b9c5da59..9a99f1dc 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "lib" ], "dependencies": { + "find-cache-dir": "^0.1.1", "loader-utils": "^0.2.11", "mkdirp": "^0.5.1", "object-assign": "^4.0.1" diff --git a/test/cache.test.js b/test/cache.test.js index 072bf7b0..624ab014 100644 --- a/test/cache.test.js +++ b/test/cache.test.js @@ -12,6 +12,8 @@ var webpack = require('webpack'); describe('Filesystem Cache', function() { this.timeout(15000); + var defaultCacheDir = path.resolve(__dirname, + '../node_modules/.cache/babel-loader'); var cacheDir = path.resolve(__dirname, 'output/cache/cachefiles'); var outputDir = path.resolve(__dirname, './output/cache/'); var babelLoader = path.resolve(__dirname, '../'); @@ -43,6 +45,9 @@ describe('Filesystem Cache', function() { mkdirp(cacheDir, done); }); }); + beforeEach(function(done) { + rimraf(defaultCacheDir, done); + }); it('should output files to cache directory', function(done) { @@ -73,7 +78,7 @@ describe('Filesystem Cache', function() { }); }); - it('should output files to OS\'s tmp dir', function(done) { + it('should output files to standard cache dir by default', function(done) { var config = assign({}, globalConfig, { module: { loaders: [ @@ -93,7 +98,7 @@ describe('Filesystem Cache', function() { webpack(config, function(err, stats) { expect(err).to.be(null); - fs.readdir(os.tmpdir(), function(err, files) { + fs.readdir(defaultCacheDir, function(err, files) { files = files.filter(function(file) { return /\b[0-9a-f]{5,40}\.json\.gzip\b/.test(file); });