---
name: webpack-config-formatter
description: "A webpack.config.js laid out in the conventional section order with a commented heading on each block, generated from your entry, output, loaders, plugins, dev server port, and target so it stays readable as it grows. Use when a webpack.config.js needs writing in a clean conventional order with a comment heading on each block, from a short spec of entry, output, loaders, plugins, port, and target. Do not use for diagnosing a failing build, splitting an existing config into files, or cutting bundle size. Related topics: coding, webpack."
---

# Webpack Config Formatter

A webpack.config.js laid out in the conventional section order with a commented heading on each block, generated from your entry, output, loaders, plugins, dev server port, and target so it stays readable as it grows.

## When to use

Use this skill when a webpack.config.js needs writing in a clean conventional order with a comment heading on each block, from a short spec of entry, output, loaders, plugins, port, and target. Do not use it for diagnosing a failing build, splitting an existing config into files, or cutting bundle size.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Fix the section order (requires, mode, entry, output, resolve, module.rules, plugins, devServer, devtool, optimization) so every setting has one place to live.
3. Write one module rule per selected loader with the test pattern, the loader chain in correct execution order, and node_modules excluded where it applies.
4. Instantiate only the selected plugins with sensible options, replacing any that the mode or webpack version makes redundant and noting why in a comment.
5. Add devServer on the given port for the web target only, and set target-specific output and externals for node or electron.
6. Head every block with a short comment and list the npm packages to install at the top, then return the file as one javascript code block.

## Inputs

- **Entry point** (free text, e.g. "./src/index.js")
- **Output directory** (free text, e.g. "./dist")
- **Build mode** (choose one: development, production)
- **Loaders to include** (choose any: Babel, TypeScript, CSS, SASS, Images, JSON)
- **Plugins needed** (choose any: HtmlWebpackPlugin, MiniCssExtractPlugin, DefinePlugin, CleanWebpackPlugin, CopyWebpackPlugin)
- **Dev server port** (number from 3000 to 9000)
- **Target environment** (choose one: web, node, electron)

## Output

One javascript code block containing the complete webpack.config.js, opened by a comment listing packages to install, with a comment heading above each configuration block.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Webpack Config Formatter

Write a complete, tidily ordered webpack.config.js from the details below.

- Entry point: {{Entry point}}
- Output directory: {{Output directory}}
- Build mode: {{Build mode}}
- Loaders to include: {{Loaders to include}}
- Plugins needed: {{Plugins needed}}
- Dev server port: {{Dev server port}}
- Target environment: {{Target environment}}

Lay the file out in one fixed order so anyone can find a setting without searching: requires and path helpers, then mode, entry, output, resolve, module.rules, plugins, devServer, and finally devtool and optimization. Put a short comment heading above each block and a one-line comment on any option whose value is not obvious. Write one rule per loader with the test regex, the loader chain in the correct order (style-loader or MiniCssExtractPlugin.loader last in the array so it runs first), and the exclude for node_modules where it applies. Instantiate only the plugins selected, with sensible options, and leave out any that are redundant for the mode (CleanWebpackPlugin is unnecessary on webpack 5 when output.clean is set; say so in a comment and use output.clean). Set devServer only for the web target, on the given port, with historyApiFallback for single-page apps. Use the target to pick the right output library settings and externals. Return the full file in a single javascript code block with no prose outside it, and list the npm packages to install as a comment at the top.

## Pitfalls

- Ordering a CSS loader chain as css-loader then style-loader in the array, which runs them backwards and breaks the build.
- Enabling devServer for a node or electron target, where it does nothing and confuses the next reader.
- Adding CleanWebpackPlugin on webpack 5 alongside output.clean, so the output folder is cleaned twice and the plugin version drifts.
