parcel-bundler
Version:
<p align="center"> <a href="https://parceljs.org/" target="_blank"> <img alt="Parcel" src="https://user-images.githubusercontent.com/19409/31321658-f6aed0f2-ac3d-11e7-8100-1587e676e0ec.png" width="749"> </a> </p>
56 lines (44 loc) • 1.35 kB
JavaScript
const fs = require('./fs');
const path = require('path');
const json5 = require('json5');
const existsCache = new Map();
async function resolve(filepath, filenames, root = path.parse(filepath).root) {
filepath = path.dirname(filepath);
// Don't traverse above the module root
if (filepath === root || path.basename(filepath) === 'node_modules') {
return null;
}
for (const filename of filenames) {
let file = path.join(filepath, filename);
let exists = existsCache.has(file)
? existsCache.get(file)
: await fs.exists(file);
if (exists) {
existsCache.set(file, true);
return file;
}
existsCache.set(file, false);
}
return resolve(filepath, filenames, root);
}
async function load(filepath, filenames, root = path.parse(filepath).root) {
let configFile = await resolve(filepath, filenames, root);
if (configFile) {
try {
if (path.extname(configFile) === '.js') {
return require(configFile);
}
let configStream = await fs.readFile(configFile);
return json5.parse(configStream.toString());
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND' || err.code === 'ENOENT') {
existsCache.delete(configFile);
return null;
}
throw err;
}
}
return null;
}
exports.resolve = resolve;
exports.load = load;