native.js 3.9 KB

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