rename-webpack-plugin

raw JSON →
2.0.0 verified Sat Apr 25 auth: no javascript

Webpack plugin for renaming output files, allowing partial filename overrides when using hash-based naming (e.g., [chunkhash]). Version 2.0.0 adds webpack 4 support. Primarily useful for customizing chunk or asset names that webpack's built-in naming can't handle. Lightweight, no extra dependencies beyond webpack peer dependency.

error TypeError: originNameReg.test is not a function
cause originNameReg is not a RegExp (e.g., a string was passed).
fix
Ensure originNameReg is a RegExp object (e.g., /pattern/).
error Error: Cannot find module 'rename-webpack-plugin'
cause Package not installed or not in node_modules.
fix
Run 'npm install --save-dev rename-webpack-plugin' or 'yarn add --dev rename-webpack-plugin'.
error Module parse failed: Unexpected token (1:0) - You may need an appropriate loader to handle this file type.
cause Using ES import syntax (import) with a package that only exports CommonJS.
fix
Use require() or enable bundler's CommonJS interop (e.g., webpack's module.rules).
breaking Version 2.0.0 drops support for webpack <4. If upgrading from v1, ensure your project uses webpack 4+.
fix Upgrade webpack to ^4.0.0 or stay on rename-webpack-plugin v1.
deprecated CommonJS pattern required; no ESM named export available.
fix Use require() or use bundler that handles CJS interop (e.g., webpack or babel).
gotcha originNameReg must be a RegExp object; passing a string will not work and will throw or silently fail.
fix Always use a RegExp literal: /pattern/ or new RegExp('pattern').
gotcha targetName uses String.replace semantics; ensure backreferences like $1 are escaped properly in strings.
fix Use single quotes or double backslashes: '$1.js' or "$1.js".
npm install rename-webpack-plugin
yarn add rename-webpack-plugin
pnpm add rename-webpack-plugin

Shows minimal webpack configuration using rename-webpack-plugin to rename files matching a pattern (e.g., thirdpart.*.js to thirdpart.js).

const RenameWebpackPlugin = require('rename-webpack-plugin');

module.exports = {
  entry: 'app.js',
  output: {
    path: __dirname + '/dist',
    filename: 'app.[chunkhash].js'
  },
  plugins: [
    new RenameWebpackPlugin({
      originNameReg: /(.*)thirdpart\..*\.js/,
      targetName: '$1thirdpart.js'
    })
  ]
};