{"id":13725759,"url":"https://github.com/kentcdodds/import-all.macro","last_synced_at":"2025-04-05T12:05:30.217Z","repository":{"id":57272953,"uuid":"103864868","full_name":"kentcdodds/import-all.macro","owner":"kentcdodds","description":"A babel-macro that allows you to import all files that match a glob","archived":false,"fork":false,"pushed_at":"2021-01-25T23:14:18.000Z","size":64,"stargazers_count":178,"open_issues_count":0,"forks_count":17,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-29T11:07:00.926Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://npm.im/import-all.macro","language":"JavaScript","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/kentcdodds.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-17T21:57:54.000Z","updated_at":"2024-06-14T16:00:07.000Z","dependencies_parsed_at":"2022-09-13T09:04:08.646Z","dependency_job_id":null,"html_url":"https://github.com/kentcdodds/import-all.macro","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kentcdodds%2Fimport-all.macro","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kentcdodds%2Fimport-all.macro/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kentcdodds%2Fimport-all.macro/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kentcdodds%2Fimport-all.macro/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kentcdodds","download_url":"https://codeload.github.com/kentcdodds/import-all.macro/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247332604,"owners_count":20921853,"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-08-03T01:02:34.091Z","updated_at":"2025-04-05T12:05:30.196Z","avatar_url":"https://github.com/kentcdodds.png","language":"JavaScript","readme":"\u003cdiv align=\"center\"\u003e\n\u003ch1\u003eimport-all.macro\u003c/h1\u003e\n\n\u003cp\u003eA babel-plugin-macro that allows you to import all files that match a glob\u003c/p\u003e\n\u003c/div\u003e\n\n---\n\n\u003c!-- prettier-ignore-start --\u003e\n[![Build Status][build-badge]][build]\n[![Code Coverage][coverage-badge]][coverage]\n[![version][version-badge]][package]\n[![downloads][downloads-badge]][npmtrends]\n[![MIT License][license-badge]][license]\n\n\u003c!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --\u003e\n[![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-)\n\u003c!-- ALL-CONTRIBUTORS-BADGE:END --\u003e\n[![PRs Welcome][prs-badge]][prs]\n[![Code of Conduct][coc-badge]][coc]\n[![Babel Macro][macros-badge]][babel-plugin-macros]\n\u003c!-- prettier-ignore-end --\u003e\n\n## The problem\n\nYou want to import all files that match a `glob` without having to import them\nindividually.\n\n## This solution\n\nThis is a [babel-plugin-macro][babel-plugin-macros] which allows you to import\nfiles that match a glob. It supports `import` statements for synchronous\nresolution as well as dynamic `import()` for deferred resolution (for code\nsplitting with react router for example).\n\n## Table of Contents\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [Caveats](#caveats)\n- [Inspiration](#inspiration)\n- [Other Solutions](#other-solutions)\n- [Issues](#issues)\n  - [🐛 Bugs](#-bugs)\n  - [💡 Feature Requests](#-feature-requests)\n- [Contributors ✨](#contributors-)\n- [LICENSE](#license)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## Installation\n\nThis module is distributed via [npm][npm] which is bundled with [node][node] and\nshould be installed as one of your project's `devDependencies`:\n\n```\nnpm install --save-dev import-all.macro\n```\n\n## Usage\n\nOnce you've\n[configured `babel-plugin-macros`](https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/user.md)\nyou can import/require `import-all.macro`.\n\nThe `importAll` functions accept a [`glob`][glob] and will transpile your code\nto import statements/dynamic imports for each file that matches the given glob.\n\nLet's imagine you have a directory called `my-files` with the files `a.js`,\n`b.js`, `c.js`, and `d.js`.\n\nHere are a few before/after examples:\n\n\u003c!-- SNAP_TO_README:START --\u003e\n\u003c!-- This section is generated by the other/snap-to-readme.js script. --\u003e\n\u003c!-- Do not edit directly. --\u003e\n\n**`importAll` uses dynamic import: README:1 `importAll` uses dynamic import**\n\n```javascript\nimport importAll from 'import-all.macro'\n\ndocument.getElementById('load-stuff').addEventListener('click', () =\u003e {\n  importAll('./files/*.js').then(all =\u003e {\n    console.log(all)\n  })\n})\n\n      ↓ ↓ ↓ ↓ ↓ ↓\n\ndocument.getElementById('load-stuff').addEventListener('click', () =\u003e {\n  Promise.all([\n    import('./files/a.js'),\n    import('./files/b.js'),\n    import('./files/c.js'),\n    import('./files/d.js'),\n  ])\n    .then(function importAllHandler(importVals) {\n      return {\n        './files/a.js': importVals[0],\n        './files/b.js': importVals[1],\n        './files/c.js': importVals[2],\n        './files/d.js': importVals[3],\n      }\n    })\n    .then(all =\u003e {\n      console.log(all)\n    })\n})\n```\n\n**`importAll.sync` uses static imports: README:2 `importAll.sync` uses static\nimports**\n\n```javascript\nimport importAll from 'import-all.macro'\n\nconst a = importAll.sync('./files/*.js')\n\n      ↓ ↓ ↓ ↓ ↓ ↓\n\nimport * as _filesAJs from './files/a.js'\nimport * as _filesBJs from './files/b.js'\nimport * as _filesCJs from './files/c.js'\nimport * as _filesDJs from './files/d.js'\nconst a = {\n  './files/a.js': _filesAJs,\n  './files/b.js': _filesBJs,\n  './files/c.js': _filesCJs,\n  './files/d.js': _filesDJs,\n}\n```\n\n**`importAll.deferred` gives an object with dynamic imports: README:3\n`importAll.deferred` gives an object with dynamic imports**\n\n```javascript\nimport importAll from 'import-all.macro'\n\nconst routes = importAll.deferred('./files/*.js')\n\n      ↓ ↓ ↓ ↓ ↓ ↓\n\nconst routes = {\n  './files/a.js': function () {\n    return import('./files/a.js')\n  },\n  './files/b.js': function () {\n    return import('./files/b.js')\n  },\n  './files/c.js': function () {\n    return import('./files/c.js')\n  },\n  './files/d.js': function () {\n    return import('./files/d.js')\n  },\n}\n```\n\n\u003c!-- SNAP_TO_README:END --\u003e\n\n**Configure `importAll` to transform import path before generating imports**\n\n`babel-plugin-macros.config.js`:\n\n```javascript\nmodule.exports = {\n  importAll: {\n    transformModulePath(modulePath, importingPath) {\n      const projectRoot = path.join(__dirname, '../../')\n      const modulePathWithoutExt = modulePath.replace(/\\.js$/, '')\n      const absolutePath = path.resolve(\n        path.dirname(importingPath),\n        modulePathWithoutExt,\n      )\n      const pathRelativeToRoot = path.relative(projectRoot, absolutePath)\n      return pathRelativeToRoot\n    },\n  },\n}\n```\n\n```javascript\nimport importAll from 'import-all.macro'\nconst a = importAll.sync('./files/*.js')\n      ↓ ↓ ↓ ↓ ↓ ↓\nimport * as _filesA from './files/a'\nimport * as _filesB from './files/b'\nimport * as _filesC from './files/c'\nimport * as _filesD from './files/d'\nconst a = {\n  './files/a': _filesA,\n  './files/b': _filesB,\n  './files/c': _filesC,\n  './files/d': _filesD,\n}\n```\n\n## Caveats\n\nSome static analysis tools (like ESLint, Flow, and Jest) wont like this very\nmuch without a little additional work. So Jest's watch mode may not pick up all\nyour tests that are relevant based on changes and some ESLint plugins (like\n`eslint-plugin-import`) will probably fail on this.\n\n## Inspiration\n\n[Sunil Pai's tweet][sunil-tweet]\n\n## Other Solutions\n\nI'm not aware of any, if you are please [make a pull request][prs] and add it\nhere!\n\n## Issues\n\n_Looking to contribute? Look for the [Good First Issue][good-first-issue]\nlabel._\n\n### 🐛 Bugs\n\nPlease file an issue for bugs, missing documentation, or unexpected behavior.\n\n[**See Bugs**][bugs]\n\n### 💡 Feature Requests\n\nPlease file an issue to suggest new features. Vote on feature requests by adding\na 👍. This helps maintainers prioritize what to work on.\n\n[**See Feature Requests**][requests]\n\n## Contributors ✨\n\nThanks goes to these people ([emoji key][emojis]):\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore-start --\u003e\n\u003c!-- markdownlint-disable --\u003e\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://kentcdodds.com\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/1500684?v=3\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eKent C. Dodds\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/kentcdodds/import-all.macro/commits?author=kentcdodds\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"https://github.com/kentcdodds/import-all.macro/commits?author=kentcdodds\" title=\"Documentation\"\u003e📖\u003c/a\u003e \u003ca href=\"#infra-kentcdodds\" title=\"Infrastructure (Hosting, Build-Tools, etc)\"\u003e🚇\u003c/a\u003e \u003ca href=\"https://github.com/kentcdodds/import-all.macro/commits?author=kentcdodds\" title=\"Tests\"\u003e⚠️\u003c/a\u003e\u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"http://jonathantneal.com\"\u003e\u003cimg src=\"https://avatars0.githubusercontent.com/u/188426?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eJonathan Neal\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/kentcdodds/import-all.macro/commits?author=jonathantneal\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://fatfisz.com\"\u003e\u003cimg src=\"https://avatars0.githubusercontent.com/u/6004414?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eRafał Ruciński\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/kentcdodds/import-all.macro/issues?q=author%3Afatfisz\" title=\"Bug reports\"\u003e🐛\u003c/a\u003e \u003ca href=\"https://github.com/kentcdodds/import-all.macro/commits?author=fatfisz\" title=\"Code\"\u003e💻\u003c/a\u003e\u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://stackshare.io/jdorfman/decisions\"\u003e\u003cimg src=\"https://avatars1.githubusercontent.com/u/398230?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eJustin Dorfman\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"#fundingFinding-jdorfman\" title=\"Funding Finding\"\u003e🔍\u003c/a\u003e\u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://michaeldeboey.be\"\u003e\u003cimg src=\"https://avatars3.githubusercontent.com/u/6643991?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eMichaël De Boey\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/kentcdodds/import-all.macro/commits?author=MichaelDeBoey\" title=\"Code\"\u003e💻\u003c/a\u003e\u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://chengjianhua.github.io\"\u003e\u003cimg src=\"https://avatars0.githubusercontent.com/u/10795207?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eJianhua Cheng\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/kentcdodds/import-all.macro/commits?author=chengjianhua\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"https://github.com/kentcdodds/import-all.macro/commits?author=chengjianhua\" title=\"Tests\"\u003e⚠️\u003c/a\u003e \u003ca href=\"https://github.com/kentcdodds/import-all.macro/commits?author=chengjianhua\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\u003c!-- markdownlint-enable --\u003e\n\u003c!-- prettier-ignore-end --\u003e\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\nThis project follows the [all-contributors][all-contributors] specification.\nContributions of any kind welcome!\n\n## LICENSE\n\nMIT\n\n\u003c!-- prettier-ignore-start --\u003e\n[npm]: https://www.npmjs.com\n[node]: https://nodejs.org\n[build-badge]: https://img.shields.io/travis/kentcdodds/import-all.macro.svg?style=flat-square\n[build]: https://travis-ci.org/kentcdodds/import-all.macro\n[coverage-badge]: https://img.shields.io/codecov/c/github/kentcdodds/import-all.macro.svg?style=flat-square\n[coverage]: https://codecov.io/github/kentcdodds/import-all.macro\n[version-badge]: https://img.shields.io/npm/v/import-all.macro.svg?style=flat-square\n[package]: https://www.npmjs.com/package/import-all.macro\n[downloads-badge]: https://img.shields.io/npm/dm/import-all.macro.svg?style=flat-square\n[npmtrends]: http://www.npmtrends.com/import-all.macro\n[license-badge]: https://img.shields.io/npm/l/import-all.macro.svg?style=flat-square\n[license]: https://github.com/kentcdodds/import-all.macro/blob/master/LICENSE\n[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square\n[prs]: http://makeapullrequest.com\n[donate-badge]: https://img.shields.io/badge/$-support-green.svg?style=flat-square\n[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square\n[coc]: https://github.com/kentcdodds/import-all.macro/blob/master/other/CODE_OF_CONDUCT.md\n[macros-badge]: https://img.shields.io/badge/babel--macro-%F0%9F%8E%A3-f5da55.svg?style=flat-square\n[babel-plugin-macros]: https://github.com/kentcdodds/babel-plugin-macros\n[emojis]: https://github.com/all-contributors/all-contributors#emoji-key\n[all-contributors]: https://github.com/all-contributors/all-contributors\n[bugs]: https://github.com/kentcdodds/import-all.macro/issues?utf8=%E2%9C%93\u0026q=is%3Aissue+is%3Aopen+sort%3Acreated-desc+label%3Abug\n[requests]: https://github.com/kentcdodds/import-all.macro/issues?utf8=%E2%9C%93\u0026q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement\n[good-first-issue]: https://github.com/kentcdodds/import-all.macro/issues?utf8=%E2%9C%93\u0026q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement+label%3A%22good+first+issue%22\n\n[glob]: https://www.npmjs.com/package/glob\n[sunil-tweet]: https://twitter.com/threepointone/status/908290510225330176\n\u003c!-- prettier-ignore-end --\u003e\n","funding_links":[],"categories":["Macros","babel","JavaScript"],"sub_categories":["Code generation","macros"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkentcdodds%2Fimport-all.macro","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkentcdodds%2Fimport-all.macro","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkentcdodds%2Fimport-all.macro/lists"}