index.es.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. import { dirname, resolve, extname, normalize, sep } from 'path';
  2. import builtinList from 'builtin-modules';
  3. import isModule from 'is-module';
  4. import fs, { realpathSync } from 'fs';
  5. import { promisify } from 'util';
  6. import { createFilter } from '@rollup/pluginutils';
  7. import resolveModule from 'resolve';
  8. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
  9. try {
  10. var info = gen[key](arg);
  11. var value = info.value;
  12. } catch (error) {
  13. reject(error);
  14. return;
  15. }
  16. if (info.done) {
  17. resolve(value);
  18. } else {
  19. Promise.resolve(value).then(_next, _throw);
  20. }
  21. }
  22. function _asyncToGenerator(fn) {
  23. return function () {
  24. var self = this,
  25. args = arguments;
  26. return new Promise(function (resolve, reject) {
  27. var gen = fn.apply(self, args);
  28. function _next(value) {
  29. asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
  30. }
  31. function _throw(err) {
  32. asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
  33. }
  34. _next(undefined);
  35. });
  36. };
  37. }
  38. const exists = promisify(fs.exists);
  39. const readFile = promisify(fs.readFile);
  40. const realpath = promisify(fs.realpath);
  41. const stat = promisify(fs.stat);
  42. const onError = error => {
  43. if (error.code === 'ENOENT') {
  44. return false;
  45. }
  46. throw error;
  47. };
  48. const makeCache = fn => {
  49. const cache = new Map();
  50. const wrapped =
  51. /*#__PURE__*/
  52. function () {
  53. var _ref = _asyncToGenerator(function* (param, done) {
  54. if (cache.has(param) === false) {
  55. cache.set(param, fn(param).catch(err => {
  56. cache.delete(param);
  57. throw err;
  58. }));
  59. }
  60. try {
  61. const result = cache.get(param);
  62. const value = yield result;
  63. return done(null, value);
  64. } catch (error) {
  65. return done(error);
  66. }
  67. });
  68. return function wrapped(_x, _x2) {
  69. return _ref.apply(this, arguments);
  70. };
  71. }();
  72. wrapped.clear = () => cache.clear();
  73. return wrapped;
  74. };
  75. const isDirCached = makeCache(
  76. /*#__PURE__*/
  77. function () {
  78. var _ref2 = _asyncToGenerator(function* (file) {
  79. try {
  80. const stats = yield stat(file);
  81. return stats.isDirectory();
  82. } catch (error) {
  83. return onError(error);
  84. }
  85. });
  86. return function (_x3) {
  87. return _ref2.apply(this, arguments);
  88. };
  89. }());
  90. const isFileCached = makeCache(
  91. /*#__PURE__*/
  92. function () {
  93. var _ref3 = _asyncToGenerator(function* (file) {
  94. try {
  95. const stats = yield stat(file);
  96. return stats.isFile();
  97. } catch (error) {
  98. return onError(error);
  99. }
  100. });
  101. return function (_x4) {
  102. return _ref3.apply(this, arguments);
  103. };
  104. }());
  105. const readCachedFile = makeCache(readFile);
  106. const resolveId = promisify(resolveModule); // returns the imported package name for bare module imports
  107. function getPackageName(id) {
  108. if (id.startsWith('.') || id.startsWith('/')) {
  109. return null;
  110. }
  111. const split = id.split('/'); // @my-scope/my-package/foo.js -> @my-scope/my-package
  112. // @my-scope/my-package -> @my-scope/my-package
  113. if (split[0][0] === '@') {
  114. return `${split[0]}/${split[1]}`;
  115. } // my-package/foo.js -> my-package
  116. // my-package -> my-package
  117. return split[0];
  118. }
  119. function getMainFields(options) {
  120. let mainFields;
  121. if (options.mainFields) {
  122. mainFields = options.mainFields;
  123. } else {
  124. mainFields = ['module', 'main'];
  125. }
  126. if (options.browser && mainFields.indexOf('browser') === -1) {
  127. return ['browser'].concat(mainFields);
  128. }
  129. if (!mainFields.length) {
  130. throw new Error('Please ensure at least one `mainFields` value is specified');
  131. }
  132. return mainFields;
  133. }
  134. function getPackageInfo(options) {
  135. const cache = options.cache,
  136. extensions = options.extensions,
  137. pkg = options.pkg,
  138. mainFields = options.mainFields,
  139. preserveSymlinks = options.preserveSymlinks,
  140. useBrowserOverrides = options.useBrowserOverrides;
  141. let pkgPath = options.pkgPath;
  142. if (cache.has(pkgPath)) {
  143. return cache.get(pkgPath);
  144. } // browserify/resolve doesn't realpath paths returned in its packageFilter callback
  145. if (!preserveSymlinks) {
  146. pkgPath = realpathSync(pkgPath);
  147. }
  148. const pkgRoot = dirname(pkgPath);
  149. const packageInfo = {
  150. // copy as we are about to munge the `main` field of `pkg`.
  151. packageJson: Object.assign({}, pkg),
  152. // path to package.json file
  153. packageJsonPath: pkgPath,
  154. // directory containing the package.json
  155. root: pkgRoot,
  156. // which main field was used during resolution of this module (main, module, or browser)
  157. resolvedMainField: 'main',
  158. // whether the browser map was used to resolve the entry point to this module
  159. browserMappedMain: false,
  160. // the entry point of the module with respect to the selected main field and any
  161. // relevant browser mappings.
  162. resolvedEntryPoint: ''
  163. };
  164. let overriddenMain = false;
  165. for (let i = 0; i < mainFields.length; i++) {
  166. const field = mainFields[i];
  167. if (typeof pkg[field] === 'string') {
  168. pkg.main = pkg[field];
  169. packageInfo.resolvedMainField = field;
  170. overriddenMain = true;
  171. break;
  172. }
  173. }
  174. const internalPackageInfo = {
  175. cachedPkg: pkg,
  176. hasModuleSideEffects: () => null,
  177. hasPackageEntry: overriddenMain !== false || mainFields.indexOf('main') !== -1,
  178. packageBrowserField: useBrowserOverrides && typeof pkg.browser === 'object' && Object.keys(pkg.browser).reduce((browser, key) => {
  179. let resolved = pkg.browser[key];
  180. if (resolved && resolved[0] === '.') {
  181. resolved = resolve(pkgRoot, resolved);
  182. }
  183. /* eslint-disable no-param-reassign */
  184. browser[key] = resolved;
  185. if (key[0] === '.') {
  186. const absoluteKey = resolve(pkgRoot, key);
  187. browser[absoluteKey] = resolved;
  188. if (!extname(key)) {
  189. extensions.reduce((subBrowser, ext) => {
  190. subBrowser[absoluteKey + ext] = subBrowser[key];
  191. return subBrowser;
  192. }, browser);
  193. }
  194. }
  195. return browser;
  196. }, {}),
  197. packageInfo
  198. };
  199. const browserMap = internalPackageInfo.packageBrowserField;
  200. if (useBrowserOverrides && typeof pkg.browser === 'object' && // eslint-disable-next-line no-prototype-builtins
  201. browserMap.hasOwnProperty(pkg.main)) {
  202. packageInfo.resolvedEntryPoint = browserMap[pkg.main];
  203. packageInfo.browserMappedMain = true;
  204. } else {
  205. // index.node is technically a valid default entrypoint as well...
  206. packageInfo.resolvedEntryPoint = resolve(pkgRoot, pkg.main || 'index.js');
  207. packageInfo.browserMappedMain = false;
  208. }
  209. const packageSideEffects = pkg.sideEffects;
  210. if (typeof packageSideEffects === 'boolean') {
  211. internalPackageInfo.hasModuleSideEffects = () => packageSideEffects;
  212. } else if (Array.isArray(packageSideEffects)) {
  213. internalPackageInfo.hasModuleSideEffects = createFilter(packageSideEffects, null, {
  214. resolve: pkgRoot
  215. });
  216. }
  217. cache.set(pkgPath, internalPackageInfo);
  218. return internalPackageInfo;
  219. }
  220. function normalizeInput(input) {
  221. if (Array.isArray(input)) {
  222. return input;
  223. } else if (typeof input === 'object') {
  224. return Object.values(input);
  225. } // otherwise it's a string
  226. return input;
  227. } // Resolve module specifiers in order. Promise resolves to the first module that resolves
  228. // successfully, or the error that resulted from the last attempted module resolution.
  229. function resolveImportSpecifiers(importSpecifierList, resolveOptions) {
  230. let promise = Promise.resolve();
  231. for (let i = 0; i < importSpecifierList.length; i++) {
  232. promise = promise.then(value => {
  233. // if we've already resolved to something, just return it.
  234. if (value) {
  235. return value;
  236. }
  237. return resolveId(importSpecifierList[i], resolveOptions).then(result => {
  238. if (!resolveOptions.preserveSymlinks) {
  239. result = realpathSync(result);
  240. }
  241. return result;
  242. });
  243. });
  244. if (i < importSpecifierList.length - 1) {
  245. // swallow MODULE_NOT_FOUND errors from all but the last resolution
  246. promise = promise.catch(error => {
  247. if (error.code !== 'MODULE_NOT_FOUND') {
  248. throw error;
  249. }
  250. });
  251. }
  252. }
  253. return promise;
  254. }
  255. const builtins = new Set(builtinList);
  256. const ES6_BROWSER_EMPTY = '\0node-resolve:empty.js';
  257. const nullFn = () => null;
  258. const defaults = {
  259. customResolveOptions: {},
  260. dedupe: [],
  261. // It's important that .mjs is listed before .js so that Rollup will interpret npm modules
  262. // which deploy both ESM .mjs and CommonJS .js files as ESM.
  263. extensions: ['.mjs', '.js', '.json', '.node'],
  264. resolveOnly: []
  265. };
  266. function nodeResolve(opts = {}) {
  267. const options = Object.assign({}, defaults, opts);
  268. const customResolveOptions = options.customResolveOptions,
  269. extensions = options.extensions,
  270. jail = options.jail;
  271. const warnings = [];
  272. const packageInfoCache = new Map();
  273. const idToPackageInfo = new Map();
  274. const mainFields = getMainFields(options);
  275. const useBrowserOverrides = mainFields.indexOf('browser') !== -1;
  276. const isPreferBuiltinsSet = options.preferBuiltins === true || options.preferBuiltins === false;
  277. const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
  278. const rootDir = options.rootDir || process.cwd();
  279. let dedupe = options.dedupe;
  280. let rollupOptions;
  281. if (options.only) {
  282. warnings.push('node-resolve: The `only` options is deprecated, please use `resolveOnly`');
  283. options.resolveOnly = options.only;
  284. }
  285. if (typeof dedupe !== 'function') {
  286. dedupe = importee => options.dedupe.includes(importee) || options.dedupe.includes(getPackageName(importee));
  287. }
  288. const resolveOnly = options.resolveOnly.map(pattern => {
  289. if (pattern instanceof RegExp) {
  290. return pattern;
  291. }
  292. const normalized = pattern.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&');
  293. return new RegExp(`^${normalized}$`);
  294. });
  295. const browserMapCache = new Map();
  296. let preserveSymlinks;
  297. return {
  298. name: 'node-resolve',
  299. buildStart(options) {
  300. rollupOptions = options;
  301. var _iteratorNormalCompletion = true;
  302. var _didIteratorError = false;
  303. var _iteratorError = undefined;
  304. try {
  305. for (var _iterator = warnings[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  306. const warning = _step.value;
  307. this.warn(warning);
  308. }
  309. } catch (err) {
  310. _didIteratorError = true;
  311. _iteratorError = err;
  312. } finally {
  313. try {
  314. if (!_iteratorNormalCompletion && _iterator.return != null) {
  315. _iterator.return();
  316. }
  317. } finally {
  318. if (_didIteratorError) {
  319. throw _iteratorError;
  320. }
  321. }
  322. }
  323. preserveSymlinks = options.preserveSymlinks;
  324. },
  325. generateBundle() {
  326. readCachedFile.clear();
  327. isFileCached.clear();
  328. isDirCached.clear();
  329. },
  330. resolveId(importee, importer) {
  331. var _this = this;
  332. return _asyncToGenerator(function* () {
  333. if (importee === ES6_BROWSER_EMPTY) {
  334. return importee;
  335. } // ignore IDs with null character, these belong to other plugins
  336. if (/\0/.test(importee)) return null;
  337. const basedir = !importer || dedupe(importee) ? rootDir : dirname(importer); // https://github.com/defunctzombie/package-browser-field-spec
  338. const browser = browserMapCache.get(importer);
  339. if (useBrowserOverrides && browser) {
  340. const resolvedImportee = resolve(basedir, importee);
  341. if (browser[importee] === false || browser[resolvedImportee] === false) {
  342. return ES6_BROWSER_EMPTY;
  343. }
  344. const browserImportee = browser[importee] || browser[resolvedImportee] || browser[`${resolvedImportee}.js`] || browser[`${resolvedImportee}.json`];
  345. if (browserImportee) {
  346. importee = browserImportee;
  347. }
  348. }
  349. const parts = importee.split(/[/\\]/);
  350. let id = parts.shift();
  351. if (id[0] === '@' && parts.length > 0) {
  352. // scoped packages
  353. id += `/${parts.shift()}`;
  354. } else if (id[0] === '.') {
  355. // an import relative to the parent dir of the importer
  356. id = resolve(basedir, importee);
  357. }
  358. const input = normalizeInput(rollupOptions.input);
  359. if (resolveOnly.length && !resolveOnly.some(pattern => pattern.test(id))) {
  360. if (input.includes(id)) {
  361. return null;
  362. }
  363. return false;
  364. }
  365. let hasModuleSideEffects = nullFn;
  366. let hasPackageEntry = true;
  367. let packageBrowserField = false;
  368. let packageInfo;
  369. const filter = (pkg, pkgPath) => {
  370. const info = getPackageInfo({
  371. cache: packageInfoCache,
  372. extensions,
  373. pkg,
  374. pkgPath,
  375. mainFields,
  376. preserveSymlinks,
  377. useBrowserOverrides
  378. });
  379. packageInfo = info.packageInfo;
  380. hasModuleSideEffects = info.hasModuleSideEffects;
  381. hasPackageEntry = info.hasPackageEntry;
  382. packageBrowserField = info.packageBrowserField;
  383. return info.cachedPkg;
  384. };
  385. let resolveOptions = {
  386. basedir,
  387. packageFilter: filter,
  388. readFile: readCachedFile,
  389. isFile: isFileCached,
  390. isDirectory: isDirCached,
  391. extensions
  392. };
  393. if (preserveSymlinks !== undefined) {
  394. resolveOptions.preserveSymlinks = preserveSymlinks;
  395. }
  396. const importSpecifierList = [];
  397. if (importer === undefined && !importee[0].match(/^\.?\.?\//)) {
  398. // For module graph roots (i.e. when importer is undefined), we
  399. // need to handle 'path fragments` like `foo/bar` that are commonly
  400. // found in rollup config files. If importee doesn't look like a
  401. // relative or absolute path, we make it relative and attempt to
  402. // resolve it. If we don't find anything, we try resolving it as we
  403. // got it.
  404. importSpecifierList.push(`./${importee}`);
  405. }
  406. const importeeIsBuiltin = builtins.has(importee);
  407. if (importeeIsBuiltin && (!preferBuiltins || !isPreferBuiltinsSet)) {
  408. // The `resolve` library will not resolve packages with the same
  409. // name as a node built-in module. If we're resolving something
  410. // that's a builtin, and we don't prefer to find built-ins, we
  411. // first try to look up a local module with that name. If we don't
  412. // find anything, we resolve the builtin which just returns back
  413. // the built-in's name.
  414. importSpecifierList.push(`${importee}/`);
  415. }
  416. importSpecifierList.push(importee);
  417. resolveOptions = Object.assign(resolveOptions, customResolveOptions);
  418. try {
  419. let resolved = yield resolveImportSpecifiers(importSpecifierList, resolveOptions);
  420. if (resolved && packageBrowserField) {
  421. if (Object.prototype.hasOwnProperty.call(packageBrowserField, resolved)) {
  422. if (!packageBrowserField[resolved]) {
  423. browserMapCache.set(resolved, packageBrowserField);
  424. return ES6_BROWSER_EMPTY;
  425. }
  426. resolved = packageBrowserField[resolved];
  427. }
  428. browserMapCache.set(resolved, packageBrowserField);
  429. }
  430. if (hasPackageEntry && !preserveSymlinks && resolved) {
  431. const fileExists = yield exists(resolved);
  432. if (fileExists) {
  433. resolved = yield realpath(resolved);
  434. }
  435. }
  436. idToPackageInfo.set(resolved, packageInfo);
  437. if (hasPackageEntry) {
  438. if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) {
  439. return null;
  440. } else if (importeeIsBuiltin && preferBuiltins) {
  441. if (!isPreferBuiltinsSet) {
  442. _this.warn(`preferring built-in module '${importee}' over local alternative at '${resolved}', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning`);
  443. }
  444. return null;
  445. } else if (jail && resolved.indexOf(normalize(jail.trim(sep))) !== 0) {
  446. return null;
  447. }
  448. }
  449. if (resolved && options.modulesOnly) {
  450. const code = yield readFile(resolved, 'utf-8');
  451. if (isModule(code)) {
  452. return {
  453. id: resolved,
  454. moduleSideEffects: hasModuleSideEffects(resolved)
  455. };
  456. }
  457. return null;
  458. }
  459. const result = {
  460. id: resolved,
  461. moduleSideEffects: hasModuleSideEffects(resolved)
  462. };
  463. return result;
  464. } catch (error) {
  465. return null;
  466. }
  467. })();
  468. },
  469. load(importee) {
  470. if (importee === ES6_BROWSER_EMPTY) {
  471. return 'export default {};';
  472. }
  473. return null;
  474. },
  475. getPackageInfoForId(id) {
  476. return idToPackageInfo.get(id);
  477. }
  478. };
  479. }
  480. export default nodeResolve;