diff --git a/loader/src/installer.ts b/loader/src/installer.ts index e078f3f5306..fb4976cbe19 100644 --- a/loader/src/installer.ts +++ b/loader/src/installer.ts @@ -1,7 +1,31 @@ import execa from "execa"; import { mkdir, pathExists, readFile, writeFile } from "fs-extra"; +import os from "os"; import path from "path"; +// Source: https://github.com/evanw/esbuild/blob/bf341f7104b373d85061c31ebb00efc6f9a8bf5a/lib/npm/node-platform.ts +const esbuildPackages: Record = { + "win32 arm64 LE": "esbuild-windows-arm64", + "win32 ia32 LE": "esbuild-windows-32", + "win32 x64 LE": "esbuild-windows-64", + "android arm64 LE": "esbuild-android-arm64", + "darwin arm64 LE": "esbuild-darwin-arm64", + "darwin x64 LE": "esbuild-darwin-64", + "freebsd arm64 LE": "esbuild-freebsd-arm64", + "freebsd x64 LE": "esbuild-freebsd-64", + "linux arm LE": "esbuild-linux-arm", + "linux arm64 LE": "esbuild-linux-arm64", + "linux ia32 LE": "esbuild-linux-32", + "linux mips64el LE": "esbuild-linux-mips64le", + "linux ppc64 LE": "esbuild-linux-ppc64le", + "linux riscv64 LE": "esbuild-linux-riscv64", + "linux s390x BE": "esbuild-linux-s390x", + "linux x64 LE": "esbuild-linux-64", + "netbsd x64 LE": "esbuild-netbsd-64", + "openbsd x64 LE": "esbuild-openbsd-64", + "sunos x64 LE": "esbuild-sunos-64", +}; + export async function isInstalled(options: { packageName: string; packageVersion: string; @@ -25,11 +49,23 @@ export async function install(options: { ); const packageJsonPath = path.join(options.installDir, "package.json"); await mkdir(options.installDir, { recursive: true }); + // Explicitly install the binary version needed by esbuild in case NPM uses a different + // architecture or decides to skip it for any reason. + // See https://github.com/fwouts/previewjs/issues/236. + const esbuildPlatformKey = `${ + process.platform + } ${os.arch()} ${os.endianness()}`; + const esbuildPlatformPackage = esbuildPackages[esbuildPlatformKey]; await writeFile( packageJsonPath, JSON.stringify({ dependencies: { [options.packageName]: options.packageVersion, + ...(esbuildPlatformPackage + ? { + [esbuildPlatformPackage]: "*", + } + : {}), }, }) );