|
| 1 | +var path = require('path'); |
| 2 | +var startsWith = require('string.prototype.startswith'); |
| 3 | + |
| 4 | +function makeError(code, message) { |
| 5 | + var error = new Error(message); |
| 6 | + error.code = code; |
| 7 | + return error; |
| 8 | +} |
| 9 | + |
| 10 | +function validateExports(exports, basePath) { |
| 11 | + var isConditional = true; |
| 12 | + |
| 13 | + if (typeof exports === 'object' && !Array.isArray(exports)) { |
| 14 | + var exportKeys = Object.keys(exports); |
| 15 | + |
| 16 | + for (var i = 0; i < exportKeys.length; i++) { |
| 17 | + var isKeyConditional = exportKeys[i][0] !== '.'; |
| 18 | + if (i === 0) { |
| 19 | + isConditional = isKeyConditional; |
| 20 | + } else if (isKeyConditional !== isConditional) { |
| 21 | + throw makeError('ERR_INVALID_PACKAGE_CONFIG', 'Invalid package config ' + basePath + path.sep + 'package.json, ' |
| 22 | + + '"exports" cannot contain some keys starting with \'.\' and some not. ' |
| 23 | + + 'The exports object must either be an object of package subpath keys ' |
| 24 | + + 'or an object of main entry condition name keys only.'); |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + if (isConditional) { |
| 30 | + return { '.': exports }; |
| 31 | + } else { |
| 32 | + return exports; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function validateConditions(names) { |
| 37 | + // TODO If exports contains any index property keys, as defined in ECMA-262 6.1.7 Array Index, throw an Invalid Package Configuration error. |
| 38 | + return names; |
| 39 | +} |
| 40 | + |
| 41 | +// eslint-disable-next-line max-lines-per-function |
| 42 | +function resolvePackageTarget(packagePath, parent, key, target, subpath, internal, conditions) { |
| 43 | + if (typeof target === 'string') { |
| 44 | + var resolvedTarget = path.resolve(packagePath, target); |
| 45 | + var invalidTarget = false; |
| 46 | + |
| 47 | + if (!startsWith(target, './')) { |
| 48 | + if (!internal) { |
| 49 | + invalidTarget = true; |
| 50 | + } else if (!startsWith(target, '../') && !startsWith(target, '/')) { |
| 51 | + invalidTarget = true; |
| 52 | + } else { |
| 53 | + // TODO: imports need call package_resolve here |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + var targetParts = target.split(/[\\/]/).slice(1); // slice to strip the leading '.' |
| 58 | + if (invalidTarget || targetParts.indexOf('node_modules') !== -1 || targetParts.indexOf('.') !== -1 || targetParts.indexOf('..') !== -1) { |
| 59 | + throw makeError('ERR_INVALID_PACKAGE_TARGET', 'Invalid "exports" target ' + JSON.stringify(target) |
| 60 | + + ' defined for ' + key + ' in ' + packagePath + path.sep + 'package.json imported from ' + parent + '.'); |
| 61 | + } |
| 62 | + |
| 63 | + if (subpath !== '' && target[target.length - 1] !== '/') { |
| 64 | + throw makeError('ERR_INVALID_MODULE_SPECIFIER', 'Package subpath "' + subpath + '" is not a valid module request for ' |
| 65 | + + 'the "exports" resolution of ' + packagePath + path.sep + 'package.json imported from ' + parent + '.'); |
| 66 | + } |
| 67 | + |
| 68 | + var resolved = path.normalize(resolvedTarget + subpath); |
| 69 | + var subpathParts = subpath.split(/[\\/]/); |
| 70 | + if (!startsWith(resolved, resolvedTarget) || subpathParts.indexOf('node_modules') !== -1 || subpathParts.indexOf('.') !== -1 || subpathParts.indexOf('..') !== -1) { |
| 71 | + throw makeError('ERR_INVALID_MODULE_SPECIFIER', 'Package subpath "' + subpath + '" is not a valid module request for ' |
| 72 | + + 'the "exports" resolution of ' + packagePath + path.sep + 'package.json imported from ' + parent + '.'); |
| 73 | + } |
| 74 | + |
| 75 | + return resolved; |
| 76 | + } |
| 77 | + |
| 78 | + if (Array.isArray(target)) { |
| 79 | + if (target.length === 0) { |
| 80 | + throw makeError('ERR_PACKAGE_PATH_NOT_EXPORTED', key === '.' |
| 81 | + ? 'No "exports" main resolved in ' + packagePath + path.sep + 'package.json.' |
| 82 | + : 'Package subpath ' + key + ' is not defined by "exports" in ' + packagePath + path.sep + 'package.json imported from ' + parent + '.'); |
| 83 | + } |
| 84 | + |
| 85 | + var lastError; |
| 86 | + for (var i = 0; i < target.length; i++) { |
| 87 | + try { |
| 88 | + return resolvePackageTarget(packagePath, parent, key, target[i], subpath, internal, conditions); |
| 89 | + } catch (e) { |
| 90 | + if (e && (e.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED' || e.code === 'ERR_INVALID_PACKAGE_TARGET')) { |
| 91 | + lastError = e; |
| 92 | + } else { |
| 93 | + throw e; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + throw lastError; |
| 98 | + } |
| 99 | + |
| 100 | + if (target === null) { |
| 101 | + throw makeError('ERR_PACKAGE_PATH_NOT_EXPORTED', key === '.' |
| 102 | + ? 'No "exports" main resolved in ' + packagePath + path.sep + 'package.json.' |
| 103 | + : 'Package subpath ' + key + ' is not defined by "exports" in ' + packagePath + path.sep + 'package.json imported from ' + parent + '.'); |
| 104 | + } |
| 105 | + |
| 106 | + if (typeof target !== 'object') { |
| 107 | + throw makeError('ERR_INVALID_PACKAGE_TARGET', 'Invalid "exports" target ' + JSON.stringify(target) |
| 108 | + + ' defined for ' + key + ' in ' + packagePath + path.sep + 'package.json imported from ' + parent + '.'); |
| 109 | + } |
| 110 | + |
| 111 | + var exportedConditions = validateConditions(Object.keys(target)); |
| 112 | + |
| 113 | + for (i = 0; i < exportedConditions.length; i++) { |
| 114 | + var exportedCondition = exportedConditions[i]; |
| 115 | + if (exportedCondition === 'default' || conditions.indexOf(exportedCondition) !== -1) { |
| 116 | + try { |
| 117 | + return resolvePackageTarget( |
| 118 | + packagePath, |
| 119 | + parent, |
| 120 | + key, |
| 121 | + target[exportedCondition], |
| 122 | + subpath, |
| 123 | + internal, |
| 124 | + conditions |
| 125 | + ); |
| 126 | + } catch (e) { |
| 127 | + if (!e || e.code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED') { |
| 128 | + throw e; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + throw makeError('ERR_PACKAGE_PATH_NOT_EXPORTED', key === '.' |
| 135 | + ? 'No "exports" main resolved in ' + packagePath + path.sep + 'package.json.' |
| 136 | + : 'Package subpath ' + key + ' is not defined by "exports" in ' + packagePath + path.sep + 'package.json imported from ' + parent + '.'); |
| 137 | +} |
| 138 | + |
| 139 | +function resolveImportExport(packagePath, parent, matchObj, matchKey, isImports, conditions) { |
| 140 | + if (Object.prototype.hasOwnProperty.call(matchObj, matchKey) && matchKey[matchKey.length - 1] !== '*') { |
| 141 | + return { |
| 142 | + resolved: resolvePackageTarget(packagePath, parent, matchKey, matchObj[matchKey], '', isImports, conditions), |
| 143 | + exact: true |
| 144 | + }; |
| 145 | + } |
| 146 | + |
| 147 | + var longestMatchingExport = ''; |
| 148 | + var exportedPaths = Object.keys(matchObj); |
| 149 | + |
| 150 | + for (var i = 0; i < exportedPaths.length; i++) { |
| 151 | + var exportedPath = exportedPaths[i]; |
| 152 | + if (exportedPath[exportedPath.length - 1] === '/' && startsWith(matchKey, exportedPath) && exportedPath.length > longestMatchingExport.length) { |
| 153 | + longestMatchingExport = exportedPath; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + if (longestMatchingExport === '') { |
| 158 | + throw makeError('ERR_PACKAGE_PATH_NOT_EXPORTED', 'Package subpath ' + matchKey + ' is not defined by "exports" in ' |
| 159 | + + packagePath + path.sep + 'package.json imported from ' + parent + '.'); |
| 160 | + } |
| 161 | + |
| 162 | + return { |
| 163 | + resolved: resolvePackageTarget( |
| 164 | + packagePath, |
| 165 | + parent, |
| 166 | + longestMatchingExport, |
| 167 | + matchObj[longestMatchingExport], |
| 168 | + matchKey.substring(longestMatchingExport.length - 1), |
| 169 | + isImports, |
| 170 | + conditions |
| 171 | + ), |
| 172 | + exact: false |
| 173 | + }; |
| 174 | +} |
| 175 | + |
| 176 | +module.exports = function resolveExports(packagePath, parent, subpath, exports, conditions) { |
| 177 | + return resolveImportExport( |
| 178 | + packagePath, |
| 179 | + parent, |
| 180 | + validateExports(exports, packagePath), |
| 181 | + '.' + subpath, |
| 182 | + false, |
| 183 | + conditions |
| 184 | + ); |
| 185 | +}; |
0 commit comments