Skip to main content

esbuild code

add esbuild to React Apptsximport * as esbuild from 'esbuild-wasm';
const App = () => {
const startService = async () => {
const service = await esbuild.startService({
work: true,
wasmURL: "/esbuild.wasm"
});
};
};

assign esbuild service to ref.current, for callable reference anywhere in app

const App = () => {
const ref = useRef<any\>();
const startService = async () => {
ref.current = await esbuild.startService({
worker: true,
wasmURL: "/esbuild.wasm",
});
};
};

assign build to result

const result = await ref.current.build({
entryPoints: ["index.js"],
bundle: true,
write: false,
plugins: [unpkgPathPlugin()],
define: {
"process.env.NODE_ENV": "'production'",
global: "window",
},
});

define: {"process.env.NODE_ENV": "'production'"} used to set production environment to production - else esbuild adds extra code checks for "process.env.NODE_ENV"

define: {global: window} used to replace any variable of "global" inside code with window - Bundler auto-substitute keyword

References