Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async readPackage(dir) {
let file = path.join(dir, 'package.json');
if (this.packageCache.has(file)) {
return this.packageCache.get(file);
}
let json = await fs.readFile(file, 'utf8');
let pkg = JSON.parse(json);
pkg.pkgfile = file;
pkg.pkgdir = dir;
// If the package has a `source` field, check if it is behind a symlink.
// If so, we treat the module as source code rather than a pre-compiled module.
if (pkg.source) {
let realpath = await fs.realpath(file);
if (realpath === file) {
delete pkg.source;
}
}
this.packageCache.set(file, pkg);
return pkg;
}
async readPackage(dir) {
let file = path.join(dir, 'package.json');
if (this.packageCache.has(file)) {
return this.packageCache.get(file);
}
let json = await fs.readFile(file, 'utf8');
let pkg = JSON.parse(json);
pkg.pkgfile = file;
pkg.pkgdir = dir;
// If the package has a `source` field, check if it is behind a symlink.
// If so, we treat the module as source code rather than a pre-compiled module.
if (pkg.source) {
let realpath = await fs.realpath(file);
if (realpath === file) {
delete pkg.source;
}
}
this.packageCache.set(file, pkg);
return pkg;
}
let cached = this.packageCache.get(file);
if (cached) {
return cached;
}
let json = await fs.readFile(file, 'utf8');
let pkg = JSON.parse(json);
pkg.pkgfile = file;
pkg.pkgdir = dir;
// If the package has a `source` field, check if it is behind a symlink.
// If so, we treat the module as source code rather than a pre-compiled module.
if (pkg.source) {
let realpath = await fs.realpath(file);
if (realpath === file) {
delete pkg.source;
}
}
this.packageCache.set(file, pkg);
return pkg;
}
async function getBabelConfig(asset) {
// Consider the module source code rather than precompiled if the resolver
// used the `source` field, or it is not in node_modules.
let pkg = await asset.getPackage();
let isSource =
!!(pkg && pkg.source && (await fs.realpath(asset.name)) !== asset.name) ||
!asset.name.includes(NODE_MODULES);
// Try to resolve a .babelrc file. If one is found, consider the module source code.
let babelrc = await getBabelRc(asset, isSource);
isSource = isSource || !!babelrc;
let envConfig = await getEnvConfig(asset, isSource);
let jsxConfig = await getJSXConfig(asset, isSource);
let flowConfig = getFlowConfig(asset, isSource);
if (babelrc && envConfig) {
// Filter out presets that are already applied by @babel/preset-env
if (Array.isArray(babelrc.config.presets)) {
babelrc.config.presets = babelrc.config.presets.filter(preset => {
return !ENV_PRESETS[getPluginName(preset)];
});
async unwatch(path, asset) {
path = await fs.realpath(path);
if (!this.watchedAssets.has(path)) {
return;
}
let watched = this.watchedAssets.get(path);
watched.delete(asset);
if (watched.size === 0) {
this.watchedAssets.delete(path);
this.watcher.unwatch(path);
}
}
async watch(path, asset) {
if (!this.watcher) {
return;
}
path = await fs.realpath(path);
if (!this.watchedAssets.has(path)) {
this.watcher.watch(path);
this.watchedAssets.set(path, new Set());
}
this.watchedAssets.get(path).add(asset);
}
async watch(path, asset) {
if (!this.watcher) {
return;
}
path = await fs.realpath(path);
if (!this.watchedAssets.has(path)) {
this.watcher.watch(path);
this.watchedAssets.set(path, new Set());
}
this.watchedAssets.get(path).add(asset);
}