Skip to content

Commit 1b214be

Browse files
Vadim Demedessindresorhus
Vadim Demedes
authored andcommitted
clean up caching precompiler (#1077)
1 parent 6165a23 commit 1b214be

File tree

6 files changed

+54
-40
lines changed

6 files changed

+54
-40
lines changed

api.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ Api.prototype._setupPrecompiler = function (files) {
127127
this.options.cacheDir = cacheDir;
128128

129129
var isPowerAssertEnabled = this.options.powerAssert !== false;
130-
this.precompiler = new CachingPrecompiler(cacheDir, this.options.babelConfig, isPowerAssertEnabled);
130+
this.precompiler = new CachingPrecompiler({
131+
path: cacheDir,
132+
babel: this.options.babelConfig,
133+
powerAssert: isPowerAssertEnabled
134+
});
131135
};
132136

133137
Api.prototype._run = function (files, options) {

lib/caching-precompiler.js

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,26 @@ var path = require('path');
33
var fs = require('fs');
44
var convertSourceMap = require('convert-source-map');
55
var cachingTransform = require('caching-transform');
6-
var stripBom = require('strip-bom');
7-
var md5Hex = require('md5-hex');
86
var packageHash = require('package-hash');
7+
var stripBom = require('strip-bom');
98
var autoBind = require('auto-bind');
9+
var md5Hex = require('md5-hex');
1010
var babelConfigHelper = require('./babel-config');
1111

12-
function CachingPrecompiler(cacheDirPath, babelConfig, powerAssert) {
12+
function CachingPrecompiler(options) {
1313
if (!(this instanceof CachingPrecompiler)) {
1414
throw new TypeError('Class constructor CachingPrecompiler cannot be invoked without \'new\'');
1515
}
1616

17-
this.babelConfig = babelConfigHelper.validate(babelConfig);
18-
this.powerAssert = powerAssert;
19-
this.cacheDirPath = cacheDirPath;
20-
this.fileHashes = {};
21-
2217
autoBind(this);
2318

19+
options = options || {};
20+
21+
this.babelConfig = babelConfigHelper.validate(options.babel);
22+
this.cacheDirPath = options.path;
23+
this.powerAssert = Boolean(options.powerAssert);
24+
this.fileHashes = {};
25+
2426
this.transform = this._createTransform();
2527
}
2628

@@ -37,14 +39,10 @@ CachingPrecompiler.prototype.precompileFile = function (filePath) {
3739
};
3840

3941
// conditionally called by caching-transform when precompiling is required
40-
CachingPrecompiler.prototype._factory = function () {
41-
this._init();
42-
43-
return this._transform;
44-
};
45-
4642
CachingPrecompiler.prototype._init = function () {
4743
this.babel = require('babel-core');
44+
45+
return this._transform;
4846
};
4947

5048
CachingPrecompiler.prototype._transform = function (code, filePath, hash) {
@@ -57,15 +55,8 @@ CachingPrecompiler.prototype._transform = function (code, filePath, hash) {
5755
var mapPath = path.join(this.cacheDirPath, hash + '.js.map');
5856
fs.writeFileSync(mapPath, JSON.stringify(result.map));
5957

60-
// When loading the test file, test workers intercept the require call and
61-
// load the cached code instead. Libraries like nyc may also be intercepting
62-
// require calls, however they won't know that different code was loaded.
63-
// They may then attempt to resolve a source map from the original file
64-
// location.
65-
//
66-
// Add a source map file comment to the cached code. The file path is
67-
// relative from the directory of the original file to where the source map
68-
// is cached. This will allow the source map to be resolved.
58+
// append source map comment to transformed code
59+
// so that other libraries (like nyc) can find the source map
6960
var dirPath = path.dirname(filePath);
7061
var relativeMapPath = path.relative(dirPath, mapPath);
7162
var comment = convertSourceMap.generateMapFileComment(relativeMapPath);
@@ -74,14 +65,18 @@ CachingPrecompiler.prototype._transform = function (code, filePath, hash) {
7465
};
7566

7667
CachingPrecompiler.prototype._createTransform = function () {
77-
var salt = packageHash.sync(
78-
[require.resolve('../package.json')].concat(babelConfigHelper.pluginPackages),
79-
JSON.stringify(this.babelConfig),
80-
process.version.split('.')[0]
81-
);
68+
var pluginPackages = babelConfigHelper.pluginPackages;
69+
var avaPackage = require.resolve('../package.json');
70+
var packages = [avaPackage].concat(pluginPackages);
71+
72+
var majorNodeVersion = process.version.split('.')[0];
73+
var babelConfig = JSON.stringify(this.babelConfig);
74+
var packageSalt = babelConfig + majorNodeVersion;
75+
76+
var salt = packageHash.sync(packages, packageSalt);
8277

8378
return cachingTransform({
84-
factory: this._factory,
79+
factory: this._init,
8580
cacheDir: this.cacheDirPath,
8681
hash: this._generateHash,
8782
salt: salt,

profile.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,14 @@ var cacheDir = findCacheDir({
6060
name: 'ava',
6161
files: [file]
6262
}) || uniqueTempDir();
63+
64+
var precompiler = new CachingPrecompiler({
65+
path: cacheDir,
66+
babel: conf.babel
67+
});
68+
6369
var precompiled = {};
64-
precompiled[file] = new CachingPrecompiler(cacheDir, conf.babel).precompileFile(file);
70+
precompiled[file] = precompiler.precompileFile(file);
6571

6672
var opts = {
6773
file: file,

test/caching-precompiler.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ sinon.spy(babel, 'transform');
2525

2626
test('creation with new', function (t) {
2727
var tempDir = uniqueTempDir();
28-
var precompiler = new CachingPrecompiler(tempDir, null);
28+
var precompiler = new CachingPrecompiler({path: tempDir});
2929
t.is(precompiler.cacheDirPath, tempDir);
3030
t.end();
3131
});
3232

3333
test('must be called with new', function (t) {
3434
t.throws(function () {
3535
var cachingPrecompiler = CachingPrecompiler;
36-
cachingPrecompiler(uniqueTempDir(), null);
36+
cachingPrecompiler({path: uniqueTempDir()});
3737
}, {message: 'Class constructor CachingPrecompiler cannot be invoked without \'new\''});
3838
t.end();
3939
});
4040

4141
test('adds files and source maps to the cache directory as needed', function (t) {
4242
var tempDir = uniqueTempDir();
43-
var precompiler = new CachingPrecompiler(tempDir, null);
43+
var precompiler = new CachingPrecompiler({path: tempDir});
4444

4545
t.false(fs.existsSync(tempDir), 'cache directory is not created before it is needed');
4646

@@ -56,7 +56,7 @@ test('adds files and source maps to the cache directory as needed', function (t)
5656

5757
test('adds a map file comment to the cached files', function (t) {
5858
var tempDir = uniqueTempDir();
59-
var precompiler = new CachingPrecompiler(tempDir, null);
59+
var precompiler = new CachingPrecompiler({path: tempDir});
6060

6161
precompiler.precompileFile(fixture('es2015.js'));
6262

@@ -84,7 +84,11 @@ test('adds a map file comment to the cached files', function (t) {
8484

8585
test('uses default babel options when babelConfig === "default"', function (t) {
8686
var tempDir = uniqueTempDir();
87-
var precompiler = new CachingPrecompiler(tempDir, 'default');
87+
var precompiler = new CachingPrecompiler({
88+
path: tempDir,
89+
babel: 'default'
90+
});
91+
8892
babel.transform.reset();
8993

9094
precompiler.precompileFile(fixture('es2015.js'));
@@ -104,7 +108,11 @@ test('uses default babel options when babelConfig === "default"', function (t) {
104108

105109
test('allows babel config from package.json/babel when babelConfig === "inherit"', function (t) {
106110
var tempDir = uniqueTempDir();
107-
var precompiler = new CachingPrecompiler(tempDir, 'inherit');
111+
var precompiler = new CachingPrecompiler({
112+
path: tempDir,
113+
babel: 'inherit'
114+
});
115+
108116
babel.transform.reset();
109117

110118
precompiler.precompileFile(fixture('es2015.js'));
@@ -122,7 +130,8 @@ test('allows babel config from package.json/babel when babelConfig === "inherit"
122130

123131
test('does not modify plugins array in babelConfig', function (t) {
124132
var plugins = [];
125-
var precompiler = new CachingPrecompiler(uniqueTempDir(), {
133+
var precompiler = new CachingPrecompiler({
134+
path: uniqueTempDir(),
126135
plugins: plugins
127136
});
128137

test/fork.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var _fork = require('../lib/fork.js');
55
var CachingPrecompiler = require('../lib/caching-precompiler');
66

77
var cacheDir = path.join(__dirname, '../node_modules/.cache/ava');
8-
var precompiler = new CachingPrecompiler(cacheDir);
8+
var precompiler = new CachingPrecompiler({path: cacheDir});
99

1010
function fork(testPath) {
1111
var hash = precompiler.precompileFile(testPath);

test/hooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var _fork = require('../lib/fork.js');
66
var CachingPrecompiler = require('../lib/caching-precompiler');
77

88
var cacheDir = path.join(__dirname, '../node_modules/.cache/ava');
9-
var precompiler = new CachingPrecompiler(cacheDir);
9+
var precompiler = new CachingPrecompiler({path: cacheDir});
1010

1111
function fork(testPath) {
1212
var hash = precompiler.precompileFile(testPath);

0 commit comments

Comments
 (0)