|
|
@ -3,24 +3,26 @@ var path = require('path'); |
|
|
|
var fs = require('fs'); |
|
|
|
var convertSourceMap = require('convert-source-map'); |
|
|
|
var cachingTransform = require('caching-transform'); |
|
|
|
var stripBom = require('strip-bom'); |
|
|
|
var md5Hex = require('md5-hex'); |
|
|
|
var packageHash = require('package-hash'); |
|
|
|
var stripBom = require('strip-bom'); |
|
|
|
var autoBind = require('auto-bind'); |
|
|
|
var md5Hex = require('md5-hex'); |
|
|
|
var babelConfigHelper = require('./babel-config'); |
|
|
|
|
|
|
|
function CachingPrecompiler(cacheDirPath, babelConfig, powerAssert) { |
|
|
|
function CachingPrecompiler(options) { |
|
|
|
if (!(this instanceof CachingPrecompiler)) { |
|
|
|
throw new TypeError('Class constructor CachingPrecompiler cannot be invoked without \'new\''); |
|
|
|
} |
|
|
|
|
|
|
|
this.babelConfig = babelConfigHelper.validate(babelConfig); |
|
|
|
this.powerAssert = powerAssert; |
|
|
|
this.cacheDirPath = cacheDirPath; |
|
|
|
this.fileHashes = {}; |
|
|
|
|
|
|
|
autoBind(this); |
|
|
|
|
|
|
|
options = options || {}; |
|
|
|
|
|
|
|
this.babelConfig = babelConfigHelper.validate(options.babel); |
|
|
|
this.cacheDirPath = options.path; |
|
|
|
this.powerAssert = Boolean(options.powerAssert); |
|
|
|
this.fileHashes = {}; |
|
|
|
|
|
|
|
this.transform = this._createTransform(); |
|
|
|
} |
|
|
|
|
|
|
@ -37,14 +39,10 @@ CachingPrecompiler.prototype.precompileFile = function (filePath) { |
|
|
|
}; |
|
|
|
|
|
|
|
// conditionally called by caching-transform when precompiling is required
|
|
|
|
CachingPrecompiler.prototype._factory = function () { |
|
|
|
this._init(); |
|
|
|
|
|
|
|
return this._transform; |
|
|
|
}; |
|
|
|
|
|
|
|
CachingPrecompiler.prototype._init = function () { |
|
|
|
this.babel = require('babel-core'); |
|
|
|
|
|
|
|
return this._transform; |
|
|
|
}; |
|
|
|
|
|
|
|
CachingPrecompiler.prototype._transform = function (code, filePath, hash) { |
|
|
@ -57,15 +55,8 @@ CachingPrecompiler.prototype._transform = function (code, filePath, hash) { |
|
|
|
var mapPath = path.join(this.cacheDirPath, hash + '.js.map'); |
|
|
|
fs.writeFileSync(mapPath, JSON.stringify(result.map)); |
|
|
|
|
|
|
|
// When loading the test file, test workers intercept the require call and
|
|
|
|
// load the cached code instead. Libraries like nyc may also be intercepting
|
|
|
|
// require calls, however they won't know that different code was loaded.
|
|
|
|
// They may then attempt to resolve a source map from the original file
|
|
|
|
// location.
|
|
|
|
//
|
|
|
|
// Add a source map file comment to the cached code. The file path is
|
|
|
|
// relative from the directory of the original file to where the source map
|
|
|
|
// is cached. This will allow the source map to be resolved.
|
|
|
|
// append source map comment to transformed code
|
|
|
|
// so that other libraries (like nyc) can find the source map
|
|
|
|
var dirPath = path.dirname(filePath); |
|
|
|
var relativeMapPath = path.relative(dirPath, mapPath); |
|
|
|
var comment = convertSourceMap.generateMapFileComment(relativeMapPath); |
|
|
@ -74,14 +65,18 @@ CachingPrecompiler.prototype._transform = function (code, filePath, hash) { |
|
|
|
}; |
|
|
|
|
|
|
|
CachingPrecompiler.prototype._createTransform = function () { |
|
|
|
var salt = packageHash.sync( |
|
|
|
[require.resolve('../package.json')].concat(babelConfigHelper.pluginPackages), |
|
|
|
JSON.stringify(this.babelConfig), |
|
|
|
process.version.split('.')[0] |
|
|
|
); |
|
|
|
var pluginPackages = babelConfigHelper.pluginPackages; |
|
|
|
var avaPackage = require.resolve('../package.json'); |
|
|
|
var packages = [avaPackage].concat(pluginPackages); |
|
|
|
|
|
|
|
var majorNodeVersion = process.version.split('.')[0]; |
|
|
|
var babelConfig = JSON.stringify(this.babelConfig); |
|
|
|
var packageSalt = babelConfig + majorNodeVersion; |
|
|
|
|
|
|
|
var salt = packageHash.sync(packages, packageSalt); |
|
|
|
|
|
|
|
return cachingTransform({ |
|
|
|
factory: this._factory, |
|
|
|
factory: this._init, |
|
|
|
cacheDir: this.cacheDirPath, |
|
|
|
hash: this._generateHash, |
|
|
|
salt: salt, |
|
|
|