index.js 17 KB

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