Giter VIP home page Giter VIP logo

Comments (1)

evanw avatar evanw commented on April 28, 2024 16

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.

from esbuild.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.