webpack-addons
webpack-addons is a utility suite designed to assist in creating webpack-cli addons by providing helper functions for common webpack configuration patterns and interactive Inquirer.js prompts. Originally published in 2017, its latest version is 1.1.5. This package is specifically tailored for scaffolding webpack configurations programmatically, offering utilities like `parseValue` for special string formatting, and `createArrowFunction` for dynamic entry points. A key differentiator was its inclusion of a `createCommonsChunkPlugin` helper, which is now deprecated in Webpack 4+ in favor of `optimization.splitChunks`. Given its last update date and the deprecation of core Webpack features it wraps, the package is considered unmaintained and potentially incompatible with modern webpack versions.
Common errors
-
TypeError: webpack.optimize.CommonsChunkPlugin is not a constructor
cause Attempting to use `createCommonsChunkPlugin` with Webpack v4 or newer, where `CommonsChunkPlugin` has been removed.fixRemove all calls to `createCommonsChunkPlugin`. Implement code splitting using Webpack's `optimization.splitChunks` configuration instead. -
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax (`import { ... } from 'webpack-addons'`) in a CommonJS module context, or when Node.js is not configured for ESM.fixReplace `import` statements with CommonJS `require()` syntax, e.g., `const { parseValue } = require('webpack-addons');`. Ensure your file is treated as CommonJS (e.g., `.js` extension without `"type": "module"` in `package.json`).
Warnings
- breaking The `createCommonsChunkPlugin` utility provided by `webpack-addons` wraps Webpack's `CommonsChunkPlugin`, which was officially removed in Webpack v4. Using this utility with Webpack v4 or newer will result in an error or unexpected behavior, as the underlying plugin no longer exists. Modern Webpack uses `optimization.splitChunks` for code splitting.
- gotcha This package is CommonJS-only, relying on `require()` for module imports. It does not provide ESM exports, meaning direct `import` statements in an ES Module context will fail. This limits its compatibility in modern JavaScript projects that primarily use ESM.
- gotcha The `webpack-addons` package appears to be unmaintained, with its last known update being in 2017. This means it may not be compatible with recent versions of Node.js, `webpack-cli`, or Webpack itself, and will not receive security patches or bug fixes for new issues. Proceed with caution when integrating into current projects.
- gotcha The package does not ship with TypeScript type definitions. Developers using TypeScript will need to create their own declaration files (`.d.ts`) or use `any` types, leading to a loss of type safety and IDE assistance when interacting with `webpack-addons`'s utilities.
Install
-
npm install webpack-addons -
yarn add webpack-addons -
pnpm add webpack-addons
Imports
- parseValue
import { parseValue } from 'webpack-addons'; /* This package is CommonJS only */const { parseValue } = require('webpack-addons'); - createArrowFunction
import createArrowFunction from 'webpack-addons/createArrowFunction'; /* Incorrect import path and ESM syntax */
const createArrowFunction = require('webpack-addons').createArrowFunction; - List
require('webpack-addons/inquirer').List; /* Inquirer utilities are directly exported */const { List } = require('webpack-addons');
Quickstart
const { parseValue, createArrowFunction, List } = require('webpack-addons');
// Simulate a webpack-cli addon's generator context
class WebpackAddonGenerator {
constructor() {
this.configuration = {
myScaffold: {
webpackOptions: {}
}
};
}
// Example of applying utilities to webpack configuration
applyConfiguration() {
this.configuration.myScaffold.webpackOptions.output = {
sourcePrefix: parseValue('\t\t')
};
this.configuration.myScaffold.webpackOptions.entry = createArrowFunction('src/main.js');
console.log('Generated Webpack Output Source Prefix:', this.configuration.myScaffold.webpackOptions.output.sourcePrefix);
console.log('Generated Webpack Entry:', this.configuration.myScaffold.webpackOptions.entry());
}
// Example of using an Inquirer prompt (requires actual Inquirer setup in a CLI)
async promptUser() {
// In a real CLI, this would be await inquirer.prompt(...)
const entryChoice = List(
'entryType',
'What kind of entry do you want?',
['String', 'Function', 'Object']
);
console.log('Simulated Inquirer List prompt for entryType:', entryChoice.name, entryChoice.choices);
}
}
const generator = new WebpackAddonGenerator();
generator.applyConfiguration();
generator.promptUser();