A Rollup plugin (v1.2.1) that replaces module paths during bundling, similar to Webpack's NormalModuleReplacementPlugin. Supports string or regex pattern matching with replacement strings or functions for dynamic rewrites. Ideal for environment-specific builds (e.g., swapping mock vs production modules). Unlike rollup-plugin-alias, it does not resolve modules internally, allowing custom resolvers (e.g., rollup-plugin-node-resolve) to be passed in. Released under MIT, with irregular updates (last in 2020).
npm install rollup-plugin-module-replacementVerified import paths — ran on the pinned version, not inferred.
Demonstrates regex-based module replacement with a custom resolver using rollup-plugin-node-resolve.
Pass a resolver plugin (e.g., rollup-plugin-node-resolve) as the second argument or ensure replacement results in an absolute path.
Order entries from most specific to least specific to avoid unintended matches.
Replace imports from 'rollup-plugin-node-resolve' to '@rollup/plugin-node-resolve'.
Always return a string from the replacement function. For unmatched patterns, return the original importee.
Ensure replacement produces a valid path, or pass a customResolver as second argument to the plugin.
Use: import replacement from 'rollup-plugin-module-replacement'
Use this plugin only for module path replacement, not for content transformation.