Skip to content

How can I prevent mockServiceWorker.js from being included in my Vite prod build? #2210

Answered by jasikpark
jasikpark asked this question in Q&A
Discussion options

You must be logged in to vote

Found a simple solution for our purposes a bit back:

if (typeof process.env.VITE_MOCK_API !== 'undefined') {
    plugins.push(
      viteStaticCopy({
        targets: [
          {
            // Only serve MSW when API is mocked.
            src: 'test-setup/mockServiceWorker.js',
            dest: '/',
          },
        ],
      })
    );
  }

We just copy from a regular path into the dist only in dev.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@jasikpark
Comment options

Answer selected by jasikpark
@jasikpark
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants