Inspect a Modern Front-End Toolchain and Module Graph
Practical 12 - Inspect a Modern Front-End Toolchain and Module Graph
Related: Chapter 12 · Lecture slides
Objective
Trace a front-end codebase through the complete modern delivery pipeline: from raw TypeScript source files and package resolution, through unbundled native ESM development with Hot Module Replacement (HMR), to production chunking, dynamic code splitting, tree shaking, and artifact auditing.
You will:
- Observe Development vs. Production Duality: Inspect the unbundled HTTP/2 module stream during local development and contrast it with production chunk bundling.
- Implement Dynamic Code Splitting: Isolate an expensive reporting and analytics module into a separate, on-demand asynchronous chunk (
import()). - Verify Chunk Separation: Mathematically verify that heavy charting and PDF dependencies are completely absent from the initial application entry bundle.
- Audit Source Maps and Asset Fingerprints: Verify reverse stack trace mapping from production minified bundles back to exact TypeScript source lines, and confirm content-hashing for cache busting.
- Enforce Environment Boundaries: Audit build-time environment replacements (
import.meta.env) to guarantee zero leakage of private credentials.
Workspace Setup
Initialize a clean TypeScript Vite project:
Configure vite.config.ts:
Stage-by-Stage Implementation
Stage 1: The Multi-Route Application Scaffold
Construct a modular application featuring a lightweight core view and a heavy, data-dense reporting dashboard.
In src/heavyAnalytics.ts, simulate a heavy charting engine:
In src/main.ts, establish a dynamic code-splitting boundary:
Stage 2: Observing Native ESM in Development
Start the development server:
- Open your browser’s Developer Tools and navigate to the Network tab.
- Load
http://localhost:5173. - Observe the Request Waterfall: Notice that Vite does not serve a bundled
bundle.js. Instead, you see individual HTTP requests for/src/main.ts,/src/style.css, etc. - Observe the Absence of
heavyAnalytics.ts: Verify thatheavyAnalytics.tsis not requested upon initial page load. - Click “Load Financial Audit (Heavy)”:
- Watch the Network panel.
- Observe the browser dynamically dispatching an HTTP GET request for
/src/heavyAnalytics.tsonly upon user interaction.
Stage 3: Production Bundling & Chunk Verification
Compile the application for production:
Examine the output emitted into dist/assets/:
Verification Requirement:
Open dist/assets/main-*.js in a text editor and search for HEAVY_CHART_CONFIG or generateMunicipalAuditReport.
- Expected Result: Neither string appears in
main-*.js. The heavy code has been strictly isolated into the lazy-loadedheavyAnalytics-*.jschunk. - Cache-Busting Check: Confirm that both emitted JavaScript files contain an 8-character content hash (
main-[hash].js), ensuring immutable CDN caching.
Stage 4: Source Map Reverse-Audit
Serve the production build locally:
- In DevTools, deliberately trigger an error inside
heavyAnalytics.ts: - Rebuild and reload
vite preview. - Open the browser Console.
- Verify that DevTools uses the
.mapfile to map the error directly back tosrc/heavyAnalytics.ts:line 4, rather than displaying minifiedheavyAnalytics-6d4b2e81.js:1:380.
Stage 5: Environment Variable Boundary Audit
Add an environment variable test:
Run npx vite build and inspect the output:
- Verify that
VITE_MUNICIPAL_API_URLwas replaced at build time with a plain string literal. - Verify that
DATABASE_SECRET(without theVITE_prefix) was stripped and replaced withundefined, preventing accidental client leakage.
Verification and Testing Matrix
| Test Case | Method / Tool | Expected Behavioral Guarantee |
|---|---|---|
| 1. Unbundled Dev Boot | Network Tab on vite dev | Zero bundle files; individual .ts modules served as native ESM. |
| 2. Dynamic Code Splitting | Initial page load vs. button click | heavyAnalytics.ts is only requested over the network after clicking the button. |
| 3. Chunk Isolation | Grep dist/assets/main-*.js | Zero occurrences of generateMunicipalAuditReport in entry bundle. |
| 4. Content Hashing | Change one line in heavyAnalytics.ts | Hash of heavyAnalytics-*.js changes; hash of main-*.js remains identical. |
| 5. Source Map Integrity | Throw Error in production preview | Stack trace points to TypeScript source line, not minified bundle. |
| 6. Secret Isolation | Grep dist/assets/*.js | No un-prefixed environment secrets exist in public client bundles. |
Optional Conceptual Extension: Educational Dependency Crawler
Implement a 30-line educational dependency graph crawler using Node’s fs and regular expressions to detect circular dependencies between modules:
Deliverables & Submission Checklist
-
vite.config.ts: Configured with source maps and chunk splitting. -
src/main.ts&src/heavyAnalytics.ts: Working dynamic import boundary. - Emitted
dist/assets/directory demonstrating isolated chunk sizes. - Verified source map stack trace demonstration.
- Completed Verification and Testing Matrix with recorded measurements.