rollup-plugin-rename-files
raw JSON → 2.0.0 verified Mon Apr 27 auth: no javascript
A Rollup plugin that renames output files, primarily used to fix extensions like .less.js to .css.js when bundling with preserveModules. Current version 2.0.0, based on rollup-plugin-rename-node-modules. Active maintenance. Differentiators: allows arbitrary filename transformations via a function, not limited to node_modules or extensions, unlike alternatives like rollup-plugin-rename-extensions or rollup-plugin-rename.
Common errors
error TypeError: rename is not a function ↓
cause Incorrect import or CJS require usage in ESM context
fix
Use dynamic import: const rename = (await import('rollup-plugin-rename-files')).default;
error Error: The 'includes' option must be a string. ↓
cause Passing an array or other type for includes
fix
Set includes to a string (e.g., '') or a glob pattern.
error TypeError: moduleName must be a string or function ↓
cause Passing a non-function non-string value for moduleName
fix
Provide a function like (filename) => filename.replace(...) or a string replacement pattern.
Warnings
breaking v2.0.0 changed default export from a function expecting a string to an object with 'includes' and 'moduleName' options. ↓
fix Update plugin call to pass an options object: rename({ includes: '', moduleName: (f) => f })
deprecated Passing a string directly to rename() is deprecated since v2.0.0 ↓
fix Use options object with moduleName property instead.
gotcha Plugin only affects files output by Rollup; it does not transform source files or handle virtual modules. ↓
fix Ensure output files are generated with preserveModules or similar setting so filenames can be processed.
gotcha If `includes` is not set (empty string), it applies to all files; specifying a pattern may cause some files to be missed. ↓
fix Leave includes empty for global rename, or use a glob pattern like '**/*.js'.
Install
npm install rollup-plugin-rename-files yarn add rollup-plugin-rename-files pnpm add rollup-plugin-rename-files Imports
- default wrong
const rename = require('rollup-plugin-rename-files')correctimport rename from 'rollup-plugin-rename-files' - RenameOptions
import type { RenameOptions } from 'rollup-plugin-rename-files' - default (with type) wrong
import { rename } from 'rollup-plugin-rename-files'correctimport rename from 'rollup-plugin-rename-files'
Quickstart
// rollup.config.js
import rename from 'rollup-plugin-rename-files';
export default {
input: 'src/index.js',
output: {
dir: 'dist',
format: 'cjs',
preserveModules: true,
},
plugins: [
rename({
includes: '',
moduleName: (filename) => filename.replace(/\.less\.js$/g, '.css.js'),
}),
],
};