agentleFS
MaheshAwasareMaheshAwasare/claude-skills-pro/skills/webpack-to-vite/SKILL.md

webpack-to-vite

Skill1 starsChanged 7 months agoMITView on GitHub

Migrate from Webpack to Vite — config translation, plugin equivalents, dev-server differences (HMR), env var handling, asset imports, the issues unique to React/Vue/Svelte migrations. Use when an existing webpack project's dev experience is too slow or maintenance burden too high.

  • Reads credentials
  • Installs packages

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use B during the migration.

    line 122

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Migrate Webpack → Vite

Vite's dev experience (sub-second cold start, instant HMR) is dramatic compared to webpack. Migration is moderately complex; the key insight is that Vite is config-by-omission — most webpack config disappears entirely.

When to use

  • Webpack dev server takes > 10s to start, > 200ms HMR.
  • Maintenance of webpack config is a tax — too many plugins, brittle config.
  • Project is Vue/Svelte (Vite is more native).
  • React project willing to pay one-time migration cost for ongoing dev-time win.

When NOT to use

  • Heavy use of webpack-specific plugins with no Vite equivalent (rare, but check).
  • Module Federation (some Vite plugins exist but maturity varies).
  • Server-side render with Webpack-managed SSR (use Next.js or Nuxt instead — they handle their own bundler).

Core differences

WebpackVite
Bundles everything in devNative ESM in dev, no bundling
Loaders for transformsPlugins (Rollup-compatible)
Long configConvention over config
Plugin ecosystem decade-deepSmaller; usually has the equivalent
Source maps slowNative ESM = browser-resolved sources
process.env.NODE_ENVimport.meta.env.MODE (and process.env polyfill)

Migration steps

1. Install Vite + framework plugin

pnpm remove webpack webpack-cli webpack-dev-server html-webpack-plugin \
  babel-loader css-loader style-loader file-loader url-loader
pnpm add -D vite @vitejs/plugin-react              # for React
# or @vitejs/plugin-vue, @sveltejs/vite-plugin-svelte

2. Move index.html to project root

Webpack: src/index.html referenced via HtmlWebpackPlugin. Vite: index.html at the project root. It IS the entry point.

<!-- index.html -->
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>My App</title>
</head>
<body>
  <div id="root"></div>
  <script type="module" src="/src/main.tsx"></script>      <!-- entry here -->
</body>
</html>

3. Create vite.config.ts

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import path from "node:path";

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: { "@": path.resolve(__dirname, "./src") },        // matches tsconfig paths
  },
  server: {
    port: 3000,
    proxy: {
      "/api": "http://localhost:4000",                       // backend during dev
    },
  },
  build: {
    outDir: "dist",
    sourcemap: true,
  },
});

That's a typical config. For most apps, this replaces 200 lines of webpack config.

4. Replace process.env.X references

Vite exposes env vars via import.meta.env. Two options:

Option A — full migration (preferred):

// before
const apiUrl = process.env.REACT_APP_API_URL;
// after
const apiUrl = import.meta.env.VITE_API_URL;

Rename REACT_APP_* → VITE_* in .env files.

Option B — polyfill (for migration):

// vite.config.ts
import { defineConfig, loadEnv } from "vite";
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), "REACT_APP_");
  return {
    define: {
      "process.env": Object.fromEntries(
        Object.entries(env).map(([k, v]) => [k, JSON.stringify(v)])
      ),
    },
  };
});

Use B during the migration; convert to A as you touch files.

5. Update import paths

Vite is stricter about imports:

IssueFix
import './styles' (no extension) for non-JSAdd extension: import './styles.css'
Importing .svg as React componentUse vite-plugin-svgr or ?react query
Importing JSONWorks natively
require() callsConvert to import

6. Asset handling

Webpack:

import logo from "./logo.png";                             // gets URL via file-loader

Vite: same syntax. Built-in. Add to TypeScript declarations if TS complains:

// src/vite-env.d.ts
/// <reference types="vite/client" />
declare module "*.svg?react" {
  import { FC, SVGProps } from "react";
  const C: FC<SVGProps<SVGSVGElement>>;
  export default C;
}

