Skip to content

Commit bc8f9c6

Browse files
committed
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](npm/npm#4531). This caused the cache files to be created in the project folder itself, for example when using Docker: facebook/create-react-app#483.
1 parent 32de4b9 commit bc8f9c6

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ module: {
7373

7474
This loader also supports the following loader-specific option:
7575

76-
* `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.
76+
* `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
77+
directory in `node_modules/.cache/babel-loader`.
7778

7879
* `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.
7980

lib/fs-cache.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
var crypto = require('crypto');
1313
var mkdirp = require('mkdirp');
14+
var findCacheDir = require('find-cache-dir');
1415
var fs = require('fs');
1516
var os = require('os');
1617
var path = require('path');
@@ -126,7 +127,7 @@ var cache = module.exports = function(params, callback) {
126127
var identifier = params.identifier;
127128
var directory = (typeof params.directory === 'string') ?
128129
params.directory :
129-
os.tmpdir();
130+
findCacheDir({ name: 'babel-loader' });
130131
var file = path.join(directory, filename(source, identifier, options));
131132

132133
// Make sure the directory exists.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"lib"
88
],
99
"dependencies": {
10+
"find-cache-dir": "^0.1.1",
1011
"loader-utils": "^0.2.11",
1112
"mkdirp": "^0.5.1",
1213
"object-assign": "^4.0.1"

test/cache.test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ var webpack = require('webpack');
1212
describe('Filesystem Cache', function() {
1313
this.timeout(15000);
1414

15+
var defaultCacheDir = path.resolve(__dirname,
16+
'../node_modules/.cache/babel-loader');
1517
var cacheDir = path.resolve(__dirname, 'output/cache/cachefiles');
1618
var outputDir = path.resolve(__dirname, './output/cache/');
1719
var babelLoader = path.resolve(__dirname, '../');
@@ -43,6 +45,9 @@ describe('Filesystem Cache', function() {
4345
mkdirp(cacheDir, done);
4446
});
4547
});
48+
beforeEach(function(done) {
49+
rimraf(defaultCacheDir, done);
50+
});
4651

4752
it('should output files to cache directory', function(done) {
4853

@@ -73,7 +78,7 @@ describe('Filesystem Cache', function() {
7378
});
7479
});
7580

76-
it('should output files to OS\'s tmp dir', function(done) {
81+
it('should output files to standard cache dir by default', function(done) {
7782
var config = assign({}, globalConfig, {
7883
module: {
7984
loaders: [
@@ -93,7 +98,7 @@ describe('Filesystem Cache', function() {
9398
webpack(config, function(err, stats) {
9499
expect(err).to.be(null);
95100

96-
fs.readdir(os.tmpdir(), function(err, files) {
101+
fs.readdir(defaultCacheDir, function(err, files) {
97102
files = files.filter(function(file) {
98103
return /\b[0-9a-f]{5,40}\.json\.gzip\b/.test(file);
99104
});

0 commit comments

Comments
 (0)