031
LVL 02 — CIRCUIT BREAKER SESSION 031 DAY 31

ES MODULES

🎫 PIXELCRAFT-019
🔧 Refactor | 🟡 Medium | Priority: 🟠 High

app.js is 1800 lines. Everything in one file. Split into modules: filters, canvas, ui, utils, state.
CONCEPTS.UNLOCKED
📦
ES Modules: import / export
JavaScript's native module system. Each file is a module with its own scope. export makes things available to other files. import brings them in.
{ }
Named Exports
export function grayscale() {...}import { grayscale } from './filters.js'. Multiple named exports per file. Import only what you need.
Default Exports
export default class FilterManagerimport FilterManager from './FilterManager.js'. One default per file. No curly braces on import.
🔒
Module Scope
Each file has its own scope — no more global variable conflicts. A variable in filters.js can't accidentally overwrite one in canvas.js. Isolation by default.
📂
Folder Structure Conventions
Group files by responsibility: scripts/, styles/, assets/. Within scripts/, split by concern: filters, canvas operations, UI, state, utilities. Every team follows conventions.
🔄
Circular Dependencies
A imports from B, B imports from A → trouble. Resolve by extracting shared code into a third module (e.g., utils.js) that both import from.
HANDS-ON.TASKS
01
Plan the Split
src/scripts/ ├── app.js ← Entry point (thin orchestrator) ├── filters.js ← All filter functions ├── canvas.js ← Canvas operations (load, draw, export) ├── ui.js ← DOM manipulation, event wiring ├── state.js ← Settings, filter state, persistence └── utils.js ← clamp(), showToast(), generateExportName()
app.js becomes a thin orchestrator: it imports from each module and wires everything together. No business logic in app.js — just imports and initialization.
02
Extract Each Module with export
// filters.js export function grayscale(r, g, b) { ... } export function brightness(r, g, b, amount) { ... } export function clamp(value) { ... }

Move each group of related functions into its own file. Add export in front of every function that other modules need.

03
Import in app.js
import { grayscale, brightness, contrast, invert } from './filters.js'; import { loadImageToCanvas, exportCanvas } from './canvas.js'; import { setupEventListeners } from './ui.js'; import { loadSettings, saveSettings } from './state.js';
Named imports use curly braces and must match the export names exactly. The paths are relative to the current file — './' means "same directory."
04
Update index.html
<script type="module" src="scripts/app.js"></script>

type="module" tells the browser this is an ES Module. Without it, import/export won't work. Modules are automatically deferred (no need for defer attribute).

05
Verify & Resolve Circular Dependencies

Verify nothing broke — all features work as before.

If you find circular dependencies (A imports B, B imports A), extract the shared code into utils.js or a new shared module.

06
Close the Ticket
git switch -c refactor/PIXELCRAFT-019-modules git add src/scripts/ src/index.html git commit -m "Split app.js into ES Modules (PIXELCRAFT-019)" git push origin refactor/PIXELCRAFT-019-modules # PR → Review → Merge → Close ticket ✅
CS.DEEP-DIVE

Modularity and separation of concerns.

Arguably the most important principle in software engineering.

// Every well-designed system is modular:

CPUs          → separate ALU, cache,
                and control unit
Operating sys  → separate kernel,
                drivers, user space
The web itself → separate HTML, CSS, JS

// Modules let teams work in parallel,
// reduce bugs, and make systems maintainable.

// The mantra:
"Low coupling, high cohesion"
// Each module depends minimally on others
// but is internally focused.
"Module Architecture"
[A] Draw a dependency graph: which module imports from which? Are there any modules that import from too many others? Can you reduce the coupling?
[B] Add an index.js barrel file that re-exports everything: export { grayscale, brightness } from './filters.js'. Compare the import experience.
[C] Open the Network tab in DevTools. How many script requests does the browser make? Research how bundlers (Vite, webpack) solve this for production.
REF.MATERIAL
ARTICLE
Mozilla Developer Network
Complete guide to ES Modules: import/export syntax, default vs named, dynamic imports, and module loading behavior.
MODULESMDNESSENTIAL
VIDEO
Fireship
Rapid overview of ES Modules: why they exist, import/export syntax, and how they compare to CommonJS.
MODULESQUICK
VIDEO
Web Dev Simplified
Practical walkthrough: splitting a project into modules, named vs default exports, and common mistakes.
MODULESPRACTICAL
ARTICLE
javascript.info
Interactive deep dive into modules: core features, module scope, import.meta, dynamic imports, and module-level execution.
MODULESINTERACTIVE
ARTICLE
Wikipedia
The foundational software engineering principle: each component addresses a separate concern. History, theory, and applications across all engineering.
ARCHITECTUREPRINCIPLESCS
// LEAVE EXCITED BECAUSE
The codebase went from one 1800-line file to 6 focused modules. Each file has a clear purpose. You can find anything in seconds. This is clean architecture.