---
name: webpack-config-builder
description: "Writes one working webpack.config.js for a chosen target (web, node, or electron) and mode, with only the loaders you picked, correct resolve settings for TypeScript, and the right devServer or minimizer for that mode. Use when you want one straightforward webpack.config.js for a specific mode and target such as node or electron, with just the loaders you picked. Do not use for a multi-environment config set with shared common rules, or understanding or repairing an existing config. Related topics: coding, webpack."
---

# Webpack Config Builder

Writes one working webpack.config.js for a chosen target (web, node, or electron) and mode, with only the loaders you picked, correct resolve settings for TypeScript, and the right devServer or minimizer for that mode.

## When to use

Use this skill when you want one straightforward webpack.config.js for a specific mode and target such as node or electron, with just the loaders you picked. Do not use it for a multi-environment config set with shared common rules, or understanding or repairing an existing config.

## 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. Set target, externals, and output for the chosen environment; node and electron-main exclude node_modules from the bundle.
3. Add one module rule per selected loader with the correct loader chain order and file test, using asset modules on webpack 5.
4. Configure resolve.extensions and ts-loader only when TypeScript is chosen, pointing at the project tsconfig.
5. Add the mode-specific block: devServer with hot reload and a fast devtool for development, minimizer and content hashes for production.
6. Trim the file to constants, used plugin imports, and literal settings with no environment conditionals.

## Inputs

- **Project language** (choose one: JavaScript, TypeScript)
- **Entry files** (longer free text)
- **Output directory** (free text, e.g. "dist")
- **Target environment** (choose one: web, node, electron-main, electron-renderer)
- **Loaders needed** (choose any: Babel, TypeScript, CSS, Sass, Images and fonts, Assets as raw text)
- **Mode** (choose one: development, production)
- **Enable source maps** (choose one: Yes, No)
- **Webpack version** (choose one: 5, 4)

## Output

A single javascript code block containing the full webpack.config.js with imports, entry, output, target, module.rules, resolve, devtool, and the devServer or optimization block for the mode.

## Prompt template

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

### Webpack Config Builder

Write a single webpack.config.js for the project below.

- Project language: {{Project language}}
- Entry files: {{Entry files}}
- Output directory: {{Output directory}}
- Target environment: {{Target environment}}
- Loaders needed: {{Loaders needed}}
- Mode: {{Mode}}
- Enable source maps: {{Enable source maps}}
- Webpack version: {{Webpack version}}

Generate exactly one config for the chosen mode and target, nothing conditional. Set target to match the environment: node and electron-main need externals for node_modules and no browser polyfills; electron-renderer keeps browser behaviour with node integration assumptions stated in a comment. Add a module rule only for each loader selected, with the loader chain in correct order and the file test it covers, and use webpack 5 asset modules instead of file-loader when the version is 5. Set resolve.extensions to include .ts and .tsx only when TypeScript is chosen, and point ts-loader at the project tsconfig. In production, enable minimization with the Terser minimizer and content hashes in output filenames; in development, configure devServer with hot reload and a static directory, and pick a fast source map such as eval-cheap-module-source-map when source maps are on. In production, use source-map only when the user asked for maps.

Keep the file short and literal: constants at the top, plugin imports only for plugins used, no environment switches.

### Output
Return the complete webpack.config.js as one fenced javascript block with plugin imports, entry, output, target, module.rules, resolve, devtool, and the mode-specific devServer or optimization block, and no explanation outside the code.

## Pitfalls

- Bundling node_modules into a node or electron-main build, which breaks native modules and bloats the output.
- Using file-loader or url-loader on webpack 5 where asset modules replace them and the old loaders are unmaintained.
- Setting devtool to source-map in development, which makes every rebuild seconds slower than the eval variants.
