---
name: webpack-config-refactoring-tool
description: "Splits a tangled webpack config into common, development, and production parts in your chosen module format, with the original build output preserved and a note on every structural change. Use when a working webpack config has grown into one tangled file and needs splitting into common, development, and production parts in a chosen module format without changing the build output. Do not use for generating a config from scratch, tuning bundle size, or diagnosing a failing build. Related topics: coding, webpack."
---

# Webpack Config Refactoring Tool

Splits a tangled webpack config into common, development, and production parts in your chosen module format, with the original build output preserved and a note on every structural change.

## When to use

Use this skill when a working webpack config has grown into one tangled file and needs splitting into common, development, and production parts in a chosen module format without changing the build output. Do not use it for generating a config from scratch, tuning bundle size, or diagnosing a failing build.

## 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. Inventory what the current config builds: entries, output names, loader-to-file mapping, and every plugin with options, as the contract the refactor must keep.
3. Split the config into a common part and per-environment parts, merged with webpack-merge, so development and production settings stop leaking into each other.
4. Extract repeated loader chains into named helpers, group rules by file type, and lift paths, ports, and the public path into constants at the top.
5. Replace deprecated plugin usage with its current equivalent and apply the chosen module format consistently, including the export.
6. Return the new files in code blocks with a bullet list of the major changes and a JSON summary of the options chosen.

## Inputs

- **Current config** (longer free text)
- **Config file location** (free text, e.g. "./webpack.config.js")
- **Target environment** (choose one: development, production, both)
- **Module format** (choose one: CommonJS, ESModules, UMD)
- **Plugins to keep or add** (choose any: HtmlWebpackPlugin, MiniCssExtractPlugin, DefinePlugin, CleanWebpackPlugin, BundleAnalyzerPlugin)
- **Source maps** (choose one: enabled, disabled)
- **Extra loaders** (longer free text)

## Output

One fenced javascript code block per resulting config file, then a bullet list explaining each structural change, then a JSON object with keys environment, moduleFormat, plugins, sourceMaps, and extraLoaders.

## Prompt template

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

### Webpack Config Refactoring Tool

Refactor my webpack configuration into a clean, modular structure without changing what it builds.

- Current config: {{Current config}}
- Config file location: {{Config file location}}
- Target environment: {{Target environment}}
- Module format: {{Module format}}
- Plugins to keep or add: {{Plugins to keep or add}}
- Source maps: {{Source maps}}
- Extra loaders: {{Extra loaders}}

Before touching anything, write down what the current config produces: entries, output names, which loaders apply to which files, and every plugin with its options. That list is the contract the refactor must still satisfy. Then split by concern: a common config holding entry, output, resolve, and shared rules; an environment config for development (devtool, devServer, HMR) and one for production (minification, hashing, extraction), merged with webpack-merge when both are needed. Extract repeated loader chains into small named functions, group rules by file type, and move magic strings (paths, public path, port) to constants at the top. Replace deprecated plugin usage with its current equivalent and note it. Apply the chosen module format consistently, including how the config is exported. Keep source maps as requested per environment. Return the new file or files each in a fenced javascript code block, then a bullet list explaining the major refactoring choices: how rules were extracted, which plugins were added or removed and why, and how source maps are handled. Finish with a JSON object summarising the options chosen, using the keys "environment", "moduleFormat", "plugins", "sourceMaps", and "extraLoaders".

## Pitfalls

- Dropping a loader option or plugin flag during the split, so the build still runs but emits different output.
- Merging environment configs with Object.assign instead of webpack-merge, which overwrites the rules array instead of concatenating it.
- Mixing require and import in one file when the module format is ESModules, which fails under node ESM resolution.
