install.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __copyProps = (to, from, except, desc) => {
  9. if (from && typeof from === "object" || typeof from === "function") {
  10. for (let key of __getOwnPropNames(from))
  11. if (!__hasOwnProp.call(to, key) && key !== except)
  12. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  13. }
  14. return to;
  15. };
  16. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  17. // If the importer is in node compatibility mode or this is not an ESM
  18. // file that has been converted to a CommonJS file using a Babel-
  19. // compatible transform (i.e. "__esModule" has not been set), then set
  20. // "default" to the CommonJS "module.exports" for node compatibility.
  21. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  22. mod
  23. ));
  24. // lib/npm/node-platform.ts
  25. var fs = require("fs");
  26. var os = require("os");
  27. var path = require("path");
  28. var ESBUILD_BINARY_PATH = process.env.ESBUILD_BINARY_PATH || ESBUILD_BINARY_PATH;
  29. var isValidBinaryPath = (x) => !!x && x !== "/usr/bin/esbuild";
  30. var knownWindowsPackages = {
  31. "win32 arm64 LE": "@esbuild/win32-arm64",
  32. "win32 ia32 LE": "@esbuild/win32-ia32",
  33. "win32 x64 LE": "@esbuild/win32-x64"
  34. };
  35. var knownUnixlikePackages = {
  36. "android arm64 LE": "@esbuild/android-arm64",
  37. "darwin arm64 LE": "@esbuild/darwin-arm64",
  38. "darwin x64 LE": "@esbuild/darwin-x64",
  39. "freebsd arm64 LE": "@esbuild/freebsd-arm64",
  40. "freebsd x64 LE": "@esbuild/freebsd-x64",
  41. "linux arm LE": "@esbuild/linux-arm",
  42. "linux arm64 LE": "@esbuild/linux-arm64",
  43. "linux ia32 LE": "@esbuild/linux-ia32",
  44. "linux mips64el LE": "@esbuild/linux-mips64el",
  45. "linux ppc64 LE": "@esbuild/linux-ppc64",
  46. "linux riscv64 LE": "@esbuild/linux-riscv64",
  47. "linux s390x BE": "@esbuild/linux-s390x",
  48. "linux x64 LE": "@esbuild/linux-x64",
  49. "linux loong64 LE": "@esbuild/linux-loong64",
  50. "netbsd x64 LE": "@esbuild/netbsd-x64",
  51. "openbsd x64 LE": "@esbuild/openbsd-x64",
  52. "sunos x64 LE": "@esbuild/sunos-x64"
  53. };
  54. var knownWebAssemblyFallbackPackages = {
  55. "android arm LE": "@esbuild/android-arm",
  56. "android x64 LE": "@esbuild/android-x64"
  57. };
  58. function pkgAndSubpathForCurrentPlatform() {
  59. let pkg;
  60. let subpath;
  61. let isWASM = false;
  62. let platformKey = `${process.platform} ${os.arch()} ${os.endianness()}`;
  63. if (platformKey in knownWindowsPackages) {
  64. pkg = knownWindowsPackages[platformKey];
  65. subpath = "esbuild.exe";
  66. } else if (platformKey in knownUnixlikePackages) {
  67. pkg = knownUnixlikePackages[platformKey];
  68. subpath = "bin/esbuild";
  69. } else if (platformKey in knownWebAssemblyFallbackPackages) {
  70. pkg = knownWebAssemblyFallbackPackages[platformKey];
  71. subpath = "bin/esbuild";
  72. isWASM = true;
  73. } else {
  74. throw new Error(`Unsupported platform: ${platformKey}`);
  75. }
  76. return { pkg, subpath, isWASM };
  77. }
  78. function downloadedBinPath(pkg, subpath) {
  79. const esbuildLibDir = path.dirname(require.resolve("esbuild"));
  80. return path.join(esbuildLibDir, `downloaded-${pkg.replace("/", "-")}-${path.basename(subpath)}`);
  81. }
  82. // lib/npm/node-install.ts
  83. var fs2 = require("fs");
  84. var os2 = require("os");
  85. var path2 = require("path");
  86. var zlib = require("zlib");
  87. var https = require("https");
  88. var child_process = require("child_process");
  89. var versionFromPackageJSON = require(path2.join(__dirname, "package.json")).version;
  90. var toPath = path2.join(__dirname, "bin", "esbuild");
  91. var isToPathJS = true;
  92. function validateBinaryVersion(...command) {
  93. command.push("--version");
  94. let stdout;
  95. try {
  96. stdout = child_process.execFileSync(command.shift(), command, {
  97. // Without this, this install script strangely crashes with the error
  98. // "EACCES: permission denied, write" but only on Ubuntu Linux when node is
  99. // installed from the Snap Store. This is not a problem when you download
  100. // the official version of node. The problem appears to be that stderr
  101. // (i.e. file descriptor 2) isn't writable?
  102. //
  103. // More info:
  104. // - https://snapcraft.io/ (what the Snap Store is)
  105. // - https://nodejs.org/dist/ (download the official version of node)
  106. // - https://github.com/evanw/esbuild/issues/1711#issuecomment-1027554035
  107. //
  108. stdio: "pipe"
  109. }).toString().trim();
  110. } catch (err) {
  111. if (os2.platform() === "darwin" && /_SecTrustEvaluateWithError/.test(err + "")) {
  112. let os3 = "this version of macOS";
  113. try {
  114. os3 = "macOS " + child_process.execFileSync("sw_vers", ["-productVersion"]).toString().trim();
  115. } catch {
  116. }
  117. throw new Error(`The "esbuild" package cannot be installed because ${os3} is too outdated.
  118. The Go compiler (which esbuild relies on) no longer supports ${os3},
  119. which means the "esbuild" binary executable can't be run. You can either:
  120. * Update your version of macOS to one that the Go compiler supports
  121. * Use the "esbuild-wasm" package instead of the "esbuild" package
  122. * Build esbuild yourself using an older version of the Go compiler
  123. `);
  124. }
  125. throw err;
  126. }
  127. if (stdout !== versionFromPackageJSON) {
  128. throw new Error(`Expected ${JSON.stringify(versionFromPackageJSON)} but got ${JSON.stringify(stdout)}`);
  129. }
  130. }
  131. function isYarn() {
  132. const { npm_config_user_agent } = process.env;
  133. if (npm_config_user_agent) {
  134. return /\byarn\//.test(npm_config_user_agent);
  135. }
  136. return false;
  137. }
  138. function fetch(url) {
  139. return new Promise((resolve, reject) => {
  140. https.get(url, (res) => {
  141. if ((res.statusCode === 301 || res.statusCode === 302) && res.headers.location)
  142. return fetch(res.headers.location).then(resolve, reject);
  143. if (res.statusCode !== 200)
  144. return reject(new Error(`Server responded with ${res.statusCode}`));
  145. let chunks = [];
  146. res.on("data", (chunk) => chunks.push(chunk));
  147. res.on("end", () => resolve(Buffer.concat(chunks)));
  148. }).on("error", reject);
  149. });
  150. }
  151. function extractFileFromTarGzip(buffer, subpath) {
  152. try {
  153. buffer = zlib.unzipSync(buffer);
  154. } catch (err) {
  155. throw new Error(`Invalid gzip data in archive: ${err && err.message || err}`);
  156. }
  157. let str = (i, n) => String.fromCharCode(...buffer.subarray(i, i + n)).replace(/\0.*$/, "");
  158. let offset = 0;
  159. subpath = `package/${subpath}`;
  160. while (offset < buffer.length) {
  161. let name = str(offset, 100);
  162. let size = parseInt(str(offset + 124, 12), 8);
  163. offset += 512;
  164. if (!isNaN(size)) {
  165. if (name === subpath)
  166. return buffer.subarray(offset, offset + size);
  167. offset += size + 511 & ~511;
  168. }
  169. }
  170. throw new Error(`Could not find ${JSON.stringify(subpath)} in archive`);
  171. }
  172. function installUsingNPM(pkg, subpath, binPath) {
  173. const env = { ...process.env, npm_config_global: void 0 };
  174. const esbuildLibDir = path2.dirname(require.resolve("esbuild"));
  175. const installDir = path2.join(esbuildLibDir, "npm-install");
  176. fs2.mkdirSync(installDir);
  177. try {
  178. fs2.writeFileSync(path2.join(installDir, "package.json"), "{}");
  179. child_process.execSync(
  180. `npm install --loglevel=error --prefer-offline --no-audit --progress=false ${pkg}@${versionFromPackageJSON}`,
  181. { cwd: installDir, stdio: "pipe", env }
  182. );
  183. const installedBinPath = path2.join(installDir, "node_modules", pkg, subpath);
  184. fs2.renameSync(installedBinPath, binPath);
  185. } finally {
  186. try {
  187. removeRecursive(installDir);
  188. } catch {
  189. }
  190. }
  191. }
  192. function removeRecursive(dir) {
  193. for (const entry of fs2.readdirSync(dir)) {
  194. const entryPath = path2.join(dir, entry);
  195. let stats;
  196. try {
  197. stats = fs2.lstatSync(entryPath);
  198. } catch {
  199. continue;
  200. }
  201. if (stats.isDirectory())
  202. removeRecursive(entryPath);
  203. else
  204. fs2.unlinkSync(entryPath);
  205. }
  206. fs2.rmdirSync(dir);
  207. }
  208. function applyManualBinaryPathOverride(overridePath) {
  209. const pathString = JSON.stringify(overridePath);
  210. fs2.writeFileSync(toPath, `#!/usr/bin/env node
  211. require('child_process').execFileSync(${pathString}, process.argv.slice(2), { stdio: 'inherit' });
  212. `);
  213. const libMain = path2.join(__dirname, "lib", "main.js");
  214. const code = fs2.readFileSync(libMain, "utf8");
  215. fs2.writeFileSync(libMain, `var ESBUILD_BINARY_PATH = ${pathString};
  216. ${code}`);
  217. }
  218. function maybeOptimizePackage(binPath) {
  219. if (os2.platform() !== "win32" && !isYarn()) {
  220. const tempPath = path2.join(__dirname, "bin-esbuild");
  221. try {
  222. fs2.linkSync(binPath, tempPath);
  223. fs2.renameSync(tempPath, toPath);
  224. isToPathJS = false;
  225. fs2.unlinkSync(tempPath);
  226. } catch {
  227. }
  228. }
  229. }
  230. async function downloadDirectlyFromNPM(pkg, subpath, binPath) {
  231. const url = `https://registry.npmjs.org/${pkg}/-/${pkg.replace("@esbuild/", "")}-${versionFromPackageJSON}.tgz`;
  232. console.error(`[esbuild] Trying to download ${JSON.stringify(url)}`);
  233. try {
  234. fs2.writeFileSync(binPath, extractFileFromTarGzip(await fetch(url), subpath));
  235. fs2.chmodSync(binPath, 493);
  236. } catch (e) {
  237. console.error(`[esbuild] Failed to download ${JSON.stringify(url)}: ${e && e.message || e}`);
  238. throw e;
  239. }
  240. }
  241. async function checkAndPreparePackage() {
  242. if (isValidBinaryPath(ESBUILD_BINARY_PATH)) {
  243. if (!fs2.existsSync(ESBUILD_BINARY_PATH)) {
  244. console.warn(`[esbuild] Ignoring bad configuration: ESBUILD_BINARY_PATH=${ESBUILD_BINARY_PATH}`);
  245. } else {
  246. applyManualBinaryPathOverride(ESBUILD_BINARY_PATH);
  247. return;
  248. }
  249. }
  250. const { pkg, subpath } = pkgAndSubpathForCurrentPlatform();
  251. let binPath;
  252. try {
  253. binPath = require.resolve(`${pkg}/${subpath}`);
  254. } catch (e) {
  255. console.error(`[esbuild] Failed to find package "${pkg}" on the file system
  256. This can happen if you use the "--no-optional" flag. The "optionalDependencies"
  257. package.json feature is used by esbuild to install the correct binary executable
  258. for your current platform. This install script will now attempt to work around
  259. this. If that fails, you need to remove the "--no-optional" flag to use esbuild.
  260. `);
  261. binPath = downloadedBinPath(pkg, subpath);
  262. try {
  263. console.error(`[esbuild] Trying to install package "${pkg}" using npm`);
  264. installUsingNPM(pkg, subpath, binPath);
  265. } catch (e2) {
  266. console.error(`[esbuild] Failed to install package "${pkg}" using npm: ${e2 && e2.message || e2}`);
  267. try {
  268. await downloadDirectlyFromNPM(pkg, subpath, binPath);
  269. } catch (e3) {
  270. throw new Error(`Failed to install package "${pkg}"`);
  271. }
  272. }
  273. }
  274. maybeOptimizePackage(binPath);
  275. }
  276. checkAndPreparePackage().then(() => {
  277. if (isToPathJS) {
  278. validateBinaryVersion(process.execPath, toPath);
  279. } else {
  280. validateBinaryVersion(toPath);
  281. }
  282. });