native.js 3.4 KB

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