Is there a way to exclude files from the bundle in production? #9957
Answered
by
ChristianMurphy
lukesmurray
asked this question in
Q&A
|
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); |
Answered by
ChristianMurphy
Nov 12, 2020
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. |
0 replies
Answer selected by
lukesmurray
|
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()}); |
5 replies
When I tested it, it worked in development but it didn't in production as intended. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.