<Zhenya>
← Сниппеты

How to import multiple files

Pattern matching + subfolders

Node.js

Для node.js можно использовать fast-glob

import fg from 'fast-glob';
const myFolder = path.join(process.cwd(), '/content');
const entries = await fg([`${myFolder}/**/*.{md,mdx}`]);

Webpack

Official docs

syntax:

require.context(
directory,
(useSubdirectories = true),
(regExp = /^\.\/.*$/),
(mode = 'sync')
);

example:

const imagesCtx = require.context('/public/images', false, /\.(png|jpe?g|svg|webp|avif)$/);

Vite

Official docs

const modules = import.meta.glob('./dir/*.js');
tsconfig.json
{
"compilerOptions": {
"types": ["vite/client"]
}
}

Паттерны

  • some-folder/*.json - все файлы json в папке some-folder
  • some-folder/**/*.json - включая вложенные папки
  • .{md,mdx} - несколько расширений
  • /\.(png|jpe?g|svg|webp|avif)$/ - несколько расширений через regExp