completefrontend

Write the codeMedium

Write a function that splits a route map into chunks.

Leaf and Kettle's build knows every module each route's page needs, its whole import graph flattened into a list, and each module's size. Write splitRoutes(routes, sizes, options), a small model of a bundler's chunk splitting: modules that enough routes use go into one shared chunk, which a visitor downloads once and the cache keeps across pages, and each route gets a chunk with the rest. routes maps each route to the modules it uses, in the order it imports them, and a route may list a module more than once. sizes maps every module to its size in bytes.

A module is shared when at least options.minRoutes routes use it (2 when it isn't given); a route that lists a module twice still counts once. The shared chunk is { modules, size }: every shared module, sorted by name the way sort() sorts strings, and their sizes added up. When there are no shared modules, or their sizes add up to less than options.minSize bytes (0 when it isn't given), there's no shared chunk: shared is null and every route keeps all its modules.

Each route's chunk is { modules, size, firstLoad }: the route's modules that aren't in the shared chunk, each once, in the order the route first lists them; their sizes added up; and firstLoad, the bytes a first visit to that route downloads, which is its own chunk plus the whole shared chunk when the route uses at least one shared module. Return { shared, routes }, with routes keyed like the input.

splitRoutes(
  {
    "/teas": ["react", "router", "tea-card", "price"],
    "/cart": ["react", "router", "cart", "price"],
    "/account": ["react", "router", "account"],
  },
  { react: 60000, router: 12000, "tea-card": 8000, price: 2000, cart: 15000, account: 9000 },
);
// {
//   shared: { modules: ["price", "react", "router"], size: 74000 },
//   routes: {
//     "/teas": { modules: ["tea-card"], size: 8000, firstLoad: 82000 },
//     "/cart": { modules: ["cart"], size: 15000, firstLoad: 89000 },
//     "/account": { modules: ["account"], size: 9000, firstLoad: 83000 },
//   },
// }
Write this: solution.js
function splitRoutes(routes, sizes, options = {}) {
  // Today: no shared chunk, so every route carries everything it uses.
  const result = { shared: null, routes: {} };
  for (const [route, modules] of Object.entries(routes)) {
    const size = modules.reduce((sum, name) => sum + sizes[name], 0);
    result.routes[route] = { modules, size, firstLoad: size };
  }
  return result;
}
The tests your code must pass
  • modules two routes use form one shared chunk, sorted by name, and each route keeps the rest in its own order
  • a module one route lists twice counts as one route, and its size is added once
  • with minRoutes 3, a module only two routes use stays in each of them
  • no shared chunk when the shared modules add up to less than minSize, and one at exactly minSize
  • a route that uses nothing shared doesn't download the shared chunk
  • a route made only of shared modules gets an empty chunk
  • nothing shared, or no routes at all, gives no shared chunk

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…