| [9af201e] | 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, '__esModule', { value: true });
|
|---|
| 4 |
|
|---|
| 5 | var path = require('path');
|
|---|
| 6 | var builtinList = require('builtin-modules');
|
|---|
| 7 | var deepMerge = require('deepmerge');
|
|---|
| 8 | var isModule = require('is-module');
|
|---|
| 9 | var fs = require('fs');
|
|---|
| 10 | var util = require('util');
|
|---|
| 11 | var url = require('url');
|
|---|
| 12 | var resolve = require('resolve');
|
|---|
| 13 | var pluginutils = require('@rollup/pluginutils');
|
|---|
| 14 |
|
|---|
| 15 | function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|---|
| 16 |
|
|---|
| 17 | var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|---|
| 18 | var builtinList__default = /*#__PURE__*/_interopDefaultLegacy(builtinList);
|
|---|
| 19 | var deepMerge__default = /*#__PURE__*/_interopDefaultLegacy(deepMerge);
|
|---|
| 20 | var isModule__default = /*#__PURE__*/_interopDefaultLegacy(isModule);
|
|---|
| 21 | var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|---|
| 22 | var resolve__default = /*#__PURE__*/_interopDefaultLegacy(resolve);
|
|---|
| 23 |
|
|---|
| 24 | const access = util.promisify(fs__default['default'].access);
|
|---|
| 25 | const readFile = util.promisify(fs__default['default'].readFile);
|
|---|
| 26 | const realpath = util.promisify(fs__default['default'].realpath);
|
|---|
| 27 | const stat = util.promisify(fs__default['default'].stat);
|
|---|
| 28 | async function exists(filePath) {
|
|---|
| 29 | try {
|
|---|
| 30 | await access(filePath);
|
|---|
| 31 | return true;
|
|---|
| 32 | } catch {
|
|---|
| 33 | return false;
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | const onError = (error) => {
|
|---|
| 38 | if (error.code === 'ENOENT') {
|
|---|
| 39 | return false;
|
|---|
| 40 | }
|
|---|
| 41 | throw error;
|
|---|
| 42 | };
|
|---|
| 43 |
|
|---|
| 44 | const makeCache = (fn) => {
|
|---|
| 45 | const cache = new Map();
|
|---|
| 46 | const wrapped = async (param, done) => {
|
|---|
| 47 | if (cache.has(param) === false) {
|
|---|
| 48 | cache.set(
|
|---|
| 49 | param,
|
|---|
| 50 | fn(param).catch((err) => {
|
|---|
| 51 | cache.delete(param);
|
|---|
| 52 | throw err;
|
|---|
| 53 | })
|
|---|
| 54 | );
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | try {
|
|---|
| 58 | const result = cache.get(param);
|
|---|
| 59 | const value = await result;
|
|---|
| 60 | return done(null, value);
|
|---|
| 61 | } catch (error) {
|
|---|
| 62 | return done(error);
|
|---|
| 63 | }
|
|---|
| 64 | };
|
|---|
| 65 |
|
|---|
| 66 | wrapped.clear = () => cache.clear();
|
|---|
| 67 |
|
|---|
| 68 | return wrapped;
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | const isDirCached = makeCache(async (file) => {
|
|---|
| 72 | try {
|
|---|
| 73 | const stats = await stat(file);
|
|---|
| 74 | return stats.isDirectory();
|
|---|
| 75 | } catch (error) {
|
|---|
| 76 | return onError(error);
|
|---|
| 77 | }
|
|---|
| 78 | });
|
|---|
| 79 |
|
|---|
| 80 | const isFileCached = makeCache(async (file) => {
|
|---|
| 81 | try {
|
|---|
| 82 | const stats = await stat(file);
|
|---|
| 83 | return stats.isFile();
|
|---|
| 84 | } catch (error) {
|
|---|
| 85 | return onError(error);
|
|---|
| 86 | }
|
|---|
| 87 | });
|
|---|
| 88 |
|
|---|
| 89 | const readCachedFile = makeCache(readFile);
|
|---|
| 90 |
|
|---|
| 91 | // returns the imported package name for bare module imports
|
|---|
| 92 | function getPackageName(id) {
|
|---|
| 93 | if (id.startsWith('.') || id.startsWith('/')) {
|
|---|
| 94 | return null;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | const split = id.split('/');
|
|---|
| 98 |
|
|---|
| 99 | // @my-scope/my-package/foo.js -> @my-scope/my-package
|
|---|
| 100 | // @my-scope/my-package -> @my-scope/my-package
|
|---|
| 101 | if (split[0][0] === '@') {
|
|---|
| 102 | return `${split[0]}/${split[1]}`;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | // my-package/foo.js -> my-package
|
|---|
| 106 | // my-package -> my-package
|
|---|
| 107 | return split[0];
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | function getMainFields(options) {
|
|---|
| 111 | let mainFields;
|
|---|
| 112 | if (options.mainFields) {
|
|---|
| 113 | ({ mainFields } = options);
|
|---|
| 114 | } else {
|
|---|
| 115 | mainFields = ['module', 'main'];
|
|---|
| 116 | }
|
|---|
| 117 | if (options.browser && mainFields.indexOf('browser') === -1) {
|
|---|
| 118 | return ['browser'].concat(mainFields);
|
|---|
| 119 | }
|
|---|
| 120 | if (!mainFields.length) {
|
|---|
| 121 | throw new Error('Please ensure at least one `mainFields` value is specified');
|
|---|
| 122 | }
|
|---|
| 123 | return mainFields;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | function getPackageInfo(options) {
|
|---|
| 127 | const {
|
|---|
| 128 | cache,
|
|---|
| 129 | extensions,
|
|---|
| 130 | pkg,
|
|---|
| 131 | mainFields,
|
|---|
| 132 | preserveSymlinks,
|
|---|
| 133 | useBrowserOverrides,
|
|---|
| 134 | rootDir,
|
|---|
| 135 | ignoreSideEffectsForRoot
|
|---|
| 136 | } = options;
|
|---|
| 137 | let { pkgPath } = options;
|
|---|
| 138 |
|
|---|
| 139 | if (cache.has(pkgPath)) {
|
|---|
| 140 | return cache.get(pkgPath);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | // browserify/resolve doesn't realpath paths returned in its packageFilter callback
|
|---|
| 144 | if (!preserveSymlinks) {
|
|---|
| 145 | pkgPath = fs.realpathSync(pkgPath);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | const pkgRoot = path.dirname(pkgPath);
|
|---|
| 149 |
|
|---|
| 150 | const packageInfo = {
|
|---|
| 151 | // copy as we are about to munge the `main` field of `pkg`.
|
|---|
| 152 | packageJson: { ...pkg },
|
|---|
| 153 |
|
|---|
| 154 | // path to package.json file
|
|---|
| 155 | packageJsonPath: pkgPath,
|
|---|
| 156 |
|
|---|
| 157 | // directory containing the package.json
|
|---|
| 158 | root: pkgRoot,
|
|---|
| 159 |
|
|---|
| 160 | // which main field was used during resolution of this module (main, module, or browser)
|
|---|
| 161 | resolvedMainField: 'main',
|
|---|
| 162 |
|
|---|
| 163 | // whether the browser map was used to resolve the entry point to this module
|
|---|
| 164 | browserMappedMain: false,
|
|---|
| 165 |
|
|---|
| 166 | // the entry point of the module with respect to the selected main field and any
|
|---|
| 167 | // relevant browser mappings.
|
|---|
| 168 | resolvedEntryPoint: ''
|
|---|
| 169 | };
|
|---|
| 170 |
|
|---|
| 171 | let overriddenMain = false;
|
|---|
| 172 | for (let i = 0; i < mainFields.length; i++) {
|
|---|
| 173 | const field = mainFields[i];
|
|---|
| 174 | if (typeof pkg[field] === 'string') {
|
|---|
| 175 | pkg.main = pkg[field];
|
|---|
| 176 | packageInfo.resolvedMainField = field;
|
|---|
| 177 | overriddenMain = true;
|
|---|
| 178 | break;
|
|---|
| 179 | }
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | const internalPackageInfo = {
|
|---|
| 183 | cachedPkg: pkg,
|
|---|
| 184 | hasModuleSideEffects: () => null,
|
|---|
| 185 | hasPackageEntry: overriddenMain !== false || mainFields.indexOf('main') !== -1,
|
|---|
| 186 | packageBrowserField:
|
|---|
| 187 | useBrowserOverrides &&
|
|---|
| 188 | typeof pkg.browser === 'object' &&
|
|---|
| 189 | Object.keys(pkg.browser).reduce((browser, key) => {
|
|---|
| 190 | let resolved = pkg.browser[key];
|
|---|
| 191 | if (resolved && resolved[0] === '.') {
|
|---|
| 192 | resolved = path.resolve(pkgRoot, resolved);
|
|---|
| 193 | }
|
|---|
| 194 | /* eslint-disable no-param-reassign */
|
|---|
| 195 | browser[key] = resolved;
|
|---|
| 196 | if (key[0] === '.') {
|
|---|
| 197 | const absoluteKey = path.resolve(pkgRoot, key);
|
|---|
| 198 | browser[absoluteKey] = resolved;
|
|---|
| 199 | if (!path.extname(key)) {
|
|---|
| 200 | extensions.reduce((subBrowser, ext) => {
|
|---|
| 201 | subBrowser[absoluteKey + ext] = subBrowser[key];
|
|---|
| 202 | return subBrowser;
|
|---|
| 203 | }, browser);
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 | return browser;
|
|---|
| 207 | }, {}),
|
|---|
| 208 | packageInfo
|
|---|
| 209 | };
|
|---|
| 210 |
|
|---|
| 211 | const browserMap = internalPackageInfo.packageBrowserField;
|
|---|
| 212 | if (
|
|---|
| 213 | useBrowserOverrides &&
|
|---|
| 214 | typeof pkg.browser === 'object' &&
|
|---|
| 215 | // eslint-disable-next-line no-prototype-builtins
|
|---|
| 216 | browserMap.hasOwnProperty(pkg.main)
|
|---|
| 217 | ) {
|
|---|
| 218 | packageInfo.resolvedEntryPoint = browserMap[pkg.main];
|
|---|
| 219 | packageInfo.browserMappedMain = true;
|
|---|
| 220 | } else {
|
|---|
| 221 | // index.node is technically a valid default entrypoint as well...
|
|---|
| 222 | packageInfo.resolvedEntryPoint = path.resolve(pkgRoot, pkg.main || 'index.js');
|
|---|
| 223 | packageInfo.browserMappedMain = false;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | if (!ignoreSideEffectsForRoot || rootDir !== pkgRoot) {
|
|---|
| 227 | const packageSideEffects = pkg.sideEffects;
|
|---|
| 228 | if (typeof packageSideEffects === 'boolean') {
|
|---|
| 229 | internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
|
|---|
| 230 | } else if (Array.isArray(packageSideEffects)) {
|
|---|
| 231 | internalPackageInfo.hasModuleSideEffects = pluginutils.createFilter(packageSideEffects, null, {
|
|---|
| 232 | resolve: pkgRoot
|
|---|
| 233 | });
|
|---|
| 234 | }
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | cache.set(pkgPath, internalPackageInfo);
|
|---|
| 238 | return internalPackageInfo;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | function normalizeInput(input) {
|
|---|
| 242 | if (Array.isArray(input)) {
|
|---|
| 243 | return input;
|
|---|
| 244 | } else if (typeof input === 'object') {
|
|---|
| 245 | return Object.values(input);
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | // otherwise it's a string
|
|---|
| 249 | return [input];
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | /* eslint-disable no-await-in-loop */
|
|---|
| 253 |
|
|---|
| 254 | const fileExists = util.promisify(fs__default['default'].exists);
|
|---|
| 255 |
|
|---|
| 256 | function isModuleDir(current, moduleDirs) {
|
|---|
| 257 | return moduleDirs.some((dir) => current.endsWith(dir));
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | async function findPackageJson(base, moduleDirs) {
|
|---|
| 261 | const { root } = path__default['default'].parse(base);
|
|---|
| 262 | let current = base;
|
|---|
| 263 |
|
|---|
| 264 | while (current !== root && !isModuleDir(current, moduleDirs)) {
|
|---|
| 265 | const pkgJsonPath = path__default['default'].join(current, 'package.json');
|
|---|
| 266 | if (await fileExists(pkgJsonPath)) {
|
|---|
| 267 | const pkgJsonString = fs__default['default'].readFileSync(pkgJsonPath, 'utf-8');
|
|---|
| 268 | return { pkgJson: JSON.parse(pkgJsonString), pkgPath: current, pkgJsonPath };
|
|---|
| 269 | }
|
|---|
| 270 | current = path__default['default'].resolve(current, '..');
|
|---|
| 271 | }
|
|---|
| 272 | return null;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | function isUrl(str) {
|
|---|
| 276 | try {
|
|---|
| 277 | return !!new URL(str);
|
|---|
| 278 | } catch (_) {
|
|---|
| 279 | return false;
|
|---|
| 280 | }
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | function isConditions(exports) {
|
|---|
| 284 | return typeof exports === 'object' && Object.keys(exports).every((k) => !k.startsWith('.'));
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | function isMappings(exports) {
|
|---|
| 288 | return typeof exports === 'object' && !isConditions(exports);
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | function isMixedExports(exports) {
|
|---|
| 292 | const keys = Object.keys(exports);
|
|---|
| 293 | return keys.some((k) => k.startsWith('.')) && keys.some((k) => !k.startsWith('.'));
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | function createBaseErrorMsg(importSpecifier, importer) {
|
|---|
| 297 | return `Could not resolve import "${importSpecifier}" in ${importer}`;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | function createErrorMsg(context, reason, internal) {
|
|---|
| 301 | const { importSpecifier, importer, pkgJsonPath } = context;
|
|---|
| 302 | const base = createBaseErrorMsg(importSpecifier, importer);
|
|---|
| 303 | const field = internal ? 'imports' : 'exports';
|
|---|
| 304 | return `${base} using ${field} defined in ${pkgJsonPath}.${reason ? ` ${reason}` : ''}`;
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | class ResolveError extends Error {}
|
|---|
| 308 |
|
|---|
| 309 | class InvalidConfigurationError extends ResolveError {
|
|---|
| 310 | constructor(context, reason) {
|
|---|
| 311 | super(createErrorMsg(context, `Invalid "exports" field. ${reason}`));
|
|---|
| 312 | }
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | class InvalidModuleSpecifierError extends ResolveError {
|
|---|
| 316 | constructor(context, internal) {
|
|---|
| 317 | super(createErrorMsg(context, internal));
|
|---|
| 318 | }
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | class InvalidPackageTargetError extends ResolveError {
|
|---|
| 322 | constructor(context, reason) {
|
|---|
| 323 | super(createErrorMsg(context, reason));
|
|---|
| 324 | }
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | /* eslint-disable no-await-in-loop, no-undefined */
|
|---|
| 328 |
|
|---|
| 329 | function includesInvalidSegments(pathSegments, moduleDirs) {
|
|---|
| 330 | return pathSegments
|
|---|
| 331 | .split('/')
|
|---|
| 332 | .slice(1)
|
|---|
| 333 | .some((t) => ['.', '..', ...moduleDirs].includes(t));
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | async function resolvePackageTarget(context, { target, subpath, pattern, internal }) {
|
|---|
| 337 | if (typeof target === 'string') {
|
|---|
| 338 | if (!pattern && subpath.length > 0 && !target.endsWith('/')) {
|
|---|
| 339 | throw new InvalidModuleSpecifierError(context);
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | if (!target.startsWith('./')) {
|
|---|
| 343 | if (internal && !['/', '../'].some((p) => target.startsWith(p)) && !isUrl(target)) {
|
|---|
| 344 | // this is a bare package import, remap it and resolve it using regular node resolve
|
|---|
| 345 | if (pattern) {
|
|---|
| 346 | const result = await context.resolveId(
|
|---|
| 347 | target.replace(/\*/g, subpath),
|
|---|
| 348 | context.pkgURL.href
|
|---|
| 349 | );
|
|---|
| 350 | return result ? url.pathToFileURL(result.location) : null;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | const result = await context.resolveId(`${target}${subpath}`, context.pkgURL.href);
|
|---|
| 354 | return result ? url.pathToFileURL(result.location) : null;
|
|---|
| 355 | }
|
|---|
| 356 | throw new InvalidPackageTargetError(context, `Invalid mapping: "${target}".`);
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | if (includesInvalidSegments(target, context.moduleDirs)) {
|
|---|
| 360 | throw new InvalidPackageTargetError(context, `Invalid mapping: "${target}".`);
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | const resolvedTarget = new URL(target, context.pkgURL);
|
|---|
| 364 | if (!resolvedTarget.href.startsWith(context.pkgURL.href)) {
|
|---|
| 365 | throw new InvalidPackageTargetError(
|
|---|
| 366 | context,
|
|---|
| 367 | `Resolved to ${resolvedTarget.href} which is outside package ${context.pkgURL.href}`
|
|---|
| 368 | );
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | if (includesInvalidSegments(subpath, context.moduleDirs)) {
|
|---|
| 372 | throw new InvalidModuleSpecifierError(context);
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | if (pattern) {
|
|---|
| 376 | return resolvedTarget.href.replace(/\*/g, subpath);
|
|---|
| 377 | }
|
|---|
| 378 | return new URL(subpath, resolvedTarget).href;
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | if (Array.isArray(target)) {
|
|---|
| 382 | let lastError;
|
|---|
| 383 | for (const item of target) {
|
|---|
| 384 | try {
|
|---|
| 385 | const resolved = await resolvePackageTarget(context, {
|
|---|
| 386 | target: item,
|
|---|
| 387 | subpath,
|
|---|
| 388 | pattern,
|
|---|
| 389 | internal
|
|---|
| 390 | });
|
|---|
| 391 |
|
|---|
| 392 | // return if defined or null, but not undefined
|
|---|
| 393 | if (resolved !== undefined) {
|
|---|
| 394 | return resolved;
|
|---|
| 395 | }
|
|---|
| 396 | } catch (error) {
|
|---|
| 397 | if (!(error instanceof InvalidPackageTargetError)) {
|
|---|
| 398 | throw error;
|
|---|
| 399 | } else {
|
|---|
| 400 | lastError = error;
|
|---|
| 401 | }
|
|---|
| 402 | }
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | if (lastError) {
|
|---|
| 406 | throw lastError;
|
|---|
| 407 | }
|
|---|
| 408 | return null;
|
|---|
| 409 | }
|
|---|
| 410 |
|
|---|
| 411 | if (target && typeof target === 'object') {
|
|---|
| 412 | for (const [key, value] of Object.entries(target)) {
|
|---|
| 413 | if (key === 'default' || context.conditions.includes(key)) {
|
|---|
| 414 | const resolved = await resolvePackageTarget(context, {
|
|---|
| 415 | target: value,
|
|---|
| 416 | subpath,
|
|---|
| 417 | pattern,
|
|---|
| 418 | internal
|
|---|
| 419 | });
|
|---|
| 420 |
|
|---|
| 421 | // return if defined or null, but not undefined
|
|---|
| 422 | if (resolved !== undefined) {
|
|---|
| 423 | return resolved;
|
|---|
| 424 | }
|
|---|
| 425 | }
|
|---|
| 426 | }
|
|---|
| 427 | return undefined;
|
|---|
| 428 | }
|
|---|
| 429 |
|
|---|
| 430 | if (target === null) {
|
|---|
| 431 | return null;
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | throw new InvalidPackageTargetError(context, `Invalid exports field.`);
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | /* eslint-disable no-await-in-loop */
|
|---|
| 438 |
|
|---|
| 439 | async function resolvePackageImportsExports(context, { matchKey, matchObj, internal }) {
|
|---|
| 440 | if (!matchKey.endsWith('*') && matchKey in matchObj) {
|
|---|
| 441 | const target = matchObj[matchKey];
|
|---|
| 442 | const resolved = await resolvePackageTarget(context, { target, subpath: '', internal });
|
|---|
| 443 | return resolved;
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | const expansionKeys = Object.keys(matchObj)
|
|---|
| 447 | .filter((k) => k.endsWith('/') || k.endsWith('*'))
|
|---|
| 448 | .sort((a, b) => b.length - a.length);
|
|---|
| 449 |
|
|---|
| 450 | for (const expansionKey of expansionKeys) {
|
|---|
| 451 | const prefix = expansionKey.substring(0, expansionKey.length - 1);
|
|---|
| 452 |
|
|---|
| 453 | if (expansionKey.endsWith('*') && matchKey.startsWith(prefix)) {
|
|---|
| 454 | const target = matchObj[expansionKey];
|
|---|
| 455 | const subpath = matchKey.substring(expansionKey.length - 1);
|
|---|
| 456 | const resolved = await resolvePackageTarget(context, {
|
|---|
| 457 | target,
|
|---|
| 458 | subpath,
|
|---|
| 459 | pattern: true,
|
|---|
| 460 | internal
|
|---|
| 461 | });
|
|---|
| 462 | return resolved;
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|
| 465 | if (matchKey.startsWith(expansionKey)) {
|
|---|
| 466 | const target = matchObj[expansionKey];
|
|---|
| 467 | const subpath = matchKey.substring(expansionKey.length);
|
|---|
| 468 |
|
|---|
| 469 | const resolved = await resolvePackageTarget(context, { target, subpath, internal });
|
|---|
| 470 | return resolved;
|
|---|
| 471 | }
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | throw new InvalidModuleSpecifierError(context, internal);
|
|---|
| 475 | }
|
|---|
| 476 |
|
|---|
| 477 | async function resolvePackageExports(context, subpath, exports) {
|
|---|
| 478 | if (isMixedExports(exports)) {
|
|---|
| 479 | throw new InvalidConfigurationError(
|
|---|
| 480 | context,
|
|---|
| 481 | 'All keys must either start with ./, or without one.'
|
|---|
| 482 | );
|
|---|
| 483 | }
|
|---|
| 484 |
|
|---|
| 485 | if (subpath === '.') {
|
|---|
| 486 | let mainExport;
|
|---|
| 487 | // If exports is a String or Array, or an Object containing no keys starting with ".", then
|
|---|
| 488 | if (typeof exports === 'string' || Array.isArray(exports) || isConditions(exports)) {
|
|---|
| 489 | mainExport = exports;
|
|---|
| 490 | } else if (isMappings(exports)) {
|
|---|
| 491 | mainExport = exports['.'];
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | if (mainExport) {
|
|---|
| 495 | const resolved = await resolvePackageTarget(context, { target: mainExport, subpath: '' });
|
|---|
| 496 | if (resolved) {
|
|---|
| 497 | return resolved;
|
|---|
| 498 | }
|
|---|
| 499 | }
|
|---|
| 500 | } else if (isMappings(exports)) {
|
|---|
| 501 | const resolvedMatch = await resolvePackageImportsExports(context, {
|
|---|
| 502 | matchKey: subpath,
|
|---|
| 503 | matchObj: exports
|
|---|
| 504 | });
|
|---|
| 505 |
|
|---|
| 506 | if (resolvedMatch) {
|
|---|
| 507 | return resolvedMatch;
|
|---|
| 508 | }
|
|---|
| 509 | }
|
|---|
| 510 |
|
|---|
| 511 | throw new InvalidModuleSpecifierError(context);
|
|---|
| 512 | }
|
|---|
| 513 |
|
|---|
| 514 | async function resolvePackageImports({
|
|---|
| 515 | importSpecifier,
|
|---|
| 516 | importer,
|
|---|
| 517 | moduleDirs,
|
|---|
| 518 | conditions,
|
|---|
| 519 | resolveId
|
|---|
| 520 | }) {
|
|---|
| 521 | const result = await findPackageJson(importer, moduleDirs);
|
|---|
| 522 | if (!result) {
|
|---|
| 523 | throw new Error(createBaseErrorMsg('. Could not find a parent package.json.'));
|
|---|
| 524 | }
|
|---|
| 525 |
|
|---|
| 526 | const { pkgPath, pkgJsonPath, pkgJson } = result;
|
|---|
| 527 | const pkgURL = url.pathToFileURL(`${pkgPath}/`);
|
|---|
| 528 | const context = {
|
|---|
| 529 | importer,
|
|---|
| 530 | importSpecifier,
|
|---|
| 531 | moduleDirs,
|
|---|
| 532 | pkgURL,
|
|---|
| 533 | pkgJsonPath,
|
|---|
| 534 | conditions,
|
|---|
| 535 | resolveId
|
|---|
| 536 | };
|
|---|
| 537 |
|
|---|
| 538 | const { imports } = pkgJson;
|
|---|
| 539 | if (!imports) {
|
|---|
| 540 | throw new InvalidModuleSpecifierError(context, true);
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | if (importSpecifier === '#' || importSpecifier.startsWith('#/')) {
|
|---|
| 544 | throw new InvalidModuleSpecifierError(context, 'Invalid import specifier.');
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | return resolvePackageImportsExports(context, {
|
|---|
| 548 | matchKey: importSpecifier,
|
|---|
| 549 | matchObj: imports,
|
|---|
| 550 | internal: true
|
|---|
| 551 | });
|
|---|
| 552 | }
|
|---|
| 553 |
|
|---|
| 554 | const resolveImportPath = util.promisify(resolve__default['default']);
|
|---|
| 555 | const readFile$1 = util.promisify(fs__default['default'].readFile);
|
|---|
| 556 |
|
|---|
| 557 | async function getPackageJson(importer, pkgName, resolveOptions, moduleDirectories) {
|
|---|
| 558 | if (importer) {
|
|---|
| 559 | const selfPackageJsonResult = await findPackageJson(importer, moduleDirectories);
|
|---|
| 560 | if (selfPackageJsonResult && selfPackageJsonResult.pkgJson.name === pkgName) {
|
|---|
| 561 | // the referenced package name is the current package
|
|---|
| 562 | return selfPackageJsonResult;
|
|---|
| 563 | }
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | try {
|
|---|
| 567 | const pkgJsonPath = await resolveImportPath(`${pkgName}/package.json`, resolveOptions);
|
|---|
| 568 | const pkgJson = JSON.parse(await readFile$1(pkgJsonPath, 'utf-8'));
|
|---|
| 569 | return { pkgJsonPath, pkgJson };
|
|---|
| 570 | } catch (_) {
|
|---|
| 571 | return null;
|
|---|
| 572 | }
|
|---|
| 573 | }
|
|---|
| 574 |
|
|---|
| 575 | async function resolveId({
|
|---|
| 576 | importer,
|
|---|
| 577 | importSpecifier,
|
|---|
| 578 | exportConditions,
|
|---|
| 579 | warn,
|
|---|
| 580 | packageInfoCache,
|
|---|
| 581 | extensions,
|
|---|
| 582 | mainFields,
|
|---|
| 583 | preserveSymlinks,
|
|---|
| 584 | useBrowserOverrides,
|
|---|
| 585 | baseDir,
|
|---|
| 586 | moduleDirectories,
|
|---|
| 587 | rootDir,
|
|---|
| 588 | ignoreSideEffectsForRoot
|
|---|
| 589 | }) {
|
|---|
| 590 | let hasModuleSideEffects = () => null;
|
|---|
| 591 | let hasPackageEntry = true;
|
|---|
| 592 | let packageBrowserField = false;
|
|---|
| 593 | let packageInfo;
|
|---|
| 594 |
|
|---|
| 595 | const filter = (pkg, pkgPath) => {
|
|---|
| 596 | const info = getPackageInfo({
|
|---|
| 597 | cache: packageInfoCache,
|
|---|
| 598 | extensions,
|
|---|
| 599 | pkg,
|
|---|
| 600 | pkgPath,
|
|---|
| 601 | mainFields,
|
|---|
| 602 | preserveSymlinks,
|
|---|
| 603 | useBrowserOverrides,
|
|---|
| 604 | rootDir,
|
|---|
| 605 | ignoreSideEffectsForRoot
|
|---|
| 606 | });
|
|---|
| 607 |
|
|---|
| 608 | ({ packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = info);
|
|---|
| 609 |
|
|---|
| 610 | return info.cachedPkg;
|
|---|
| 611 | };
|
|---|
| 612 |
|
|---|
| 613 | const resolveOptions = {
|
|---|
| 614 | basedir: baseDir,
|
|---|
| 615 | readFile: readCachedFile,
|
|---|
| 616 | isFile: isFileCached,
|
|---|
| 617 | isDirectory: isDirCached,
|
|---|
| 618 | extensions,
|
|---|
| 619 | includeCoreModules: false,
|
|---|
| 620 | moduleDirectory: moduleDirectories,
|
|---|
| 621 | preserveSymlinks,
|
|---|
| 622 | packageFilter: filter
|
|---|
| 623 | };
|
|---|
| 624 |
|
|---|
| 625 | let location;
|
|---|
| 626 |
|
|---|
| 627 | const pkgName = getPackageName(importSpecifier);
|
|---|
| 628 | if (importSpecifier.startsWith('#')) {
|
|---|
| 629 | // this is a package internal import, resolve using package imports field
|
|---|
| 630 | const resolveResult = await resolvePackageImports({
|
|---|
| 631 | importSpecifier,
|
|---|
| 632 | importer,
|
|---|
| 633 | moduleDirs: moduleDirectories,
|
|---|
| 634 | conditions: exportConditions,
|
|---|
| 635 | resolveId(id, parent) {
|
|---|
| 636 | return resolveId({
|
|---|
| 637 | importSpecifier: id,
|
|---|
| 638 | importer: parent,
|
|---|
| 639 | exportConditions,
|
|---|
| 640 | warn,
|
|---|
| 641 | packageInfoCache,
|
|---|
| 642 | extensions,
|
|---|
| 643 | mainFields,
|
|---|
| 644 | preserveSymlinks,
|
|---|
| 645 | useBrowserOverrides,
|
|---|
| 646 | baseDir,
|
|---|
| 647 | moduleDirectories
|
|---|
| 648 | });
|
|---|
| 649 | }
|
|---|
| 650 | });
|
|---|
| 651 | location = url.fileURLToPath(resolveResult);
|
|---|
| 652 | } else if (pkgName) {
|
|---|
| 653 | // it's a bare import, find the package.json and resolve using package exports if available
|
|---|
| 654 | const result = await getPackageJson(importer, pkgName, resolveOptions, moduleDirectories);
|
|---|
| 655 |
|
|---|
| 656 | if (result && result.pkgJson.exports) {
|
|---|
| 657 | const { pkgJson, pkgJsonPath } = result;
|
|---|
| 658 | try {
|
|---|
| 659 | const subpath =
|
|---|
| 660 | pkgName === importSpecifier ? '.' : `.${importSpecifier.substring(pkgName.length)}`;
|
|---|
| 661 | const pkgDr = pkgJsonPath.replace('package.json', '');
|
|---|
| 662 | const pkgURL = url.pathToFileURL(pkgDr);
|
|---|
| 663 |
|
|---|
| 664 | const context = {
|
|---|
| 665 | importer,
|
|---|
| 666 | importSpecifier,
|
|---|
| 667 | moduleDirs: moduleDirectories,
|
|---|
| 668 | pkgURL,
|
|---|
| 669 | pkgJsonPath,
|
|---|
| 670 | conditions: exportConditions
|
|---|
| 671 | };
|
|---|
| 672 | const resolvedPackageExport = await resolvePackageExports(
|
|---|
| 673 | context,
|
|---|
| 674 | subpath,
|
|---|
| 675 | pkgJson.exports
|
|---|
| 676 | );
|
|---|
| 677 | location = url.fileURLToPath(resolvedPackageExport);
|
|---|
| 678 | } catch (error) {
|
|---|
| 679 | if (error instanceof ResolveError) {
|
|---|
| 680 | return error;
|
|---|
| 681 | }
|
|---|
| 682 | throw error;
|
|---|
| 683 | }
|
|---|
| 684 | }
|
|---|
| 685 | }
|
|---|
| 686 |
|
|---|
| 687 | if (!location) {
|
|---|
| 688 | // package has no imports or exports, use classic node resolve
|
|---|
| 689 | try {
|
|---|
| 690 | location = await resolveImportPath(importSpecifier, resolveOptions);
|
|---|
| 691 | } catch (error) {
|
|---|
| 692 | if (error.code !== 'MODULE_NOT_FOUND') {
|
|---|
| 693 | throw error;
|
|---|
| 694 | }
|
|---|
| 695 | return null;
|
|---|
| 696 | }
|
|---|
| 697 | }
|
|---|
| 698 |
|
|---|
| 699 | if (!preserveSymlinks) {
|
|---|
| 700 | if (await exists(location)) {
|
|---|
| 701 | location = await realpath(location);
|
|---|
| 702 | }
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 | return {
|
|---|
| 706 | location,
|
|---|
| 707 | hasModuleSideEffects,
|
|---|
| 708 | hasPackageEntry,
|
|---|
| 709 | packageBrowserField,
|
|---|
| 710 | packageInfo
|
|---|
| 711 | };
|
|---|
| 712 | }
|
|---|
| 713 |
|
|---|
| 714 | // Resolve module specifiers in order. Promise resolves to the first module that resolves
|
|---|
| 715 | // successfully, or the error that resulted from the last attempted module resolution.
|
|---|
| 716 | async function resolveImportSpecifiers({
|
|---|
| 717 | importer,
|
|---|
| 718 | importSpecifierList,
|
|---|
| 719 | exportConditions,
|
|---|
| 720 | warn,
|
|---|
| 721 | packageInfoCache,
|
|---|
| 722 | extensions,
|
|---|
| 723 | mainFields,
|
|---|
| 724 | preserveSymlinks,
|
|---|
| 725 | useBrowserOverrides,
|
|---|
| 726 | baseDir,
|
|---|
| 727 | moduleDirectories,
|
|---|
| 728 | rootDir,
|
|---|
| 729 | ignoreSideEffectsForRoot
|
|---|
| 730 | }) {
|
|---|
| 731 | let lastResolveError;
|
|---|
| 732 |
|
|---|
| 733 | for (let i = 0; i < importSpecifierList.length; i++) {
|
|---|
| 734 | // eslint-disable-next-line no-await-in-loop
|
|---|
| 735 | const result = await resolveId({
|
|---|
| 736 | importer,
|
|---|
| 737 | importSpecifier: importSpecifierList[i],
|
|---|
| 738 | exportConditions,
|
|---|
| 739 | warn,
|
|---|
| 740 | packageInfoCache,
|
|---|
| 741 | extensions,
|
|---|
| 742 | mainFields,
|
|---|
| 743 | preserveSymlinks,
|
|---|
| 744 | useBrowserOverrides,
|
|---|
| 745 | baseDir,
|
|---|
| 746 | moduleDirectories,
|
|---|
| 747 | rootDir,
|
|---|
| 748 | ignoreSideEffectsForRoot
|
|---|
| 749 | });
|
|---|
| 750 |
|
|---|
| 751 | if (result instanceof ResolveError) {
|
|---|
| 752 | lastResolveError = result;
|
|---|
| 753 | } else if (result) {
|
|---|
| 754 | return result;
|
|---|
| 755 | }
|
|---|
| 756 | }
|
|---|
| 757 |
|
|---|
| 758 | if (lastResolveError) {
|
|---|
| 759 | // only log the last failed resolve error
|
|---|
| 760 | warn(lastResolveError);
|
|---|
| 761 | }
|
|---|
| 762 | return null;
|
|---|
| 763 | }
|
|---|
| 764 |
|
|---|
| 765 | function handleDeprecatedOptions(opts) {
|
|---|
| 766 | const warnings = [];
|
|---|
| 767 |
|
|---|
| 768 | if (opts.customResolveOptions) {
|
|---|
| 769 | const { customResolveOptions } = opts;
|
|---|
| 770 | if (customResolveOptions.moduleDirectory) {
|
|---|
| 771 | // eslint-disable-next-line no-param-reassign
|
|---|
| 772 | opts.moduleDirectories = Array.isArray(customResolveOptions.moduleDirectory)
|
|---|
| 773 | ? customResolveOptions.moduleDirectory
|
|---|
| 774 | : [customResolveOptions.moduleDirectory];
|
|---|
| 775 |
|
|---|
| 776 | warnings.push(
|
|---|
| 777 | 'node-resolve: The `customResolveOptions.moduleDirectory` option has been deprecated. Use `moduleDirectories`, which must be an array.'
|
|---|
| 778 | );
|
|---|
| 779 | }
|
|---|
| 780 |
|
|---|
| 781 | if (customResolveOptions.preserveSymlinks) {
|
|---|
| 782 | throw new Error(
|
|---|
| 783 | 'node-resolve: `customResolveOptions.preserveSymlinks` is no longer an option. We now always use the rollup `preserveSymlinks` option.'
|
|---|
| 784 | );
|
|---|
| 785 | }
|
|---|
| 786 |
|
|---|
| 787 | [
|
|---|
| 788 | 'basedir',
|
|---|
| 789 | 'package',
|
|---|
| 790 | 'extensions',
|
|---|
| 791 | 'includeCoreModules',
|
|---|
| 792 | 'readFile',
|
|---|
| 793 | 'isFile',
|
|---|
| 794 | 'isDirectory',
|
|---|
| 795 | 'realpath',
|
|---|
| 796 | 'packageFilter',
|
|---|
| 797 | 'pathFilter',
|
|---|
| 798 | 'paths',
|
|---|
| 799 | 'packageIterator'
|
|---|
| 800 | ].forEach((resolveOption) => {
|
|---|
| 801 | if (customResolveOptions[resolveOption]) {
|
|---|
| 802 | throw new Error(
|
|---|
| 803 | `node-resolve: \`customResolveOptions.${resolveOption}\` is no longer an option. If you need this, please open an issue.`
|
|---|
| 804 | );
|
|---|
| 805 | }
|
|---|
| 806 | });
|
|---|
| 807 | }
|
|---|
| 808 |
|
|---|
| 809 | return { warnings };
|
|---|
| 810 | }
|
|---|
| 811 |
|
|---|
| 812 | /* eslint-disable no-param-reassign, no-shadow, no-undefined */
|
|---|
| 813 |
|
|---|
| 814 | const builtins = new Set(builtinList__default['default']);
|
|---|
| 815 | const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js';
|
|---|
| 816 | const deepFreeze = (object) => {
|
|---|
| 817 | Object.freeze(object);
|
|---|
| 818 |
|
|---|
| 819 | for (const value of Object.values(object)) {
|
|---|
| 820 | if (typeof value === 'object' && !Object.isFrozen(value)) {
|
|---|
| 821 | deepFreeze(value);
|
|---|
| 822 | }
|
|---|
| 823 | }
|
|---|
| 824 |
|
|---|
| 825 | return object;
|
|---|
| 826 | };
|
|---|
| 827 |
|
|---|
| 828 | const baseConditions = ['default', 'module'];
|
|---|
| 829 | const baseConditionsEsm = [...baseConditions, 'import'];
|
|---|
| 830 | const baseConditionsCjs = [...baseConditions, 'require'];
|
|---|
| 831 | const defaults = {
|
|---|
| 832 | dedupe: [],
|
|---|
| 833 | // It's important that .mjs is listed before .js so that Rollup will interpret npm modules
|
|---|
| 834 | // which deploy both ESM .mjs and CommonJS .js files as ESM.
|
|---|
| 835 | extensions: ['.mjs', '.js', '.json', '.node'],
|
|---|
| 836 | resolveOnly: [],
|
|---|
| 837 | moduleDirectories: ['node_modules'],
|
|---|
| 838 | ignoreSideEffectsForRoot: false
|
|---|
| 839 | };
|
|---|
| 840 | const DEFAULTS = deepFreeze(deepMerge__default['default']({}, defaults));
|
|---|
| 841 |
|
|---|
| 842 | function nodeResolve(opts = {}) {
|
|---|
| 843 | const { warnings } = handleDeprecatedOptions(opts);
|
|---|
| 844 |
|
|---|
| 845 | const options = { ...defaults, ...opts };
|
|---|
| 846 | const { extensions, jail, moduleDirectories, ignoreSideEffectsForRoot } = options;
|
|---|
| 847 | const conditionsEsm = [...baseConditionsEsm, ...(options.exportConditions || [])];
|
|---|
| 848 | const conditionsCjs = [...baseConditionsCjs, ...(options.exportConditions || [])];
|
|---|
| 849 | const packageInfoCache = new Map();
|
|---|
| 850 | const idToPackageInfo = new Map();
|
|---|
| 851 | const mainFields = getMainFields(options);
|
|---|
| 852 | const useBrowserOverrides = mainFields.indexOf('browser') !== -1;
|
|---|
| 853 | const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
|
|---|
| 854 | const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
|
|---|
| 855 | const rootDir = path.resolve(options.rootDir || process.cwd());
|
|---|
| 856 | let { dedupe } = options;
|
|---|
| 857 | let rollupOptions;
|
|---|
| 858 |
|
|---|
| 859 | if (typeof dedupe !== 'function') {
|
|---|
| 860 | dedupe = (importee) =>
|
|---|
| 861 | options.dedupe.includes(importee) || options.dedupe.includes(getPackageName(importee));
|
|---|
| 862 | }
|
|---|
| 863 |
|
|---|
| 864 | const resolveOnly = options.resolveOnly.map((pattern) => {
|
|---|
| 865 | if (pattern instanceof RegExp) {
|
|---|
| 866 | return pattern;
|
|---|
| 867 | }
|
|---|
| 868 | const normalized = pattern.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&');
|
|---|
| 869 | return new RegExp(`^${normalized}$`);
|
|---|
| 870 | });
|
|---|
| 871 |
|
|---|
| 872 | const browserMapCache = new Map();
|
|---|
| 873 | let preserveSymlinks;
|
|---|
| 874 |
|
|---|
| 875 | return {
|
|---|
| 876 | name: 'node-resolve',
|
|---|
| 877 |
|
|---|
| 878 | buildStart(options) {
|
|---|
| 879 | rollupOptions = options;
|
|---|
| 880 |
|
|---|
| 881 | for (const warning of warnings) {
|
|---|
| 882 | this.warn(warning);
|
|---|
| 883 | }
|
|---|
| 884 |
|
|---|
| 885 | ({ preserveSymlinks } = options);
|
|---|
| 886 | },
|
|---|
| 887 |
|
|---|
| 888 | generateBundle() {
|
|---|
| 889 | readCachedFile.clear();
|
|---|
| 890 | isFileCached.clear();
|
|---|
| 891 | isDirCached.clear();
|
|---|
| 892 | },
|
|---|
| 893 |
|
|---|
| 894 | async resolveId(importee, importer, opts) {
|
|---|
| 895 | if (importee === ES6_BROWSER_EMPTY) {
|
|---|
| 896 | return importee;
|
|---|
| 897 | }
|
|---|
| 898 | // ignore IDs with null character, these belong to other plugins
|
|---|
| 899 | if (/\0/.test(importee)) return null;
|
|---|
| 900 |
|
|---|
| 901 | if (/\0/.test(importer)) {
|
|---|
| 902 | importer = undefined;
|
|---|
| 903 | }
|
|---|
| 904 |
|
|---|
| 905 | // strip query params from import
|
|---|
| 906 | const [importPath, params] = importee.split('?');
|
|---|
| 907 | const importSuffix = `${params ? `?${params}` : ''}`;
|
|---|
| 908 | importee = importPath;
|
|---|
| 909 |
|
|---|
| 910 | const baseDir = !importer || dedupe(importee) ? rootDir : path.dirname(importer);
|
|---|
| 911 |
|
|---|
| 912 | // https://github.com/defunctzombie/package-browser-field-spec
|
|---|
| 913 | const browser = browserMapCache.get(importer);
|
|---|
| 914 | if (useBrowserOverrides && browser) {
|
|---|
| 915 | const resolvedImportee = path.resolve(baseDir, importee);
|
|---|
| 916 | if (browser[importee] === false || browser[resolvedImportee] === false) {
|
|---|
| 917 | return ES6_BROWSER_EMPTY;
|
|---|
| 918 | }
|
|---|
| 919 | const browserImportee =
|
|---|
| 920 | browser[importee] ||
|
|---|
| 921 | browser[resolvedImportee] ||
|
|---|
| 922 | browser[`${resolvedImportee}.js`] ||
|
|---|
| 923 | browser[`${resolvedImportee}.json`];
|
|---|
| 924 | if (browserImportee) {
|
|---|
| 925 | importee = browserImportee;
|
|---|
| 926 | }
|
|---|
| 927 | }
|
|---|
| 928 |
|
|---|
| 929 | const parts = importee.split(/[/\\]/);
|
|---|
| 930 | let id = parts.shift();
|
|---|
| 931 | let isRelativeImport = false;
|
|---|
| 932 |
|
|---|
| 933 | if (id[0] === '@' && parts.length > 0) {
|
|---|
| 934 | // scoped packages
|
|---|
| 935 | id += `/${parts.shift()}`;
|
|---|
| 936 | } else if (id[0] === '.') {
|
|---|
| 937 | // an import relative to the parent dir of the importer
|
|---|
| 938 | id = path.resolve(baseDir, importee);
|
|---|
| 939 | isRelativeImport = true;
|
|---|
| 940 | }
|
|---|
| 941 |
|
|---|
| 942 | if (
|
|---|
| 943 | !isRelativeImport &&
|
|---|
| 944 | resolveOnly.length &&
|
|---|
| 945 | !resolveOnly.some((pattern) => pattern.test(id))
|
|---|
| 946 | ) {
|
|---|
| 947 | if (normalizeInput(rollupOptions.input).includes(importee)) {
|
|---|
| 948 | return null;
|
|---|
| 949 | }
|
|---|
| 950 | return false;
|
|---|
| 951 | }
|
|---|
| 952 |
|
|---|
| 953 | const importSpecifierList = [];
|
|---|
| 954 |
|
|---|
| 955 | if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
|
|---|
| 956 | // For module graph roots (i.e. when importer is undefined), we
|
|---|
| 957 | // need to handle 'path fragments` like `foo/bar` that are commonly
|
|---|
| 958 | // found in rollup config files. If importee doesn't look like a
|
|---|
| 959 | // relative or absolute path, we make it relative and attempt to
|
|---|
| 960 | // resolve it. If we don't find anything, we try resolving it as we
|
|---|
| 961 | // got it.
|
|---|
| 962 | importSpecifierList.push(`./${importee}`);
|
|---|
| 963 | }
|
|---|
| 964 |
|
|---|
| 965 | const importeeIsBuiltin = builtins.has(importee);
|
|---|
| 966 |
|
|---|
| 967 | if (importeeIsBuiltin) {
|
|---|
| 968 | // The `resolve` library will not resolve packages with the same
|
|---|
| 969 | // name as a node built-in module. If we're resolving something
|
|---|
| 970 | // that's a builtin, and we don't prefer to find built-ins, we
|
|---|
| 971 | // first try to look up a local module with that name. If we don't
|
|---|
| 972 | // find anything, we resolve the builtin which just returns back
|
|---|
| 973 | // the built-in's name.
|
|---|
| 974 | importSpecifierList.push(`${importee}/`);
|
|---|
| 975 | }
|
|---|
| 976 |
|
|---|
| 977 | // TypeScript files may import '.js' to refer to either '.ts' or '.tsx'
|
|---|
| 978 | if (importer && importee.endsWith('.js')) {
|
|---|
| 979 | for (const ext of ['.ts', '.tsx']) {
|
|---|
| 980 | if (importer.endsWith(ext) && extensions.includes(ext)) {
|
|---|
| 981 | importSpecifierList.push(importee.replace(/.js$/, ext));
|
|---|
| 982 | }
|
|---|
| 983 | }
|
|---|
| 984 | }
|
|---|
| 985 |
|
|---|
| 986 | importSpecifierList.push(importee);
|
|---|
| 987 |
|
|---|
| 988 | const warn = (...args) => this.warn(...args);
|
|---|
| 989 | const isRequire =
|
|---|
| 990 | opts && opts.custom && opts.custom['node-resolve'] && opts.custom['node-resolve'].isRequire;
|
|---|
| 991 | const exportConditions = isRequire ? conditionsCjs : conditionsEsm;
|
|---|
| 992 |
|
|---|
| 993 | const resolvedWithoutBuiltins = await resolveImportSpecifiers({
|
|---|
| 994 | importer,
|
|---|
| 995 | importSpecifierList,
|
|---|
| 996 | exportConditions,
|
|---|
| 997 | warn,
|
|---|
| 998 | packageInfoCache,
|
|---|
| 999 | extensions,
|
|---|
| 1000 | mainFields,
|
|---|
| 1001 | preserveSymlinks,
|
|---|
| 1002 | useBrowserOverrides,
|
|---|
| 1003 | baseDir,
|
|---|
| 1004 | moduleDirectories,
|
|---|
| 1005 | rootDir,
|
|---|
| 1006 | ignoreSideEffectsForRoot
|
|---|
| 1007 | });
|
|---|
| 1008 |
|
|---|
| 1009 | const resolved =
|
|---|
| 1010 | importeeIsBuiltin && preferBuiltins
|
|---|
| 1011 | ? {
|
|---|
| 1012 | packageInfo: undefined,
|
|---|
| 1013 | hasModuleSideEffects: () => null,
|
|---|
| 1014 | hasPackageEntry: true,
|
|---|
| 1015 | packageBrowserField: false
|
|---|
| 1016 | }
|
|---|
| 1017 | : resolvedWithoutBuiltins;
|
|---|
| 1018 | if (!resolved) {
|
|---|
| 1019 | return null;
|
|---|
| 1020 | }
|
|---|
| 1021 |
|
|---|
| 1022 | const { packageInfo, hasModuleSideEffects, hasPackageEntry, packageBrowserField } = resolved;
|
|---|
| 1023 | let { location } = resolved;
|
|---|
| 1024 | if (packageBrowserField) {
|
|---|
| 1025 | if (Object.prototype.hasOwnProperty.call(packageBrowserField, location)) {
|
|---|
| 1026 | if (!packageBrowserField[location]) {
|
|---|
| 1027 | browserMapCache.set(location, packageBrowserField);
|
|---|
| 1028 | return ES6_BROWSER_EMPTY;
|
|---|
| 1029 | }
|
|---|
| 1030 | location = packageBrowserField[location];
|
|---|
| 1031 | }
|
|---|
| 1032 | browserMapCache.set(location, packageBrowserField);
|
|---|
| 1033 | }
|
|---|
| 1034 |
|
|---|
| 1035 | if (hasPackageEntry && !preserveSymlinks) {
|
|---|
| 1036 | const fileExists = await exists(location);
|
|---|
| 1037 | if (fileExists) {
|
|---|
| 1038 | location = await realpath(location);
|
|---|
| 1039 | }
|
|---|
| 1040 | }
|
|---|
| 1041 |
|
|---|
| 1042 | idToPackageInfo.set(location, packageInfo);
|
|---|
| 1043 |
|
|---|
| 1044 | if (hasPackageEntry) {
|
|---|
| 1045 | if (importeeIsBuiltin && preferBuiltins) {
|
|---|
| 1046 | if (!isPreferBuiltinsSet && resolvedWithoutBuiltins && resolved !== importee) {
|
|---|
| 1047 | this.warn(
|
|---|
| 1048 | `preferring built-in module '${importee}' over local alternative at '${resolvedWithoutBuiltins.location}', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning`
|
|---|
| 1049 | );
|
|---|
| 1050 | }
|
|---|
| 1051 | return false;
|
|---|
| 1052 | } else if (jail && location.indexOf(path.normalize(jail.trim(path.sep))) !== 0) {
|
|---|
| 1053 | return null;
|
|---|
| 1054 | }
|
|---|
| 1055 | }
|
|---|
| 1056 |
|
|---|
| 1057 | if (options.modulesOnly && (await exists(location))) {
|
|---|
| 1058 | const code = await readFile(location, 'utf-8');
|
|---|
| 1059 | if (isModule__default['default'](code)) {
|
|---|
| 1060 | return {
|
|---|
| 1061 | id: `${location}${importSuffix}`,
|
|---|
| 1062 | moduleSideEffects: hasModuleSideEffects(location)
|
|---|
| 1063 | };
|
|---|
| 1064 | }
|
|---|
| 1065 | return null;
|
|---|
| 1066 | }
|
|---|
| 1067 | const result = {
|
|---|
| 1068 | id: `${location}${importSuffix}`,
|
|---|
| 1069 | moduleSideEffects: hasModuleSideEffects(location)
|
|---|
| 1070 | };
|
|---|
| 1071 | return result;
|
|---|
| 1072 | },
|
|---|
| 1073 |
|
|---|
| 1074 | load(importee) {
|
|---|
| 1075 | if (importee === ES6_BROWSER_EMPTY) {
|
|---|
| 1076 | return 'export default {};';
|
|---|
| 1077 | }
|
|---|
| 1078 | return null;
|
|---|
| 1079 | },
|
|---|
| 1080 |
|
|---|
| 1081 | getPackageInfoForId(id) {
|
|---|
| 1082 | return idToPackageInfo.get(id);
|
|---|
| 1083 | }
|
|---|
| 1084 | };
|
|---|
| 1085 | }
|
|---|
| 1086 |
|
|---|
| 1087 | exports.DEFAULTS = DEFAULTS;
|
|---|
| 1088 | exports.default = nodeResolve;
|
|---|
| 1089 | exports.nodeResolve = nodeResolve;
|
|---|