7. CSS handling

CSS Modules: filename must be *.module.css. (Same as Webpack convention with css-loader.)

Sass/Less: pnpm add -D sass — Vite picks it up automatically. No config needed.

PostCSS: works via postcss.config.js at the root.

Tailwind: works as documented; the v4 PostCSS-less version works particularly well with Vite.

8. Plugin equivalents (the cheat sheet)

WebpackVite
HtmlWebpackPluginBuilt-in (index.html at root)
MiniCssExtractPluginBuilt-in for prod
DefinePlugindefine: in config
CopyWebpackPluginvite-plugin-static-copy
ProvidePlugin (e.g. global jQuery)Discouraged; use ES imports
BundleAnalyzerPluginrollup-plugin-visualizer
webpack-bundle-analyzerrollup-plugin-visualizer
tsconfig-paths-webpack-pluginWorks via vite-tsconfig-paths
@svgr/webpackvite-plugin-svgr
webpack-pwa-manifest / workbox-webpack-pluginvite-plugin-pwa

9. Update scripts in package.json

{
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  }
}

10. Update CI / Docker

# Build stage
RUN pnpm build                          # vite build, outputs to dist/
# Serve stage
COPY --from=build /app/dist /usr/share/nginx/html

Common gotchas

  • require calls in code — Vite errors. Convert to import.
  • CommonJS dependencies — Vite handles with optimizeDeps. If a dep fails: add it to optimizeDeps.include in vite.config.
  • Polyfills for Node globals (Buffer, process) — install vite-plugin-node-polyfills if a dep needs them in browser. Better: replace the dep with a browser-compatible alternative.
  • Dynamic imports with template literals — import(\./locales/${lang}.js`)— Vite needs explicit glob:import.meta.glob('./locales/*.js')`.
  • Web Workers — Vite uses ?worker query: import Worker from "./my-worker.ts?worker".
  • Tests — webpack often shared config with Jest. Vite + Vitest is the natural pair (see jest-to-vitest).
  • Storybook — Storybook 7+ has Vite builder; older versions need migration.
  • Source maps in production — build.sourcemap: true outputs .map files; serve them or upload to Sentry per sentry-monitoring.

Performance comparison

After migration, expect:

MetricWebpackVite
Cold dev start10–60s<2s
HMR after edit200–2000ms<50ms
Production build timesimilarsimilar to Rollup
Production bundle sizesimilarsimilar

The dev-time delta is the win. Production builds are comparable.

Anti-patterns

  • Trying to preserve webpack config 1:1 — Vite is convention-first. Trust the defaults; configure only what's different.
  • Migrating and refactoring in one PR — too many variables. Migrate first; refactor later.
  • Ignoring optimizeDeps warnings — slow startup or runtime errors. Add deps to optimizeDeps.include.
  • Not migrating tests — running webpack-built tests in a Vite project doubles maintenance.
  • Production using webpack, dev using Vite — divergent bundles, divergent bugs. Migrate fully or don't.
  • Polyfilling Node globals as a default — the deps making you do this are a smell. Prefer browser-native alternatives.
  • Not benchmarking — claim "Vite is faster" without measuring. Sometimes the win is smaller than expected; sometimes much larger.
  • Migrating before TS/ESM cleanup — fix .ts extensions, mixed CJS/ESM first; then migrate.
  • Skipping production preview — vite preview shows the actual prod build; test it before deploying.

Verify it worked

  • pnpm dev starts in < 2 seconds.
  • HMR after a code edit shows in < 200 ms.
  • pnpm build && pnpm preview produces a working production preview.
  • Production bundle size is comparable to webpack output (within 10%).
  • All tests pass under the new test runner.
  • Source maps work in production debugger.
  • CI is faster (build step time reduced).
  • No webpack, webpack-cli, webpack-dev-server in dependencies.
  • process.env.X references are migrated to import.meta.env.VITE_X (or polyfill is removed after migration).
  • The dev experience improvement is real-world (devs report it; not just on-paper).

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.