Is there a way to exclude files from the bundle in production? #9957
-
I have some mock data which I use for testing in dev but it's getting bundled with the rest of my code in the production build. Is there a way to avoid bundling mock files in production? // in file which imports a mock server
if (process.env.NODE_ENV === "development")) {
const { worker } = require("../mocks/browser");
worker.start(); // in file which starts a mock server
import { setupWorker } from "msw";
import { handlers } from "./handlers"; // handlers import the mock data
// This configures a Service Worker with the given request handlers.
export const worker = setupWorker(...handlers); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Use code splitting https://create-react-app.dev/docs/code-splitting to keep the part you don't want to load in production, in it's own chuck that only loads when the conditional allows it to. |
Beta Was this translation helpful? Give feedback.
-
Thank you that is the answer. Changing the require to an import solved the issue. if (process.env.NODE_ENV === "development")) {
const { worker } = import("../mocks/browser").then(({worker}) => { worker.start()}); |
Beta Was this translation helpful? Give feedback.
-
When I tested it, it worked in development but it didn't in production as intended. |
Beta Was this translation helpful? Give feedback.
Use code splitting https://create-react-app.dev/docs/code-splitting to keep the part you don't want to load in production, in it's own chuck that only loads when the conditional allows it to.