"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NodeResolverBase = void 0; function _module() { const data = _interopRequireDefault(require("module")); _module = function () { return data; }; return data; } function _path() { const data = _interopRequireDefault(require("path")); _path = function () { return data; }; return data; } function _assert() { const data = _interopRequireDefault(require("assert")); _assert = function () { return data; }; return data; } function _utils() { const data = require("@parcel/utils"); _utils = function () { return data; }; return data; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // $FlowFixMe const builtins = { pnpapi: true }; for (let builtin of _module().default.builtinModules) { builtins[builtin] = true; } const NODE_MODULES = `${_path().default.sep}node_modules${_path().default.sep}`; class NodeResolverBase { constructor(fs, projectRoot, extensions) { this.fs = fs; this.projectRoot = projectRoot; this.extensions = extensions || // $FlowFixMe[prop-missing] Object.keys(_module().default._extensions); this.packageCache = new Map(); } resolve(id, from) { throw new Error(`Could not resolve "${id}" from "${from}"`); } expandFile(file) { // Expand extensions and aliases let res = []; for (let ext of this.extensions) { res.push(file + ext); } if (_path().default.extname(file)) { res.unshift(file); } else { res.push(file); } return res; } getPackageEntries(dir, pkg) { let main = pkg.main; return [main].filter(entry => typeof entry === 'string').map(main => { // Default to index file if no main field find if (!main || main === '.' || main === './') { main = 'index'; } (0, _assert().default)(typeof main === 'string'); return _path().default.resolve(dir, main); }); } getModuleParts(name) { name = _path().default.normalize(name); let splitOn = name.indexOf(_path().default.sep); if (name.charAt(0) === '@') { splitOn = name.indexOf(_path().default.sep, splitOn + 1); } if (splitOn < 0) { return [(0, _utils().normalizeSeparators)(name), undefined]; } else { return [(0, _utils().normalizeSeparators)(name.substring(0, splitOn)), name.substring(splitOn + 1) || undefined]; } } isBuiltin(name) { return !!(builtins[name] || name.startsWith('node:')); } findNodeModulePath(id, sourceFile, ctx) { if (this.isBuiltin(id)) { return { resolved: id, invalidateOnFileChange: new Set(), invalidateOnFileCreate: [] }; } let [moduleName, subPath] = this.getModuleParts(id); let dir = _path().default.dirname(sourceFile); let moduleDir = this.fs.findNodeModule(moduleName, dir); ctx.invalidateOnFileCreate.push({ fileName: `node_modules/${moduleName}`, aboveFilePath: sourceFile }); if (!moduleDir && process.versions.pnp != null) { try { // $FlowFixMe[prop-missing] let pnp = _module().default.findPnpApi(dir + '/'); moduleDir = pnp.resolveToUnqualified(moduleName + ( // retain slash in `require('assert/')` to force loading builtin from npm id[moduleName.length] === '/' ? '/' : ''), dir + '/'); // Invalidate whenever the .pnp.js file changes. ctx.invalidateOnFileChange.add(pnp.resolveToUnqualified('pnpapi', null)); } catch (e) { if (e.code !== 'MODULE_NOT_FOUND') { throw e; } } } if (moduleDir) { return { moduleName, subPath, moduleDir: moduleDir, filePath: subPath ? _path().default.join(moduleDir, subPath) : moduleDir }; } return null; } getNodeModulesPackagePath(sourceFile) { // If the file is in node_modules, we can find the package.json in the root of the package // by slicing from the start of the string until 1-2 path segments after node_modules. let index = sourceFile.lastIndexOf(NODE_MODULES); if (index >= 0) { index += NODE_MODULES.length; // If a scoped path, add an extra path segment. if (sourceFile[index] === '@') { index = sourceFile.indexOf(_path().default.sep, index) + 1; } index = sourceFile.indexOf(_path().default.sep, index); return _path().default.join(sourceFile.slice(0, index >= 0 ? index : undefined), 'package.json'); } } invalidate(filePath) { // Invalidate the package.jsons above `filePath` let dir = _path().default.dirname(filePath); let { root } = _path().default.parse(dir); while (dir !== root && _path().default.basename(dir) !== 'node_modules') { this.packageCache.delete(_path().default.join(dir, 'package.json')); dir = _path().default.dirname(dir); } } } exports.NodeResolverBase = NodeResolverBase;