Skip to content

Commit

Permalink
feat: add replaceEsbuild - script to replace esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
aiji42 committed Mar 15, 2022
1 parent 97f661c commit 96be0be
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
33 changes: 7 additions & 26 deletions src/bin/index.ts
@@ -1,31 +1,12 @@
#!/usr/bin/env node
import { execSync } from "node:child_process";
import { replaceEsbuild } from "../replace";

const isOverridden = require
.resolve("esbuild")
.endsWith("remix-esbuild-override/dist/index.js");
if (isOverridden) {
try {
replaceEsbuild();
} catch (e) {
if (e instanceof Error)
console.warn("💽 Skipped esbuild replacement:", e.message);
console.log(
"esbuild has already been replaced. Your custom config can be used to build for Remix💡"
);
} else {
const [originalEsbuildPath] =
require.resolve("esbuild").match(/^.*\/esbuild/) ?? [];
const [linkedEsbuildPath] =
require
.resolve("remix-esbuild-override")
.match(/^.*\/remix-esbuild-override/) ?? [];

execSync(
`mv ${originalEsbuildPath} ${originalEsbuildPath.replace(
/esbuild$/,
"esbuild-org"
)}`
);

execSync(`ln -s ${linkedEsbuildPath} ${originalEsbuildPath}`);

console.log(
"Replaced esbuild. Your custom config can be used to build for Remix💡"
"💽 Please read https://github.com/aiji42/remix-esbuild-override#if-postinstall-fails"
);
}
1 change: 1 addition & 0 deletions src/index.ts
@@ -1,4 +1,5 @@
export * from "esbuild-org";
export { replaceEsbuild } from "./replace";
import { build as buildOrg, BuildOptions } from "esbuild-org";
import { resolve } from "path";
import { AppConfig as AppConfigOrg } from "@remix-run/dev";
Expand Down
32 changes: 32 additions & 0 deletions src/replace.ts
@@ -0,0 +1,32 @@
import { execSync } from "node:child_process";

export const replaceEsbuild = () => {
const isOverridden = require
.resolve("esbuildあ")
.endsWith("remix-esbuild-override/dist/index.js");
if (isOverridden) {
console.log(
"💽 esbuild has already been replaced. Your custom config can be used to build for Remix"
);
} else {
const [originalEsbuildPath] =
require.resolve("esbuild").match(/^.*\/esbuild/) ?? [];
const [linkedEsbuildPath] =
require
.resolve("remix-esbuild-override")
.match(/^.*\/remix-esbuild-override/) ?? [];

execSync(
`mv ${originalEsbuildPath} ${originalEsbuildPath.replace(
/esbuild$/,
"esbuild-org"
)}`
);

execSync(`ln -s ${linkedEsbuildPath} ${originalEsbuildPath}`);

console.log(
"💽 Replaced esbuild. Your custom config can be used to build for Remix"
);
}
};

0 comments on commit 96be0be

Please sign in to comment.