Giter Site home page Giter Site logo

Comments (5)

wKovacs64 avatar wKovacs64 commented on May 21, 2024 3

I saw Kent run into this on stream too, but possibly for different reasons. The reason I ran into it was that I assigned process.env.NODE_ENV to another variable, which doesn't work apparently (I vaguely remember knowing that once upon a time, but don't recall the reasoning behind it).

πŸ‘πŸ» msw not in production bundle:

if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
  const { worker } = require('./mocks')
  worker.start()
}

πŸ‘ŽπŸ» msw in production bundle:

const isDevOrTest = process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development'
if (isDevOrTest) {
  const { worker } = require('./mocks')
  worker.start()
}

(edit: note the example works just fine unmodified, so that was an invalid report)

from examples.

thebuilder avatar thebuilder commented on May 21, 2024

@wKovacs64

if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
  const { worker } = require('./mocks')
  worker.start()
}

This allows the bundler to eliminate the dead code paths, since it will be transformed to:

if (false || false) { // Will never be true
  const { worker } = require('./mocks')
  worker.start()
}

If the if statement can never be true, it will be removed from the bundle. By assigning the ENV variable to another variable first, it would require runtime analysis of the code to determine if it can be eliminated.

const isDevOrTest = false ||Β false
if (isDevOrTest) { // Won't know the value `isDevOrTest` without running the code
  const { worker } = require('./mocks')
  worker.start()
}

from examples.

wKovacs64 avatar wKovacs64 commented on May 21, 2024

@thebuilder There we go, that sounds familiar. Thanks for the explanation!

from examples.

kettanaito avatar kettanaito commented on May 21, 2024

We need to add an automated test that asserts the msw is not bundled when built a full example like this.
Anybody interested in this can pick this up, otherwise I will push something similar in some time. Thanks everybody for discussion on this!

from examples.

alishi973 avatar alishi973 commented on May 21, 2024

Hey, any update on this? if it's not, can I pick this up?

from examples.

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.