Quick answer
Give each browser context an explicit entry point, then share only code that can safely run in all of its consumers. A popup, service worker, and content script have different lifetimes and available APIs. A single global application object obscures those differences even if it lives in a neatly named folder.
Keep the installed package separate from the source tree. src/worker/index.ts may become dist/worker.js; the manifest must name worker.js relative to dist. The browser validates the package it loads, not your source layout. This article addresses ownership and build boundaries; the beginner tutorial covers the first working extension, and the manifest guide covers individual keys.
Practical implementation steps
Separate entry points from reusable logic
Let popup/index mount the UI, worker/index register listeners, and content/index attach page behavior. Keep DOM imports out of the worker dependency graph. Put validation and transformations in ordinary modules with no window, document, or implicit browser calls. The content script can then reuse a report formatter without also importing popup rendering.
Own state by durability
A selected tab in the popup is view state. A saved user preference belongs in extension storage. A page’s original text belongs to the content context that changed it and must be reconciled with later site edits. Passing all three through one generic store creates stale ownership and unnecessary message traffic.
Make the build contract visible
Compile all entry points, copy HTML and images, and generate or copy the manifest into one output root. Verify that every referenced file exists there. Hashed asset names are useful for ordinary web builds but a manifest cannot guess them; either reserve stable entry names or generate its references from the build output.
Review changes by boundary
A new capability should show up in the entry point, manifest permission, message contract, and product explanation together. If a small UI change requires every context to import the same browser adapter, reconsider that adapter’s ownership. LocaleStress offers a concrete product distinction: temporary page changes, exported reports, and optional saved runs are different responsibilities. Its public feature record does not establish a particular source-folder architecture.
Implementation example
A source layout can remain small while keeping those boundaries explicit:
src/
popup/index.ts
worker/index.ts
content/index.ts
shared/messages.ts
shared/report.ts
adapters/settings.ts
static/
popup.html
icons/
manifest.json
dist/
manifest.json
popup.html
popup.js
worker.js
content.js
icons/
Use a discriminated message contract rather than sending a method name and arbitrary arguments into privileged code:
export type Request =
| { type: "settings/read" }
| { type: "report/save"; report: { id: string; text: string } };
export function isSettingsRead(value: unknown): value is { type: "settings/read" } {
return typeof value === "object" && value !== null
&& "type" in value && value.type === "settings/read";
}
The type helps trusted callers compile; the validator protects the receiving runtime. Add validation for each accepted message, including report size and allowed fields. Keep the report formatter pure so a unit test can check its output without pretending to emulate Chrome. Shared code is copied or bundled into each context; importing it does not create shared memory.
Tradeoffs and alternatives
Feature folders become useful when several contexts participate in the same stable feature. Keep their context-specific adapters explicit: reports/worker-save and reports/content-collect communicate ownership better than a universal reports/service. A monorepo can share pure libraries across several extensions, but each product still needs an independently inspectable manifest and artifact.
Bundling reduces file-management work but can accidentally pull a DOM library into the worker. Review entry-point dependency graphs and output imports before optimizing chunk size. For a small extension, stable output names and a transparent copy step may be easier to maintain than a sophisticated build plugin.
Common mistakes
- Treating a shared module singleton as shared memory between contexts.
- Importing UI helpers that read
documentat module initialization into the worker. - Using a broad message dispatcher that lets a content script select privileged API methods.
- Shipping
srcwhile testingdist, or leaving manifest paths tied to a development server. - Moving every file into a feature folder without documenting which context runs it.
Production checklist
- Every manifest entry resolves inside the output directory.
- Worker imports contain no DOM initialization or remote executable code.
- Shared contracts have receiving-side runtime validation.
- Durable state and transient page state have separate owners.
- A clean build removes obsolete chunks and unused copied assets.
- The release ZIP is produced from the inspected output root.
FAQ
Where should browser API wrappers live?
Beside the context that owns their capability. A storage adapter can serve several extension pages; a page-DOM adapter should remain in the content boundary. Share interfaces when useful without forcing every implementation into one module.
When should I split a shared package?
When a stable pure contract has several real consumers and independent tests. Splitting folders early does not remove coupling. First remove browser globals and feature-specific assumptions from the reusable code.
What is the next useful review?
Follow one request from UI entry to message validation, storage or page operation, and result rendering. Then inspect the built files for that path. That exposes architectural mistakes more reliably than debating folder names.
Primary sources
Related guides and examples
Need this implemented in a real product?
Browser tools with a clear purpose, focused permissions, and a maintainable Manifest V3 architecture.