{"id":15284301,"url":"https://github.com/ttempaa/bun-plugin-glob-import","last_synced_at":"2025-12-28T17:35:08.672Z","repository":{"id":249569597,"uuid":"831131567","full_name":"ttempaa/bun-plugin-glob-import","owner":"ttempaa","description":"A Bun plugin that enables importing modules using glob patterns","archived":false,"fork":false,"pushed_at":"2024-07-22T01:59:52.000Z","size":11,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-22T11:32:12.616Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ttempaa.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-19T18:26:23.000Z","updated_at":"2024-12-20T13:53:21.000Z","dependencies_parsed_at":"2024-10-14T14:41:55.366Z","dependency_job_id":"f0638708-e33d-415a-ab22-06f0310857ce","html_url":"https://github.com/ttempaa/bun-plugin-glob-import","commit_stats":null,"previous_names":["ttempaa/bun-plugin-glob-import"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttempaa%2Fbun-plugin-glob-import","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttempaa%2Fbun-plugin-glob-import/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttempaa%2Fbun-plugin-glob-import/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttempaa%2Fbun-plugin-glob-import/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ttempaa","download_url":"https://codeload.github.com/ttempaa/bun-plugin-glob-import/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243526953,"owners_count":20305115,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-30T14:53:12.978Z","updated_at":"2025-12-28T17:35:08.666Z","avatar_url":"https://github.com/ttempaa.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bun-plugin-glob-import\n\nThis plugin allows you to import modules using glob patterns.\n\n[![npm](https://img.shields.io/npm/v/bun-plugin-glob-import.svg)](https://www.npmjs.com/package/bun-plugin-glob-import)\n[![npm](https://img.shields.io/npm/dt/bun-plugin-glob-import.svg)](https://www.npmjs.com/package/bun-plugin-glob-import)\n[![npm](https://img.shields.io/npm/l/bun-plugin-glob-import.svg)](https://www.npmjs.com/package/bun-plugin-glob-import)\n\n## Installation\n\nTo install the plugin, run:\n\n```sh\nbun add -d bun-plugin-glob-import\n```\n\n## Usage\n\n### As a Bundler Plugin\n\nAdd the plugin to your build configuration:\n\n```ts\nimport { globImportPlugin } from 'bun-plugin-glob-import';\n\nawait Bun.build({\n  plugins: [globImportPlugin()],\n});\n```\n\n### As a Runtime Plugin\n\nRegister the plugin in your `bunfig.toml` to use it at runtime:\n\n```toml\npreload = [\"bun-plugin-glob-import/register\"]\n```\n\n## Example\n\nImagine a project with the following file structure:\n\n```\nsrc/\n├── commands\n│   ├── special\n│   │   ├── list.ts\n│   │   └── ping.ts\n│   ├── create.ts\n│   ├── delete.ts\n│   ├── get.ts\n│   └── update.ts\n└── index.ts\n```\n\nEach command file, like `create.ts`, has a default export:\n\n```ts\nexport default function create() {\n  console.log('Created');\n}\n```\n\nIn your `index.ts` file, you can import these modules using a glob pattern. The plugin provides the result in two formats: a simple array or a path-keyed object.\n\n### Import as an Array\n\nUsing the default export, you get an array of all imported modules. This is useful when you just need to iterate over them without referencing their original file paths.\n\n```ts\nimport commands from './commands/**/*.ts';\n\nconsole.log(commands);\n/* =\u003e [\n  { default: [Function: update] },\n  { default: [Function: get] },\n  { default: [Function: create] },\n  { default: [Function: delete] },\n  { default: [Function: list] },\n  { default: [Function: ping] },\n] */\n```\n\n### Import as an Object\n\nUsing the named export `asObject`, you get an object where keys are the relative file paths and values are the modules. This is ideal when you need to map modules back to their source files.\n\n```ts\nimport { asObject as commands } from './commands/**/*.ts';\n\nconsole.log(commands);\n/* =\u003e {\n  'commands/update.ts': { default: [Function: update] },\n  'commands/get.ts': { default: [Function: get] },\n  'commands/create.ts': { default: [Function: create] },\n  'commands/delete.ts': { default: [Function: delete] },\n  'commands/special/list.ts': { default: [Function: list] },\n  'commands/special/ping.ts': { default: [Function: ping] }\n} */\n```\n\n## Important Note on Import Syntax\n\nThe static `import ... from ...` syntax is processed at build time and works perfectly when using the Bun bundler.\nFor runtime usage (with preload) or for code that should work universally in both environments, you **must** use a dynamic import:\n\n```ts\nconst { default: commands, asObject } = await import('./commands/**/*.ts');\n```\n\n## Type Definitions\n\nTo avoid TypeScript errors when importing with a glob pattern, add the types to your `tsconfig.json`:\n\n```json\n{\n  \"compilerOptions\": {\n    \"types\": [\"bun-plugin-glob-import/types\"]\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fttempaa%2Fbun-plugin-glob-import","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fttempaa%2Fbun-plugin-glob-import","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fttempaa%2Fbun-plugin-glob-import/lists"}