Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tests to be ran with dummy Babel compilation #1605

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const arrify = require('arrify');
const ms = require('ms');
const babelConfigHelper = require('./lib/babel-config');
const CachingPrecompiler = require('./lib/caching-precompiler');
const NativeCachingPrecompiler = require('./lib/native-caching-precompiler');
const RunStatus = require('./lib/run-status');
const AvaError = require('./lib/ava-error');
const AvaFiles = require('./lib/ava-files');
Expand Down Expand Up @@ -118,13 +119,21 @@ class Api extends EventEmitter {
this.options.cacheDir = cacheDir;

const isPowerAssertEnabled = this.options.powerAssert !== false;
const compileJavascriptFiles = this.options.compileJavascriptFiles !== false;
return babelConfigHelper.build(this.options.projectDir, cacheDir, this.options.babelConfig, isPowerAssertEnabled)
.then(result => {
this.precompiler = new CachingPrecompiler({
path: cacheDir,
getBabelOptions: result.getOptions,
babelCacheKeys: result.cacheKeys
});
if (compileJavascriptFiles) {
this.precompiler = new CachingPrecompiler({
path: cacheDir,
getBabelOptions: result.getOptions,
babelCacheKeys: result.cacheKeys
});
} else {
this.precompiler = new NativeCachingPrecompiler({
path: cacheDir,
babelCacheKeys: result.cacheKeys
});
}
});
}

Expand Down Expand Up @@ -262,7 +271,6 @@ class Api extends EventEmitter {
const options = {
runOnlyExclusive: this.options.match.length > 0
};

resolve(test.run(options));
}).catch(err => {
err.file = file;
Expand Down
3 changes: 2 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ exports.run = () => {
concurrency: conf.concurrency ? parseInt(conf.concurrency, 10) : 0,
updateSnapshots: conf.updateSnapshots,
snapshotDir: conf.snapshotDir ? path.resolve(projectDir, conf.snapshotDir) : null,
color: conf.color
color: conf.color,
compileJavascriptFiles: conf.compileJavascriptFiles !== false
});

let reporter;
Expand Down
39 changes: 39 additions & 0 deletions lib/native-caching-precompiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
const fs = require('fs');
const cachingTransform = require('caching-transform');
const packageHash = require('package-hash');
const stripBomBuf = require('strip-bom-buf');
const autoBind = require('auto-bind');
const md5Hex = require('md5-hex');

class NativeCachingPrecompiler {

constructor(options) {
autoBind(this);
this.babelCacheKeys = options.babelCacheKeys;
this.cacheDirPath = options.path;
this.fileHashes = {};
this.transform = cachingTransform({
cacheDir: this.cacheDirPath,
ext: '.js',
factory: () => code => code.toString(), // Native raw
salt: packageHash.sync([require.resolve('../package.json')], this.babelCacheKeys),
hash: (code, filePath, salt) => {
const hash = md5Hex([code, filePath, salt]);
this.fileHashes[filePath] = hash;
return hash;
}
});
}

precompileFile(filePath) {
if (!this.fileHashes[filePath]) {
const source = stripBomBuf(fs.readFileSync(filePath));
this.transform(source, filePath);
}

return this.fileHashes[filePath];
}
}

module.exports = NativeCachingPrecompiler;
5 changes: 3 additions & 2 deletions lib/process-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ exports.installSourceMapSupport = () => {
exports.installPrecompilerHook = () => {
installPrecompiler(filename => {
const precompiled = opts.precompiled[filename];

if (precompiled) {
sourceMapCache.set(filename, path.join(cacheDir, `${precompiled}.js.map`));
if (opts.compileJavascriptFiles) {
sourceMapCache.set(filename, path.join(cacheDir, `${precompiled}.js.map`));
}
return fs.readFileSync(path.join(cacheDir, `${precompiled}.js`), 'utf8');
}

Expand Down
19 changes: 14 additions & 5 deletions profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const arrify = require('arrify');
const resolveCwd = require('resolve-cwd');
const babelConfigHelper = require('./lib/babel-config');
const CachingPrecompiler = require('./lib/caching-precompiler');
const NativeCachingPrecompiler = require('./lib/native-caching-precompiler');
const globals = require('./lib/globals');

function resolveModules(modules) {
Expand Down Expand Up @@ -78,11 +79,19 @@ const cacheDir = findCacheDir({

babelConfigHelper.build(process.cwd(), cacheDir, conf.babel, true)
.then(result => {
const precompiler = new CachingPrecompiler({
path: cacheDir,
getBabelOptions: result.getOptions,
babelCacheKeys: result.cacheKeys
});
let precompiler = null;
if (conf.compileJavascriptFiles) {
precompiler = new CachingPrecompiler({
path: cacheDir,
getBabelOptions: result.getOptions,
babelCacheKeys: result.cacheKeys
});
} else {
precompiler = new NativeCachingPrecompiler({
path: cacheDir,
babelCacheKeys: result.cacheKeys
});
}

const precompiled = {};
precompiled[file] = precompiler.precompileFile(file);
Expand Down