native.js 3.8 KB

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