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