Skip to content

chore: exclude mockServiceWorker.js from production builds BED-7993#2651

Merged
elikmiller merged 3 commits intomainfrom
BED-7993-remove-msw-from-prod
Apr 20, 2026
Merged

chore: exclude mockServiceWorker.js from production builds BED-7993#2651
elikmiller merged 3 commits intomainfrom
BED-7993-remove-msw-from-prod

Conversation

@elikmiller
Copy link
Copy Markdown
Contributor

@elikmiller elikmiller commented Apr 14, 2026

Description

Excludes mockServiceWorker.js from production builds while keeping it available for development mode.

MSW (Mock Service Worker) requires the service worker file to be served from the public directory to function properly in development. However, this file should not be included in production builds as it's only needed for local mocking during development.

This PR adds a custom Vite plugin that automatically removes mockServiceWorker.js from the build output after Vite copies public assets to the dist folder.

Motivation and Context

Resolves BED-7993

The mockServiceWorker.js file was being included in production builds, adding unnecessary development-only code to the production bundle. This change ensures that MSW artifacts are excluded from production while maintaining full functionality in development mode.

How Has This Been Tested?

  • Built the BHCE UI with yarn build
  • Verified build output and dist folder contents (to ensure mockServiceWorker.js is not present)

Screenshots (optional):

N/A

Types of changes

  • Chore (a change that does not modify the application functionality)

Checklist:

Summary by CodeRabbit

  • Chores
    • Remove mock service worker from production builds to streamline output and avoid shipping test artifacts.
    • Ensure build reliably locates the output directory for consistent deployment artifacts.
    • Add a build-only step that detects and removes unwanted mock assets during production packaging.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 14, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 60b0c97e-998c-4776-95f7-b3eb5b1794fc

📥 Commits

Reviewing files that changed from the base of the PR and between f1447b1 and 84e018f.

📒 Files selected for processing (1)
  • cmd/ui/vite.config.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • cmd/ui/vite.config.ts

📝 Walkthrough

Walkthrough

Added a Vite build-only plugin that, on closeBundle, resolves the build output path (from env.BUILD_PATH or __dirname/dist) and removes mockServiceWorker.js if present; also added fs and Vite Plugin typing imports in cmd/ui/vite.config.ts.

Changes

Cohort / File(s) Summary
Vite Build Configuration
cmd/ui/vite.config.ts
Imported fs and Vite Plugin type, registered a new internal excludeMockServiceWorker(env: ReturnType<typeof loadEnv>): Plugin plugin in the plugins array. Plugin runs only during build and, in closeBundle, resolves output dir from env.BUILD_PATH or falls back to __dirname/dist and deletes mockServiceWorker.js if found. No exported API changes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: excluding mockServiceWorker.js from production builds, with the associated ticket reference BED-7993.
Description check ✅ Passed The description is comprehensive and follows the template structure, including all major sections: Description, Motivation and Context with ticket reference, How Has This Been Tested, Types of changes, and a completed Checklist.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch BED-7993-remove-msw-from-prod

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@elikmiller elikmiller changed the title build: exclude mockServiceWorker.js from production builds Exclude mockServiceWorker.js from production builds BED-7993 Apr 14, 2026
@elikmiller elikmiller self-assigned this Apr 14, 2026
@elikmiller elikmiller changed the title Exclude mockServiceWorker.js from production builds BED-7993 chore: exclude mockServiceWorker.js from production builds BED-7993 Apr 14, 2026
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@cmd/ui/vite.config.ts`:
- Around line 123-126: The cleanup hook currently reads process.env.BUILD_PATH
instead of the Vite-resolved output directory; update the plugin to capture
Vite's resolved build.outDir in configResolved (store it in a plugin-scoped
variable like resolvedOutDir) and then use that resolvedOutDir inside
closeBundle (instead of process.env.BUILD_PATH) to compute mockServiceWorkerPath
(e.g., path.resolve(__dirname, resolvedOutDir, 'mockServiceWorker.js')),
ensuring the file is removed from the actual build output directory configured
via loadEnv.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: ed185af1-c974-4437-83a3-f9be0ced933b

📥 Commits

Reviewing files that changed from the base of the PR and between 10b21bb and c238388.

📒 Files selected for processing (1)
  • cmd/ui/vite.config.ts

Comment thread cmd/ui/vite.config.ts Outdated
Copy link
Copy Markdown

@utafrali utafrali left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The custom Vite plugin is a clean, well-scoped solution and the closeBundle + apply: 'build' combination is the right approach. There is one real bug: the plugin resolves the output directory from process.env.BUILD_PATH while the build config uses Vite's loadEnv, so any BUILD_PATH set in a .env file would be invisible to the plugin, causing a silent no-op deletion.

Comment thread cmd/ui/vite.config.ts Outdated
Copy link
Copy Markdown
Contributor

@zinic zinic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upon testing this drops the file on disk but when I run the service I can no longer request it from the API. This appears to be working as intended.

@elikmiller elikmiller merged commit 17b6fbd into main Apr 20, 2026
13 checks passed
@elikmiller elikmiller deleted the BED-7993-remove-msw-from-prod branch April 20, 2026 23:52
@github-actions github-actions bot locked and limited conversation to collaborators Apr 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants