rollup-auto-resolve
Automatically resolves entry points for inputs files in Vite builds
Install
npm install --save @serverduty/rollup-auto-resolve
or
yarn add @serverduty/rollup-auto-resolve
Usage
The autoResolve
function dynamically scans a given directory (or directories) for HTML files (or other file types, if specified) and generates an object suitable for use as rollupOptions.input
in Vite's configuration. This is particularly useful for multi-page applications, automating the process of defining entry points for the build process.
Quick Example
If you have a multi-page site in the src
directory, the following vite.config.js
allows you to automatically include all .html
files without manually configuring them, saving you time and energy to build the things that matter.
import { defineConfig } from 'vite'
import { autoResolve } from '@serverduty/rollup-auto-resolve'
export default defineConfig({
root: 'src',
build: {
outDir: '../dist',
emptyOutDir: true,
rollupOptions: {
input: autoResolve([__dirname, 'src'])
}
}
})
Basic Usage
Single Path
To scan a single directory for HTML files:
import { autoResolve } from '@serverduty/rollup-auto-resolve'
const inputOptions = autoResolve('path/to/your/directory')
// Use inputOptions in your vite.config.js
This will scan the specified directory and all its subdirectories for .html
files, adding them to the build configuration.
Handling Nested Directories
The autoResolve
function can handle nested directory structures by accepting an array of path segments that form the base path. This is useful when your project structure has nested directories, and you want to specify the base path more dynamically.
import { autoResolve } from '@serverduty/rollup-auto-resolve'
const inputOptions = autoResolve([__dirname, 'src'])
// Use inputOptions in your vite.config.js
In this example, the function will resolve the src
subdirectory and scan it for the specified file types. This approach provides flexibility in specifying the base path for scanning, especially in complex project structures.
Advanced Usage
Specifying File Extensions
To scan for file types other than .html
, use the extensions
option:
import { autoResolve } from '@serverduty/rollup-auto-resolve'
const inputOptions = autoResolve('path/to/your/directory', {
extensions: ['.html', '.htm'] // Add any file extensions you need
})
// Use inputOptions in your vite.config.js
This configuration will include both .html
and .htm
files in the search.
How It Works
-
autoResolve
recursively scans the provided path(s) for files with the specified extensions. - It then creates an object where each key-value pair corresponds to a found file. The key is derived from the file's relative path, replacing path separators with underscores and removing file extensions.
- This object is formatted to be directly used in Vite's
vite.config.js
, underbuild.rollupOptions.input
, automating the setup for multi-page applications.
vite.config.js
Example in Here's how you might use autoResolve
in your Vite configuration:
import { defineConfig } from 'vite'
import { autoResolve } from '@serverduty/rollup-auto-resolve'
export default defineConfig({
build: {
rollupOptions: {
input: autoResolve('src'),
// Other Rollup options as needed
},
},
// Other Vite configurations
})
This setup automatically resolves all HTML files within the src
directory to be entry points for Vite's build process.
License
The MIT License (MIT)
Copyright 2023 ServerDuty Limited
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.