rollup-plugin-local-import
raw JSON → 1.2.0 verified Mon Apr 27 auth: no javascript
A Rollup plugin that transforms local import/export paths in JavaScript/TypeScript bundles. Version 1.2.0 is the latest stable release, as a native addon supporting Windows, macOS, and Linux. It identifies local imports (starting with './' or '../') and applies a user-defined callback to modify the path, leaving external dependencies untouched. Unlike generic string replacement plugins, it uses AST-based detection to avoid false positives on non-import strings.
Common errors
error TypeError: localImport is not a function ↓
cause Importing default instead of named export
fix
Use import { localImport } from 'rollup-plugin-local-import' instead of import localImport from ...
error Error: The plugin 'rollup-plugin-local-import' does not support the current platform ... ↓
cause Running on an unsupported operating system or architecture
fix
Upgrade node or switch to a supported OS. See documentation for supported platforms.
error Cannot find module 'rollup-plugin-local-import' ↓
cause Package not installed or installed as devDependency
fix
Run 'npm install --save-dev rollup-plugin-local-import' or 'yarn add --dev rollup-plugin-local-import'
Warnings
breaking Native addon may not build on unsupported OS/arch combinations (e.g., FreeBSD, older glibc) ↓
fix Use a previous version or run on a supported system.
gotcha The plugin only modifies imports/exports that start with './' or '../'. Imports like 'foo' or 'some-package' are left unchanged. ↓
fix None needed; this is by design.
gotcha The callback is called for every identified local import. If the callback returns the same path unchanged, no modification occurs. ↓
fix Ensure callback returns a modified string if transformation is intended.
gotcha Plugin must be placed before other plugins that also transform import paths to avoid conflicts. ↓
fix Order plugins carefully in the Rollup config.
Install
npm install rollup-plugin-local-import yarn add rollup-plugin-local-import pnpm add rollup-plugin-local-import Imports
- localImport wrong
import localImport from 'rollup-plugin-local-import'correctimport { localImport } from 'rollup-plugin-local-import' - localImport wrong
const localImport = require('rollup-plugin-local-import')correctconst { localImport } = require('rollup-plugin-local-import') - Callback type
import type { Callback } from 'rollup-plugin-local-import'
Quickstart
// rollup.config.js
import { defineConfig } from 'rollup';
import { localImport } from 'rollup-plugin-local-import';
export default defineConfig({
input: 'src/index.js',
output: {
dir: 'dist',
format: 'esm'
},
plugins: [
localImport((path) => {
// Add .js extension to local imports
return `${path}.js`;
})
]
});