rollup-plugin-import-paths
v0.0.1
Published
Requires/Imports an additionnal list of files while bundling, those files being in the subtree of a given folder
Readme
rollup-plugin-import-paths
A Rollup plugin which automatically imports or requires files from directories and bundles them together into a virtual module.
This is useful for collecting things like components, validators, or any file category into a single object available at bundle time — without maintaining an explicit list.
(Rewriting of rollup-plugin-autorequire, generalized and more robust, originally used in the FormantJS framework)
Installation
npm install rollup-plugin-auto-importUsage
Add it to your rollup.config.mjs (or .js):
import autoImport from 'rollup-plugin-auto-import';
export default {
input: 'src/index.js',
plugins: [
autoImport({
format: 'cjs', // or 'esm'
targets: [
{
dir: 'src/components',
name: 'components',
filter: '**/*.js'
},
{
dir: 'src/validators',
name: 'validators'
}
],
debug: 1 // optional: show summary logs
})
]
};Anywhere in your code:
// CJS
const modules = require('virtual:auto-import.js');
// ESM
import modules from 'virtual:auto-import.js';
// Access collected files
modules.components.TextInput(...);
modules.validators.email(...);Options
format:'cjs'(default) or'esm'.- CJS uses
require(). - ESM uses
() => import(...)loader functions.
- CJS uses
targets: Array of directories to scan. Each target can be a string (\"src/components\") or an object:{ dir: 'src/resources', // Directory to scan name: 'resources', // propery name in the export filter: ['**/*.json'], // Optional glob(s) to include (string|array, defaults to '**/*.{js,mjs,cjs,ts,tsx,jsx}') ignore: ['**/*.test.*'] // Optional ignore patterns (string|array, defaults to ['**/*.spec.*', '**/*.test.*', '**/__tests__/**', '**/node_modules/**']) }root: Project root (defaults toprocess.cwd()).debug: Debug verbosity level:0: silent (default)1: summary (counts, size, timing)2: list imported files3: dump generated virtual module code
Example Output
Generated module looks like:
// (CJS output example)
const modules = {
components: {
TextInput: require("/abs/path/src/components/TextInput.js"),
Button: require("/abs/path/src/components/Button.js")
},
validators: {
email: require("/abs/path/src/validators/email.js"),
password: require("/abs/path/src/validators/password.js")
}
};
module.exports = modules;Notes
- Files are resolved at bundle-time.
- Only files inside the project root are watched in dev; external dirs are imported but not watched.
- A pragmatic, zero-maintenance way to keep your imports in sync with your file structure.
