native.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const { existsSync } = require('node:fs');
  2. const path = require('node:path');
  3. const { platform, arch, report } = require('node:process');
  4. const isMusl = () => !report.getReport().header.glibcVersionRuntime;
  5. const bindingsByPlatformAndArch = {
  6. android: {
  7. arm: { base: 'android-arm-eabi' },
  8. arm64: { base: 'android-arm64' }
  9. },
  10. darwin: {
  11. arm64: { base: 'darwin-arm64' },
  12. x64: { base: 'darwin-x64' }
  13. },
  14. freebsd: {
  15. arm64: { base: 'freebsd-arm64' },
  16. x64: { base: 'freebsd-x64' }
  17. },
  18. linux: {
  19. arm: { base: 'linux-arm-gnueabihf', musl: 'linux-arm-musleabihf' },
  20. arm64: { base: 'linux-arm64-gnu', musl: 'linux-arm64-musl' },
  21. ppc64: { base: 'linux-powerpc64le-gnu', musl: null },
  22. riscv64: { base: 'linux-riscv64-gnu', musl: null },
  23. s390x: { base: 'linux-s390x-gnu', musl: null },
  24. x64: { base: 'linux-x64-gnu', musl: 'linux-x64-musl' }
  25. },
  26. win32: {
  27. arm64: { base: 'win32-arm64-msvc' },
  28. ia32: { base: 'win32-ia32-msvc' },
  29. x64: { base: 'win32-x64-msvc' }
  30. }
  31. };
  32. const msvcLinkFilenameByArch = {
  33. arm64: 'vc_redist.arm64.exe',
  34. ia32: 'vc_redist.x86.exe',
  35. x64: 'vc_redist.x64.exe'
  36. };
  37. const packageBase = getPackageBase();
  38. const localName = `./rollup.${packageBase}.node`;
  39. const requireWithFriendlyError = id => {
  40. try {
  41. return require(id);
  42. } catch (error) {
  43. if (
  44. platform === 'win32' &&
  45. error instanceof Error &&
  46. error.code === 'ERR_DLOPEN_FAILED' &&
  47. error.message.includes('The specified module could not be found')
  48. ) {
  49. const msvcDownloadLink = `https://aka.ms/vs/17/release/${msvcLinkFilenameByArch[arch]}`;
  50. throw new Error(
  51. `Failed to load module ${id}. ` +
  52. 'Required DLL was not found. ' +
  53. 'This error usually happens when Microsoft Visual C++ Redistributable is not installed. ' +
  54. `You can download it from ${msvcDownloadLink}`,
  55. { cause: error }
  56. );
  57. }
  58. throw new Error(
  59. `Cannot find module ${id}. ` +
  60. `npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). ` +
  61. 'Please try `npm i` again after removing both package-lock.json and node_modules directory.',
  62. { cause: error }
  63. );
  64. }
  65. };
  66. const { parse, parseAsync, xxhashBase64Url, xxhashBase36, xxhashBase16 } = requireWithFriendlyError(
  67. existsSync(path.join(__dirname, localName)) ? localName : `@rollup/rollup-${packageBase}`
  68. );
  69. function getPackageBase() {
  70. const imported = bindingsByPlatformAndArch[platform]?.[arch];
  71. if (!imported) {
  72. throwUnsupportedError(false);
  73. }
  74. if ('musl' in imported && isMusl()) {
  75. return imported.musl || throwUnsupportedError(true);
  76. }
  77. return imported.base;
  78. }
  79. function throwUnsupportedError(isMusl) {
  80. throw new Error(
  81. `Your current platform "${platform}${isMusl ? ' (musl)' : ''}" and architecture "${arch}" combination is not yet supported by the native Rollup build. Please use the WASM build "@rollup/wasm-node" instead.
  82. The following platform-architecture combinations are supported:
  83. ${Object.entries(bindingsByPlatformAndArch)
  84. .flatMap(([platformName, architectures]) =>
  85. Object.entries(architectures).flatMap(([architectureName, { musl }]) => {
  86. const name = `${platformName}-${architectureName}`;
  87. return musl ? [name, `${name} (musl)`] : [name];
  88. })
  89. )
  90. .join('\n')}
  91. If this is important to you, please consider supporting Rollup to make a native build for your platform and architecture available.`
  92. );
  93. }
  94. module.exports.parse = parse;
  95. module.exports.parseAsync = parseAsync;
  96. module.exports.xxhashBase64Url = xxhashBase64Url;
  97. module.exports.xxhashBase36 = xxhashBase36;
  98. module.exports.xxhashBase16 = xxhashBase16;