You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.5 KiB

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