rollup-plugin-autorequire
v0.0.4
Published
Requires an additionnal list of files while bundling, those files being single files in each subfolder of a given folder
Readme
rollup-plugin-autorequire
A Rollup plugin used in the FormantJS framework.
It automatically requires files from given directories (and their subfolders) and bundles them together into a virtual module.
It is used for collecting component types and validators (or any other categories you define) into a single importable object.
Installation
npm install rollup-plugin-autorequireUsage
Add it to your rollup.config.mjs:
import autoRequirePlugin from 'rollup-plugin-autorequire';
export default {
input: 'src/index.js',
plugins: [
autoRequirePlugin({
targets : [
{
dir: codebaseFolder + 'FormantComponentLib/src/UI/categories',
name : 'componentTypes',
filter : '**/!(validators)/!(*Def).js'
},
{
dir: codebaseFolder + 'FormantComponentLib/src/UI/categories/validators',
name : 'validators'
},
]
})
]
};
- If
filteris not specified, the plugin defaults to"**/*.js", meaning it will recursively collect all.jsfiles under the given directory.- You can restrict the depth by providing your own glob patterns (e.g.
"*.js"for flat,"*/*.js"for exactly one subfolder, etc.).
This generates a virtual module you can import anywhere in your code:
import modules from "virtual-modules";
// Components collected under "componentTypes"
modules.componentTypes.TextInput(componentTemplate, parentComponent);
// Validators collected under "validators"
// Use them as suggested in FormantJS documentation
modules.validators.emailInput;In FormantJS, this allows us to reflect the component lib on the App type at bundle time (every defined component is bundled, we don't maintain an explicit list):
import {App} from "formantjs";
App.componentTypes.TextInput(componentTemplate, parentComponent);Options
targets: Array of objects describing what to collect. Each target supports:dir(string, required) → Root directory to traverse.name(string, required) → Property name in the resulting object.filter(string|string[], optional) → One or multiple glob patterns.- If omitted, defaults to
"**/*.js". - If provided, your globbing patterns are trusted as-is (e.g.
"**/*.json"will include JSON files).
- If omitted, defaults to
Example with multiple filters:
{
dir: "src/resources",
name: "resources",
filter: ["**/*.json", "**/*.yaml"]
}Generated Output
The plugin injects a virtual module looking like this:
const modules = {
componentTypes: {
TextInput: require("/abs/path/FormantComponentLib/src/UI/categories/inputs/TextInput.js"),
PasswordInput: require("/abs/path/FormantComponentLib/src/UI/categories/inputs/PasswordInput.js"),
ClickableComponent: require("/abs/path/FormantComponentLib/src/UI/categories/buttons/ClickableComponent.js")
},
validators: {
emailInput: require("/abs/path/FormantComponentLib/src/validators/emailInput.js"),
passwordInput: require("/abs/path/FormantComponentLib/src/validators/passwordInput.js")
}
};
module.exports = modules;License
MIT
Notes
- This plugin builds a virtual module at bundle-time (no runtime FS access needed in production).
- By default, only
.jsfiles are included. - Primarily intended for internal builds with the FormantJS framework.
