Part 16 / Topic 5 of 9
Module boundaries: a feature's public surface vs. its own internals
examples/05_module_boundaries/cart/ splits into two files with two different audiences. internal.js is private - its exports are deliberately never re-exported through index.js:
// examples/05_module_boundaries/cart/internal.js
export function formatRupees(amountInPaise) {
return `₹${(amountInPaise / 100).toFixed(2)}`;
}
export const TAX_RATE = 0.05;
index.js is cart's public API - the one thing another feature is supposed to import:
// examples/05_module_boundaries/cart/index.js
import { formatRupees, TAX_RATE } from "./internal.js";
export function getCartTotalLabel(amountInPaise) {
const withTax = amountInPaise * (1 + TAX_RATE);
return formatRupees(withTax);
}
wishlist/WishlistSummary.jsx, a different, sibling feature, imports only getCartTotalLabel from cart/index.js - never cart/internal.js directly:
// examples/05_module_boundaries/wishlist/WishlistSummary.jsx
import { getCartTotalLabel } from "../cart/index.js";
export function WishlistSummary({ totalInPaise }) {
return <p>If you bought everything in your wishlist: {getCartTotalLabel(totalInPaise)}</p>;
}
boundaries.test.jsx really proves both the computation (₹1,00,000 in paise, plus 5% tax, labels as "₹1050.00") and that WishlistSummary reaches that number with zero knowledge of TAX_RATE or formatRupees - it only ever depended on getCartTotalLabel's return value. Nothing in plain JavaScript stops WishlistSummary from importing cart/internal.js directly instead - there's no compiler enforcing "private" the way a language with real module visibility would. The boundary here is a convention, not a rule the runtime enforces, which is exactly why it's worth naming out loud: the actual bug index.js/internal.js prevents isn't a crash today, it's the hidden coupling that shows up the day cart changes how it rounds paise or renames TAX_RATE - a feature that only ever imported cart/index.js is unaffected, because index.js's one promise (getCartTotalLabel's return value, for a given input) never had to change; a feature that reached into cart/internal.js directly breaks immediately, in a file outside its own feature, and nothing in its own tests would have caught that risk before the break actually happened. Exercise 2 below asks you to write exactly this mistake, on purpose, and then fix it.
Done here? The practice set for Part 16 — including the solutions — is on the part page.