1.0.5 • Published 1 year ago
@arbz/glob-import-loader v1.0.5
@arbz/glob-import-loader
A Webpack loader that enables glob imports for your project. With this loader, you can easily import multiple files matching a glob pattern and get an array of objects containing the module and its relative path.
Installation
npm install @arbz/glob-import-loaderor
yarn add @arbz/glob-import-loaderUsage
Add the loader to your Webpack configuration:
module.exports = {
module: {
rules: [
{
test: /\.glob-import-allowed\.(js|mjs|jsx|ts|tsx)?$/,
loader: '@arbz/glob-import-loader',
enforce: 'pre',
},
],
},
};Example
Given the following file structure:
src/
├── components/
│ ├── Header.jsx
│ ├── Footer.jsx
│ └── Sidebar.tsxYou can use the loader to import multiple files:
import modules from './components/**/*.{jsx,tsx,ts}';
console.log(modules);
// Output:
// [
// { module: Header, path: './Header.jsx' },
// { module: Footer, path: './Footer.jsx' },
// { module: Sidebar, path: './Sidebar.tsx' },
// ]Notes
- This loader supports only files with the
.glob-import-allowedsuffix by default (e.g.,index.glob-import-allowed.js). - The
pathproperty in the resulting array is the relative path from the glob import location.
License
This project is licensed under the MIT License.