Structuring browser JavaScript with AMD
As browser applications grow, global script order becomes difficult to reason about. AMD loaders such as RequireJS make dependencies explicit, but production delivery still requires a planned build.
Define narrow modules
Give each module a clear return value and declare its dependencies rather than reading mutable globals. Keep DOM startup separate from reusable data and formatting logic so modules can be tested without a complete page.
Optimise for production
Use the loader optimiser to combine stable dependency groups and minify the result. Preserve source maps or unminified release artefacts for diagnosis, and verify that dynamic module paths are included.
Control initialisation
Load only the entry point required by the template and initialise after its DOM is available. Watch for plugins that conceal synchronous work or create serial requests after the main bundle has completed.
A dependency graph that works through dozens of local requests may still need bundling to perform acceptably over a high-latency connection.