install.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  18. mod
  19. ));
  20. // lib/npm/node-platform.ts
  21. var fs = require("fs");
  22. var os = require("os");
  23. var path = require("path");
  24. var ESBUILD_BINARY_PATH = process.env.ESBUILD_BINARY_PATH || ESBUILD_BINARY_PATH;
  25. var knownWindowsPackages = {
  26. "win32 arm64 LE": "@esbuild/win32-arm64",
  27. "win32 ia32 LE": "@esbuild/win32-ia32",
  28. "win32 x64 LE": "@esbuild/win32-x64"
  29. };
  30. var knownUnixlikePackages = {
  31. "android arm64 LE": "@esbuild/android-arm64",
  32. "darwin arm64 LE": "@esbuild/darwin-arm64",
  33. "darwin x64 LE": "@esbuild/darwin-x64",
  34. "freebsd arm64 LE": "@esbuild/freebsd-arm64",
  35. "freebsd x64 LE": "@esbuild/freebsd-x64",
  36. "linux arm LE": "@esbuild/linux-arm",
  37. "linux arm64 LE": "@esbuild/linux-arm64",
  38. "linux ia32 LE": "@esbuild/linux-ia32",
  39. "linux mips64el LE": "@esbuild/linux-mips64el",
  40. "linux ppc64 LE": "@esbuild/linux-ppc64",
  41. "linux riscv64 LE": "@esbuild/linux-riscv64",
  42. "linux s390x BE": "@esbuild/linux-s390x",
  43. "linux x64 LE": "@esbuild/linux-x64",
  44. "linux loong64 LE": "@esbuild/linux-loong64",
  45. "netbsd x64 LE": "@esbuild/netbsd-x64",
  46. "openbsd x64 LE": "@esbuild/openbsd-x64",
  47. "sunos x64 LE": "@esbuild/sunos-x64"
  48. };
  49. var knownWebAssemblyFallbackPackages = {
  50. "android arm LE": "@esbuild/android-arm",
  51. "android x64 LE": "@esbuild/android-x64"
  52. };
  53. function pkgAndSubpathForCurrentPlatform() {
  54. let pkg;
  55. let subpath;
  56. let isWASM = false;
  57. let platformKey = `${process.platform} ${os.arch()} ${os.endianness()}`;
  58. if (platformKey in knownWindowsPackages) {
  59. pkg = knownWindowsPackages[platformKey];
  60. subpath = "esbuild.exe";
  61. } else if (platformKey in knownUnixlikePackages) {
  62. pkg = knownUnixlikePackages[platformKey];
  63. subpath = "bin/esbuild";
  64. } else if (platformKey in knownWebAssemblyFallbackPackages) {
  65. pkg = knownWebAssemblyFallbackPackages[platformKey];
  66. subpath = "bin/esbuild";
  67. isWASM = true;
  68. } else {
  69. throw new Error(`Unsupported platform: ${platformKey}`);
  70. }
  71. return { pkg, subpath, isWASM };
  72. }
  73. function downloadedBinPath(pkg, subpath) {
  74. const esbuildLibDir = path.dirname(require.resolve("esbuild"));
  75. return path.join(esbuildLibDir, `downloaded-${pkg.replace("/", "-")}-${path.basename(subpath)}`);
  76. }
  77. // lib/npm/node-install.ts
  78. var fs2 = require("fs");
  79. var os2 = require("os");
  80. var path2 = require("path");
  81. var zlib = require("zlib");
  82. var https = require("https");
  83. var child_process = require("child_process");
  84. var toPath = path2.join(__dirname, "bin", "esbuild");
  85. var isToPathJS = true;
  86. function validateBinaryVersion(...command) {
  87. command.push("--version");
  88. const stdout = child_process.execFileSync(command.shift(), command, {
  89. stdio: "pipe"
  90. }).toString().trim();
  91. if (stdout !== "0.16.3") {
  92. throw new Error(`Expected ${JSON.stringify("0.16.3")} but got ${JSON.stringify(stdout)}`);
  93. }
  94. }
  95. function isYarn() {
  96. const { npm_config_user_agent } = process.env;
  97. if (npm_config_user_agent) {
  98. return /\byarn\//.test(npm_config_user_agent);
  99. }
  100. return false;
  101. }
  102. function fetch(url) {
  103. return new Promise((resolve, reject) => {
  104. https.get(url, (res) => {
  105. if ((res.statusCode === 301 || res.statusCode === 302) && res.headers.location)
  106. return fetch(res.headers.location).then(resolve, reject);
  107. if (res.statusCode !== 200)
  108. return reject(new Error(`Server responded with ${res.statusCode}`));
  109. let chunks = [];
  110. res.on("data", (chunk) => chunks.push(chunk));
  111. res.on("end", () => resolve(Buffer.concat(chunks)));
  112. }).on("error", reject);
  113. });
  114. }
  115. function extractFileFromTarGzip(buffer, subpath) {
  116. try {
  117. buffer = zlib.unzipSync(buffer);
  118. } catch (err) {
  119. throw new Error(`Invalid gzip data in archive: ${err && err.message || err}`);
  120. }
  121. let str = (i, n) => String.fromCharCode(...buffer.subarray(i, i + n)).replace(/\0.*$/, "");
  122. let offset = 0;
  123. subpath = `package/${subpath}`;
  124. while (offset < buffer.length) {
  125. let name = str(offset, 100);
  126. let size = parseInt(str(offset + 124, 12), 8);
  127. offset += 512;
  128. if (!isNaN(size)) {
  129. if (name === subpath)
  130. return buffer.subarray(offset, offset + size);
  131. offset += size + 511 & ~511;
  132. }
  133. }
  134. throw new Error(`Could not find ${JSON.stringify(subpath)} in archive`);
  135. }
  136. function installUsingNPM(pkg, subpath, binPath) {
  137. const env = { ...process.env, npm_config_global: void 0 };
  138. const esbuildLibDir = path2.dirname(require.resolve("esbuild"));
  139. const installDir = path2.join(esbuildLibDir, "npm-install");
  140. fs2.mkdirSync(installDir);
  141. try {
  142. fs2.writeFileSync(path2.join(installDir, "package.json"), "{}");
  143. child_process.execSync(
  144. `npm install --loglevel=error --prefer-offline --no-audit --progress=false ${pkg}@${"0.16.3"}`,
  145. { cwd: installDir, stdio: "pipe", env }
  146. );
  147. const installedBinPath = path2.join(installDir, "node_modules", pkg, subpath);
  148. fs2.renameSync(installedBinPath, binPath);
  149. } finally {
  150. try {
  151. removeRecursive(installDir);
  152. } catch {
  153. }
  154. }
  155. }
  156. function removeRecursive(dir) {
  157. for (const entry of fs2.readdirSync(dir)) {
  158. const entryPath = path2.join(dir, entry);
  159. let stats;
  160. try {
  161. stats = fs2.lstatSync(entryPath);
  162. } catch {
  163. continue;
  164. }
  165. if (stats.isDirectory())
  166. removeRecursive(entryPath);
  167. else
  168. fs2.unlinkSync(entryPath);
  169. }
  170. fs2.rmdirSync(dir);
  171. }
  172. function applyManualBinaryPathOverride(overridePath) {
  173. const pathString = JSON.stringify(overridePath);
  174. fs2.writeFileSync(toPath, `#!/usr/bin/env node
  175. require('child_process').execFileSync(${pathString}, process.argv.slice(2), { stdio: 'inherit' });
  176. `);
  177. const libMain = path2.join(__dirname, "lib", "main.js");
  178. const code = fs2.readFileSync(libMain, "utf8");
  179. fs2.writeFileSync(libMain, `var ESBUILD_BINARY_PATH = ${pathString};
  180. ${code}`);
  181. }
  182. function maybeOptimizePackage(binPath) {
  183. if (os2.platform() !== "win32" && !isYarn()) {
  184. const tempPath = path2.join(__dirname, "bin-esbuild");
  185. try {
  186. fs2.linkSync(binPath, tempPath);
  187. fs2.renameSync(tempPath, toPath);
  188. isToPathJS = false;
  189. fs2.unlinkSync(tempPath);
  190. } catch {
  191. }
  192. }
  193. }
  194. async function downloadDirectlyFromNPM(pkg, subpath, binPath) {
  195. const url = `https://registry.npmjs.org/${pkg}/-/${pkg.replace("@esbuild/", "")}-${"0.16.3"}.tgz`;
  196. console.error(`[esbuild] Trying to download ${JSON.stringify(url)}`);
  197. try {
  198. fs2.writeFileSync(binPath, extractFileFromTarGzip(await fetch(url), subpath));
  199. fs2.chmodSync(binPath, 493);
  200. } catch (e) {
  201. console.error(`[esbuild] Failed to download ${JSON.stringify(url)}: ${e && e.message || e}`);
  202. throw e;
  203. }
  204. }
  205. async function checkAndPreparePackage() {
  206. if (ESBUILD_BINARY_PATH) {
  207. applyManualBinaryPathOverride(ESBUILD_BINARY_PATH);
  208. return;
  209. }
  210. const { pkg, subpath } = pkgAndSubpathForCurrentPlatform();
  211. let binPath;
  212. try {
  213. binPath = require.resolve(`${pkg}/${subpath}`);
  214. } catch (e) {
  215. console.error(`[esbuild] Failed to find package "${pkg}" on the file system
  216. This can happen if you use the "--no-optional" flag. The "optionalDependencies"
  217. package.json feature is used by esbuild to install the correct binary executable
  218. for your current platform. This install script will now attempt to work around
  219. this. If that fails, you need to remove the "--no-optional" flag to use esbuild.
  220. `);
  221. binPath = downloadedBinPath(pkg, subpath);
  222. try {
  223. console.error(`[esbuild] Trying to install package "${pkg}" using npm`);
  224. installUsingNPM(pkg, subpath, binPath);
  225. } catch (e2) {
  226. console.error(`[esbuild] Failed to install package "${pkg}" using npm: ${e2 && e2.message || e2}`);
  227. try {
  228. await downloadDirectlyFromNPM(pkg, subpath, binPath);
  229. } catch (e3) {
  230. throw new Error(`Failed to install package "${pkg}"`);
  231. }
  232. }
  233. }
  234. maybeOptimizePackage(binPath);
  235. }
  236. checkAndPreparePackage().then(() => {
  237. if (isToPathJS) {
  238. validateBinaryVersion(process.execPath, toPath);
  239. } else {
  240. validateBinaryVersion(toPath);
  241. }
  242. });