rollup-plugin-dynamic-import-variables
raw JSON → 1.1.0 verified Mon Apr 27 auth: no javascript
Rollup plugin (v1.1.0) that resolves dynamic imports containing variables at build time by converting concatenated strings into glob patterns and bundling matching files. Maintained by LarsDenBakker with infrequent releases. Key differentiator: enables dynamic imports with variable parts in Rollup, similar to webpack's dynamic import handling, without requiring complex configuration. Supports ESM and CJS. Alternatives include @rollup/plugin-dynamic-import-vars (official fork) which is more actively maintained.
Common errors
error Error: Could not resolve './foo/bar.js' from 'src/index.js' ↓
cause The glob pattern matched files but the exact import path is not found because the file doesn't exist or the pattern is too restrictive.
fix
Check that the variable parts produce at least one valid file path. Adjust the import pattern or ensure the referenced files exist.
error (!) Plugin dynamic-import-variables: Dynamic imports must start with './' or '../' ↓
cause The dynamic import string starts with a variable or bare module specifier (e.g., `import(bar)` or `import("some-library/" + bar + ".js")`).
fix
Prefix the import with a relative path starting with './' or '../'.
error (!) Plugin dynamic-import-variables: Dynamic imports must end with a file extension ↓
cause The dynamic import string does not contain a file extension in the static part (e.g., `import('./' + name)` without '.js').
fix
Add a file extension to the static part of the import string, e.g.,
import('./' + name + '.js'). error (!) Plugin dynamic-import-variables: Imports to your own directory must specify a filename pattern ↓
cause The import matches the same directory as the source file without specifying a filename pattern (e.g., `import('./' + name + '.js')` when in the same directory).
fix
Add a static prefix or suffix to the filename part, e.g.,
import('./module-' + name + '.js'). Warnings
breaking Dynamic imports must start with './' or '../' - variable-only paths are not supported. ↓
fix Prefix the dynamic import with a relative path, e.g., `import('./' + name + '.js')` instead of `import(name)`.
breaking Dynamic imports must end with a file extension in the static part. ↓
fix Ensure the import string ends with a file extension (e.g., `.js`, `.mjs`). For example: `import('./' + name + '.js')`.
gotcha Globs only go one level deep per directory - nested variables may not import as expected. ↓
fix Avoid multiple variables in the same directory segment; reorganize directory structure if deeper recursion is needed.
gotcha Importing your own directory (e.g., `import('./' + name + '.js')`) is not allowed if the import path resolves to the same directory as the source file. ↓
fix Use a more specific filename pattern, e.g., `import('./module-' + name + '.js')`.
deprecated This package is no longer actively maintained; consider using the official fork '@rollup/plugin-dynamic-import-vars'. ↓
fix Replace 'rollup-plugin-dynamic-import-variables' with '@rollup/plugin-dynamic-import-vars' in your dependencies and import.
Install
npm install rollup-plugin-dynamic-import-variables yarn add rollup-plugin-dynamic-import-variables pnpm add rollup-plugin-dynamic-import-variables Imports
- default (dynamicImportVariables) wrong
const dynamicImportVariables = require('rollup-plugin-dynamic-import-variables');correctimport dynamicImportVariables from 'rollup-plugin-dynamic-import-variables'; - dynamicImportVariables wrong
import * as dynamicImportVariables from 'rollup-plugin-dynamic-import-variables';correctimport dynamicImportVariables from 'rollup-plugin-dynamic-import-variables'; - dynamicImportVariables (as function call) wrong
export default { plugins: [ new dynamicImportVariables() ] }correctexport default { plugins: [ dynamicImportVariables({ include: ['**/*.js'] }) ] }
Quickstart
// rollup.config.js
import dynamicImportVariables from 'rollup-plugin-dynamic-import-variables';
export default {
input: 'src/index.js',
output: { dir: 'dist', format: 'esm' },
plugins: [
dynamicImportVariables({
// Only process files in 'src' folder
include: ['src/**'],
// Exclude node_modules by default
exclude: ['node_modules/**'],
// Warn instead of error for problematic imports
warnOnError: true
})
]
};