esbuild 9.0 KB

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