I've tried to use esbuild. It is impressively fast :)
But i faced issue with env variables.
Once i run compiled code i got following error:
Uncaught ReferenceError: process is not defined
I looked into line were error occurred
"use strict";
if (process.env.NODE_ENV === "production") {
module.exports = require(1172 /* ./cjs/react.production.min.js */);
} else {
module.exports = require(1171 /* ./cjs/react.development.js */);
}
The way this works with esbuild is to use --define like this:
esbuild src/index.js--outfile=dist/index.js --bundle '--define:process.env.NODE_ENV="production"'
This replaces all instances of process.env.NODE_ENV with the string "production". I have also included enough dead code elimination logic in esbuild to be able to handle cases like this. The code above will turn into this:
"use strict";
if (true) {
module.exports = require(1172 /* ./cjs/react.production.min.js */);
} else {
module.exports = null;
}
The require() call in the false branch turns into null because esbuild knows it's never executed, and should not be included in the bundle. If you also pass --minify that gives esbuild permission to compact the structure of your code, so it will then turn into this:
module.exports = require(1172 /* ./cjs/react.production.min.js */);
The --define feature can replace any identifier or member expression in your code, not just process.env.NODE_ENV.
Most helpful comment
The way this works with esbuild is to use
--definelike this:This replaces all instances of
process.env.NODE_ENVwith the string"production". I have also included enough dead code elimination logic in esbuild to be able to handle cases like this. The code above will turn into this:The
require()call in the false branch turns intonullbecause esbuild knows it's never executed, and should not be included in the bundle. If you also pass--minifythat gives esbuild permission to compact the structure of your code, so it will then turn into this:The
--definefeature can replace any identifier or member expression in your code, not justprocess.env.NODE_ENV.