Grafana plugin bundle size optimisation
module.js is the render-blocking entry point for every Grafana app plugin. The smaller it is, the less impact the plugin has on Grafana's overall startup time. A well-split plugin should have a module.js under ~200 KB that contains nothing but lazy-loaded wrappers — all feature code loads on demand.
Target: ~15–25 JS chunks total. Fewer means too little splitting; far more (50+) means over-engineering.
Risk levels
Not all splitting opportunities carry the same risk. Apply them in this order:
When in doubt, stop after Priority 2. Routes alone typically reduce module.js by 95%+.
Step 1: Add bundle size CI reporting (recommended)
Add the grafana/plugin-actions/bundle-size action to get automatic bundle size comparison comments on every PR. This posts a table showing entry point size changes, file count diffs, and total bundle impact.
Root-level plugins (plugin at repo root):
Subdirectory plugins (e.g. plugin/ in a monorepo):
The action's install step runs at the repo root and cannot find yarn.lock in a subdirectory. Work around this by installing deps yourself and symlinking to root:
How it works: On push to main, builds and uploads a baseline artifact. On PRs, compares against it and posts a diff comment. Use workflow_dispatch to generate the first baseline.
Reference: grafana-k8s-plugin workflow
Step 2: Detect plugin context
Record the baseline. A pre-split plugin commonly has a module.js of 1–3 MB with no other JS chunks.
Step 3: Check and update create-plugin
The @grafana/create-plugin tool controls .config/webpack/, .config/jest/, and other build scaffolding. Updating it often unlocks faster SWC compilation and better chunk output.
After updating, review the diff (especially .config/webpack/webpack.config.ts) and run a test build. If the plugin has a top-level webpack.config.ts that webpack-merges the base config, review the merge for conflicts.
Step 4: Analyse the codebase — find what to split
Do not start implementing until you have read all of these.
Key rule: If a file is imported by module.ts directly (even transitively), it ends up in module.js. Everything reachable from a lazy boundary becomes its own chunk.
Step 5: Implement splits — in priority order
Named vs default exports:
React.lazy()requires adefaultexport. Most Grafana plugin components use named exports — use.then()to re-map:
Priority 1: module.tsx (highest impact, always do this first)
If the entry point is module.ts, rename it: git mv src/module.ts src/module.tsx
Make module.tsx import nothing from feature code except through lazy():
Key details:
import typefor props prevents webpack from following the import into the eager bundle- Use
new AppPlugin<JsonData>()if App usesAppRootProps<JsonData>— without the generic,setRootPage()type won't match - Remove any
App as unknown as ComponentClass<AppRootProps>cast — the lazy wrapper is a valid function component
Expected impact: module.js drops from MB range to ~50–200 KB.
Singletons (e.g. Faro): If module.ts has export const faro = initializeFaro(), do NOT keep it as a top-level import. Extract it to src/faro.ts, update all internal imports from '*/module' → '*/faro', then use the dynamic initFaro() pattern above.
Priority 2: Route-based splitting in App.tsx
Bypass barrel files: Target the actual component file in the import(), not an index.ts barrel that re-exports multiple things:
Priority 3: Extension components
Each extension should export default its component. Use fallback={null} for extensions that load quickly:
Surgical split: If an extension wrapper must stay eager in module.tsx, lazy-load the heavy component it renders:
Priority 4: Component registries and tab panels
For arrays of objects containing React components (e.g. tab panels), lazy-load each entry. Critical: ensure a <Suspense> boundary exists where the component renders.
For datasource plugins (setConfigEditor, setQueryEditor, VariableSupport, AnnotationSupport), see references/datasource-plugins.md [blocked].
Step 6: Group related chunks if over-splitting
If the build produces more than ~25 JS files, use webpack magic comments:
One webpackChunkName per logical feature area. Don't group unrelated pages.
Step 7: Measure and verify
Step 8: Test the running plugin
- Open the plugin in a Grafana instance
- Navigate to every route — each triggers a new chunk download
- DevTools → Network → JS: confirm lazy chunks load on navigation, not all upfront
- Check Console for errors
- Test any
exposeComponentextensions from other Grafana apps
For troubleshooting common issues, see references/troubleshooting.md [blocked].
References
- grafana-collector-app — app plugin reference implementation
- grafana/plugin-actions — official Grafana plugin CI actions
- Web.dev — code splitting with lazy and Suspense
- SurviveJS — webpack code splitting
- webpack magic comments


