Chapter 3 ยท CORE

Optimization Heuristics

๐Ÿ“„ 03_optimization_heuristics.md ๐Ÿท Core

Chapter 3: Optimization Heuristics

Welcome back! In Chapter 2: Package Classification Strategies, we acted as "Recycling Sorters," separating raw Framework packages (Hazardous) from Standard packages (Safe).

We learned that Framework Packages must be excluded from Vite's pre-bundling process to avoid errors. However, this creates a new problem.

The Motivation: The "Deep Dependency" Problem

Imagine this scenario:

  1. You are using a Framework Library called my-ui-kit.
  2. my-ui-kit is marked as excluded from optimization (because it has raw code).
  3. Inside my-ui-kit, it imports a standard library like lodash or axios.

When the browser tries to run my-ui-kit, it encounters the import for lodash. Since my-ui-kit was excluded, Vite didn't pre-bundle its dependencies either.

The Problem: lodash is often written in an older format called CommonJS (CJS). Browsers generally cannot understand CJS. If the browser tries to read raw lodash, it crashes.

The Solution: We need a logic layer that scans these nested standard dependencies. If they are "Old Fashioned" (CJS), we must force Vite to bundle them, converting them to the modern format (ESM).

The Analogy: Airport Security

Think of the browser environment as a modern Airplane. It only accepts luggage that fits specific dimensions (ESM format).

  1. Framework Packages: These are VIP passengers. We wave them through a special gate (Plugins).
  2. Standard Packages: This is the luggage.

Optimization Heuristics act as the Security Scanner. It scans every piece of luggage (standard package). If it sees an "Old Fashioned Trunk" (CJS), it tags it for "Special Handling" (Pre-bundling/Conversion) so it can fit on the plane.

How to Use It

This logic is encapsulated in the function pkgNeedsOptimization.

You likely won't call this manually if you are using the main crawlFrameworkPkgs function, as it runs automatically in the background. However, understanding it helps you debug why certain packages are added to optimizeDeps.include.

If you were to use it manually:

import { pkgNeedsOptimization } from 'vitefu';
import path from 'path';

// 1. Get package data
const pkgJson = { main: "index.js" }; // Simulating a CJS package
const pkgPath = path.resolve("./node_modules/my-lib/package.json");

// 2. Scan it
const needsHelp = await pkgNeedsOptimization(pkgJson, pkgPath);

if (needsHelp) {
    console.log("Tag this package for bundling!");
}

Internal Implementation: The Scanner Logic

How does the scanner know if a package is an "Old Fashioned Trunk" (CJS) or "Modern Luggage" (ESM)? It looks at the package.json tags.

High-Level Flow

sequenceDiagram participant C as Crawler participant H as Heuristic Logic participant P as Package JSON C->>H: Check Package H->>P: Read Fields (module, exports, main) alt Has "module" or "exports" H->>C: Return FALSE (It is Modern ESM) else Main file is .js / .cjs H->>C: Return TRUE (It is likely CJS) else Has implicit index.js H->>C: Return TRUE (It is Implicit CJS) end

Code Walkthrough

Let's break down the pkgNeedsOptimization function found in src/index.js. It performs three specific checks, in order.

Check 1: Is it already Modern? (ESM)

Modern packages usually have a module field or an exports field in their package.json. If these exist, Vite can usually handle them without forcing optimization.

// src/index.js
export async function pkgNeedsOptimization(pkgJson, pkgJsonPath) {
  // If it explicitly says it has modules or exports
  if (pkgJson.module || pkgJson.exports) {
    return false; // It fits in the overhead bin!
  }
  // ... continue checks

Check 2: Does "Main" point to a JS file?

If a package only has a main field, it is traditionally CommonJS. We check the file extension.

  // src/index.js (continued)
  if (pkgJson.main) {
    const entryExt = path.extname(pkgJson.main)
    // No extension, .js, or .cjs usually means CommonJS logic
    return !entryExt || entryExt === '.js' || entryExt === '.cjs'
  }

Why optimize this? Because .js in Node land is often CJS. We optimize it to be safe.

Check 3: The Implicit Entry

Some very old packages don't even have a main field. They just rely on index.js existing in the root. This is almost guaranteed to be CJS.

  // src/index.js (continued)
  try {
    // Check if index.js exists in the folder
    await fs.access(path.join(path.dirname(pkgJsonPath), 'index.js'))
    return true // Found implicit CJS entry
  } catch {
    return false // No entry found, ignore it
  }
}

What Happens Next?

If pkgNeedsOptimization returns true:

  1. The crawler takes the name of that package.
  2. It adds it to the optimizeDeps.include list in the Vite config.
  3. Vite sees this list, grabs the package, and runs it through esbuild.
  4. The output is a clean ESM file that the browser can read.

Summary

In this chapter, we learned:

  1. The Risk: Standard libraries hidden inside excluded Framework packages might be CJS, which breaks in the browser.
  2. The Scanner: pkgNeedsOptimization inspects package.json.
  3. The Rules:

Now that we have successfully identified exactly which packages need to be included, excluded, or externalized, we need a way to merge these results with the user's existing Vite configuration without breaking anything.

Next Chapter: Configuration Matchers


Generated by Code IQ