Skip to content

Commit

Permalink
Use standard cache dir as default cacheDirectory
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fson committed Sep 18, 2016
1 parent 32de4b9 commit bc8f9c6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion lib/fs-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 7 additions & 2 deletions test/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '../');
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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: [
Expand All @@ -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);
});
Expand Down

0 comments on commit bc8f9c6

Please sign in to comment.