Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Note that Babel ignores empty string values, even for NODE_ENV. Here
// default to 'test' unless NODE_ENV is defined, in which case fall back to
// Babel's default of 'development' if it's empty.
const envName = process.env.BABEL_ENV || ('NODE_ENV' in process.env ? process.env.NODE_ENV : 'test') || 'development';
// Prepare inputs for caching seeds. Compute a seed based on the Node.js
// version and the project directory. Dependency hashes may vary based on the
// Node.js version, e.g. with the @ava/stage-4 Babel preset. Certain plugins
// and presets are provided as absolute paths, which wouldn't necessarily
// be valid if the project directory changes. Also include `envName`, so
// options can be cached even if users change BABEL_ENV or NODE_ENV between
// runs.
const seedInputs = [
process.versions.node,
packageHash.sync(require.resolve('../package.json')),
projectDir,
envName,
concordance.serialize(concordance.describe(userOptions))
];
const partialCacheKey = md5Hex(seedInputs);
const pluginAndPresetHashes = new Map();
const ensureStage4 = wantsStage4(userOptions, projectDir, {experiments});
const containsStage4 = makeValueChecker('../stage-4');
const containsTransformTestFiles = makeValueChecker('@ava/babel-preset-transform-test-files');
const loadOptions = filename => {
const partialTestConfig = babel.loadPartialConfig({
babelrc: false,
babelrcRoots: [projectDir],
test('hashes packages', async t => {
const fromPackage = fixture('compare', 'node_modules', 'plugin')
const filename = path.join(fromPackage, 'index.js')
const hashed = await hashDependencies([
{filename, fromPackage}
])
t.deepEqual(hashed, [await packageHash(path.join(fromPackage, 'package.json'))])
})
test.before(t => {
const promises = []
for (const pkg of [
fixture('compare', 'node_modules', 'env-plugin', 'package.json'),
fixture('compare', 'node_modules', 'plugin', 'package.json'),
fixture('compare', 'node_modules', 'plugin-copy', 'package.json'),
fixture('compare', 'node_modules', 'plugin-default-opts', 'package.json'),
fixture('compare', 'node_modules', 'preset', 'package.json')
]) {
promises.push(
packageHash(pkg)
.then(hash => (hashes[pkg] = hash)))
}
for (const fp of [
fixture('compare', '.babelrc'),
fixture('compare', 'dir', 'subdir', 'extended-by-babelrc.js'),
fixture('compare', 'extended-by-virtual.json5'),
fixture('compare', 'dir', 'extended-by-virtual-foo.json5'),
fixture('compare', 'virtual.json')
]) {
promises.push(
new Promise((resolve, reject) => {
fs.readFile(fp, (err, contents) => err ? reject(err) : resolve(contents))
})
.then(md5Hex)
.then(hash => (hashes[fp] = hash)))
for (const item of [...plugins, ...presets]) {
if (!item.file) {
continue;
}
const {file: {resolved: filename}} = item;
if (pluginAndPresetHashes.has(filename)) {
inputs.push(pluginAndPresetHashes.get(filename));
continue;
}
const [firstComponent] = path.relative(projectDir, filename).split(path.sep);
let hash;
if (firstComponent === 'node_modules') {
hash = packageHash.sync(findUp.sync('package.json', {cwd: path.dirname(filename)}));
} else {
hash = md5Hex(stripBomBuf(fs.readFileSync(filename)));
}
pluginAndPresetHashes.set(filename, hash);
inputs.push(hash);
}
return md5Hex(inputs);
}
_createTransform() {
const salt = packageHash.sync([
require.resolve('../package.json'),
require.resolve('@babel/core/package.json')
], this.babelCacheKeys);
return cachingTransform({
factory: this._init,
cacheDir: this.cacheDirPath,
hash: this._generateHash,
salt,
ext: '.js'
});
}
function getOwnHash() {
ownHash = packageHash.sync(path.join(__dirname, 'package.json'));
return ownHash;
}