Skip to content

Commit

Permalink
fix: explicitly install the required esbuild binary
Browse files Browse the repository at this point in the history
This should help address #236.
  • Loading branch information
fwouts committed Feb 20, 2022
1 parent 4b19b1f commit ad38a52
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions loader/src/installer.ts
Original file line number Diff line number Diff line change
@@ -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<string, string> = {
"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;
Expand All @@ -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]: "*",
}
: {}),
},
})
);
Expand Down

0 comments on commit ad38a52

Please sign in to comment.