From bc8f9c682944c63a51dc080a8087d519c0da13ac Mon Sep 17 00:00:00 2001 From: Ville Immonen Date: Sun, 18 Sep 2016 03:13:32 +0300 Subject: [PATCH] Use standard cache dir as default cacheDirectory Set the default cache directory as the [common cache directory](https://github.com/avajs/find-cache-dir), `./node_modules/.cache/babel-loader`. Previously, when `cacheDirectory` was set to `true`, babel-loader tried to use the operating system's temporary directory as a cache directory. However, when running npm scripts as the root user, [npm overrides the TMPDIR environment variable](https://github.com/npm/npm/issues/4531). This caused the cache files to be created in the project folder itself, for example when using Docker: facebookincubator/create-react-app#483. --- README.md | 3 ++- lib/fs-cache.js | 3 ++- package.json | 1 + test/cache.test.js | 9 +++++++-- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 01cd32b7..f182d806 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,8 @@ module: { This loader also supports the following loader-specific option: - * `cacheDirectory`: Default `false`. 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. If the value is blank (`loader: 'babel-loader?cacheDirectory'`) the loader will use the default OS temporary file directory. + * `cacheDirectory`: Default `false`. 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. If the value is blank (`loader: 'babel-loader?cacheDirectory'`) the loader will use the default cache + directory in `node_modules/.cache/babel-loader`. * `cacheIdentifier`: Default is a string composed by the babel-core's version, the babel-loader's version, the contents of .babelrc file if it exists and the value of the environment variable `BABEL_ENV` with a fallback to the `NODE_ENV` environment variable. This can be set to a custom value to force cache busting if the identifier changes. 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); });