Production builds
Diagnostic catalogs contain user-facing text. That text is useful during development, but you often do not want report-only diagnostics in a production bundle.
nosticsStrip is a build-time plugin for that.
Setup
Vite:
import { nosticsStrip } from 'nostics/unplugin/strip-transform'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [nosticsStrip.vite()],
})
Rolldown through tsdown:
import { nosticsStrip } from 'nostics/unplugin/strip-transform'
import { defineConfig } from 'tsdown'
export default defineConfig({
entry: ['src/index.ts'],
plugins: [nosticsStrip.rolldown()],
})
The plugin is built with unplugin, so it also exposes .rollup(), .webpack(), .rspack(), .esbuild(), and .farm().
What it changes
First, it marks defineDiagnostics() calls as pure.
const diagnostics = /*#__PURE__*/ defineDiagnostics({
codes: {
NUXT_B2011: { why: 'Plugin mode conflicts with file suffix.' },
},
})
Second, it wraps bare diagnostic expression statements with a production guard.
diagnostics.NUXT_B2011()
becomes:
process.env.NODE_ENV !== "production" && diagnostics.NUXT_B2011()
In a production build, bundlers can remove the call. Once the calls are gone, the diagnostic catalog usually becomes unused too.
What stays
The plugin only strips report-only expression statements.
These can be stripped:
diagnostics.NUXT_B2011()
condition && diagnostics.NUXT_B5001()
These stay:
throw diagnostics.NUXT_B2011()
return diagnostics.NUXT_B2011()
const diagnostic = diagnostics.NUXT_B2011()
fn(diagnostics.NUXT_B2011())
This matters. Throwing a diagnostic is part of your runtime behavior, so the plugin leaves it alone.
Cross-file tracking
The plugin can track a directly exported diagnostics object across one relative import.
export const diagnostics = defineDiagnostics({
codes: {
NUXT_B2011: { why: 'Plugin mode conflicts with file suffix.' },
},
})
import { diagnostics } from './diagnostics'
diagnostics.NUXT_B2011()
Keep this simple for best results:
- Use relative imports.
- Export the result of
defineDiagnostics()directly. - Avoid factory wrappers for exported diagnostics objects.
- Avoid relying on deep barrel re-exports for stripping.
Options
nosticsStrip.vite({
packageName: 'my-diagnostics-wrapper',
})
| Option | Default | Use |
|---|---|---|
packageName | 'nostics' | Detect imports from another package name |
Check the output
After a production build, search the bundle:
rg 'NUXT_B2011|Plugin mode conflicts' dist/
Report-only diagnostics should be gone. Diagnostics used in throw, return, assignments, or function arguments can remain.