esbuild 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env node
  2. "use strict";
  3. var __create = Object.create;
  4. var __defProp = Object.defineProperty;
  5. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  6. var __getOwnPropNames = Object.getOwnPropertyNames;
  7. var __getProtoOf = Object.getPrototypeOf;
  8. var __hasOwnProp = Object.prototype.hasOwnProperty;
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  18. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  19. mod
  20. ));
  21. // lib/npm/node-platform.ts
  22. var fs = require("fs");
  23. var os = require("os");
  24. var path = require("path");
  25. var ESBUILD_BINARY_PATH = process.env.ESBUILD_BINARY_PATH || ESBUILD_BINARY_PATH;
  26. var packageDarwin_arm64 = "@esbuild/darwin-arm64";
  27. var packageDarwin_x64 = "@esbuild/darwin-x64";
  28. var knownWindowsPackages = {
  29. "win32 arm64 LE": "@esbuild/win32-arm64",
  30. "win32 ia32 LE": "@esbuild/win32-ia32",
  31. "win32 x64 LE": "@esbuild/win32-x64"
  32. };
  33. var knownUnixlikePackages = {
  34. "android arm64 LE": "@esbuild/android-arm64",
  35. "darwin arm64 LE": "@esbuild/darwin-arm64",
  36. "darwin x64 LE": "@esbuild/darwin-x64",
  37. "freebsd arm64 LE": "@esbuild/freebsd-arm64",
  38. "freebsd x64 LE": "@esbuild/freebsd-x64",
  39. "linux arm LE": "@esbuild/linux-arm",
  40. "linux arm64 LE": "@esbuild/linux-arm64",
  41. "linux ia32 LE": "@esbuild/linux-ia32",
  42. "linux mips64el LE": "@esbuild/linux-mips64el",
  43. "linux ppc64 LE": "@esbuild/linux-ppc64",
  44. "linux riscv64 LE": "@esbuild/linux-riscv64",
  45. "linux s390x BE": "@esbuild/linux-s390x",
  46. "linux x64 LE": "@esbuild/linux-x64",
  47. "linux loong64 LE": "@esbuild/linux-loong64",
  48. "netbsd x64 LE": "@esbuild/netbsd-x64",
  49. "openbsd x64 LE": "@esbuild/openbsd-x64",
  50. "sunos x64 LE": "@esbuild/sunos-x64"
  51. };
  52. var knownWebAssemblyFallbackPackages = {
  53. "android arm LE": "@esbuild/android-arm",
  54. "android x64 LE": "@esbuild/android-x64"
  55. };
  56. function pkgAndSubpathForCurrentPlatform() {
  57. let pkg;
  58. let subpath;
  59. let isWASM2 = false;
  60. let platformKey = `${process.platform} ${os.arch()} ${os.endianness()}`;
  61. if (platformKey in knownWindowsPackages) {
  62. pkg = knownWindowsPackages[platformKey];
  63. subpath = "esbuild.exe";
  64. } else if (platformKey in knownUnixlikePackages) {
  65. pkg = knownUnixlikePackages[platformKey];
  66. subpath = "bin/esbuild";
  67. } else if (platformKey in knownWebAssemblyFallbackPackages) {
  68. pkg = knownWebAssemblyFallbackPackages[platformKey];
  69. subpath = "bin/esbuild";
  70. isWASM2 = true;
  71. } else {
  72. throw new Error(`Unsupported platform: ${platformKey}`);
  73. }
  74. return { pkg, subpath, isWASM: isWASM2 };
  75. }
  76. function pkgForSomeOtherPlatform() {
  77. const libMainJS = require.resolve("esbuild");
  78. const nodeModulesDirectory = path.dirname(path.dirname(path.dirname(libMainJS)));
  79. if (path.basename(nodeModulesDirectory) === "node_modules") {
  80. for (const unixKey in knownUnixlikePackages) {
  81. try {
  82. const pkg = knownUnixlikePackages[unixKey];
  83. if (fs.existsSync(path.join(nodeModulesDirectory, pkg)))
  84. return pkg;
  85. } catch {
  86. }
  87. }
  88. for (const windowsKey in knownWindowsPackages) {
  89. try {
  90. const pkg = knownWindowsPackages[windowsKey];
  91. if (fs.existsSync(path.join(nodeModulesDirectory, pkg)))
  92. return pkg;
  93. } catch {
  94. }
  95. }
  96. }
  97. return null;
  98. }
  99. function downloadedBinPath(pkg, subpath) {
  100. const esbuildLibDir = path.dirname(require.resolve("esbuild"));
  101. return path.join(esbuildLibDir, `downloaded-${pkg.replace("/", "-")}-${path.basename(subpath)}`);
  102. }
  103. function generateBinPath() {
  104. if (ESBUILD_BINARY_PATH) {
  105. return { binPath: ESBUILD_BINARY_PATH, isWASM: false };
  106. }
  107. const { pkg, subpath, isWASM: isWASM2 } = pkgAndSubpathForCurrentPlatform();
  108. let binPath2;
  109. try {
  110. binPath2 = require.resolve(`${pkg}/${subpath}`);
  111. } catch (e) {
  112. binPath2 = downloadedBinPath(pkg, subpath);
  113. if (!fs.existsSync(binPath2)) {
  114. try {
  115. require.resolve(pkg);
  116. } catch {
  117. const otherPkg = pkgForSomeOtherPlatform();
  118. if (otherPkg) {
  119. let suggestions = `
  120. Specifically the "${otherPkg}" package is present but this platform
  121. needs the "${pkg}" package instead. People often get into this
  122. situation by installing esbuild on Windows or macOS and copying "node_modules"
  123. into a Docker image that runs Linux, or by copying "node_modules" between
  124. Windows and WSL environments.
  125. If you are installing with npm, you can try not copying the "node_modules"
  126. directory when you copy the files over, and running "npm ci" or "npm install"
  127. on the destination platform after the copy. Or you could consider using yarn
  128. instead of npm which has built-in support for installing a package on multiple
  129. platforms simultaneously.
  130. If you are installing with yarn, you can try listing both this platform and the
  131. other platform in your ".yarnrc.yml" file using the "supportedArchitectures"
  132. feature: https://yarnpkg.com/configuration/yarnrc/#supportedArchitectures
  133. Keep in mind that this means multiple copies of esbuild will be present.
  134. `;
  135. if (pkg === packageDarwin_x64 && otherPkg === packageDarwin_arm64 || pkg === packageDarwin_arm64 && otherPkg === packageDarwin_x64) {
  136. suggestions = `
  137. Specifically the "${otherPkg}" package is present but this platform
  138. needs the "${pkg}" package instead. People often get into this
  139. situation by installing esbuild with npm running inside of Rosetta 2 and then
  140. trying to use it with node running outside of Rosetta 2, or vice versa (Rosetta
  141. 2 is Apple's on-the-fly x86_64-to-arm64 translation service).
  142. If you are installing with npm, you can try ensuring that both npm and node are
  143. not running under Rosetta 2 and then reinstalling esbuild. This likely involves
  144. changing how you installed npm and/or node. For example, installing node with
  145. the universal installer here should work: https://nodejs.org/en/download/. Or
  146. you could consider using yarn instead of npm which has built-in support for
  147. installing a package on multiple platforms simultaneously.
  148. If you are installing with yarn, you can try listing both "arm64" and "x64"
  149. in your ".yarnrc.yml" file using the "supportedArchitectures" feature:
  150. https://yarnpkg.com/configuration/yarnrc/#supportedArchitectures
  151. Keep in mind that this means multiple copies of esbuild will be present.
  152. `;
  153. }
  154. throw new Error(`
  155. You installed esbuild for another platform than the one you're currently using.
  156. This won't work because esbuild is written with native code and needs to
  157. install a platform-specific binary executable.
  158. ${suggestions}
  159. Another alternative is to use the "esbuild-wasm" package instead, which works
  160. the same way on all platforms. But it comes with a heavy performance cost and
  161. can sometimes be 10x slower than the "esbuild" package, so you may also not
  162. want to do that.
  163. `);
  164. }
  165. throw new Error(`The package "${pkg}" could not be found, and is needed by esbuild.
  166. If you are installing esbuild with npm, make sure that you don't specify the
  167. "--no-optional" or "--omit=optional" flags. The "optionalDependencies" feature
  168. of "package.json" is used by esbuild to install the correct binary executable
  169. for your current platform.`);
  170. }
  171. throw e;
  172. }
  173. }
  174. if (/\.zip\//.test(binPath2)) {
  175. let pnpapi;
  176. try {
  177. pnpapi = require("pnpapi");
  178. } catch (e) {
  179. }
  180. if (pnpapi) {
  181. const root = pnpapi.getPackageInformation(pnpapi.topLevel).packageLocation;
  182. const binTargetPath = path.join(
  183. root,
  184. "node_modules",
  185. ".cache",
  186. "esbuild",
  187. `pnpapi-${pkg.replace("/", "-")}-${"0.16.3"}-${path.basename(subpath)}`
  188. );
  189. if (!fs.existsSync(binTargetPath)) {
  190. fs.mkdirSync(path.dirname(binTargetPath), { recursive: true });
  191. fs.copyFileSync(binPath2, binTargetPath);
  192. fs.chmodSync(binTargetPath, 493);
  193. }
  194. return { binPath: binTargetPath, isWASM: isWASM2 };
  195. }
  196. }
  197. return { binPath: binPath2, isWASM: isWASM2 };
  198. }
  199. // lib/npm/node-shim.ts
  200. var { binPath, isWASM } = generateBinPath();
  201. if (isWASM) {
  202. require("child_process").execFileSync("node", [binPath].concat(process.argv.slice(2)), { stdio: "inherit" });
  203. } else {
  204. require("child_process").execFileSync(binPath, process.argv.slice(2), { stdio: "inherit" });
  205. }