A Babel Macro that replicates Webpack's require.context() for use in Storybook and other non-Webpack environments. Current stable version is 1.2.2. The package is maintained by the Storybook team and depends on babel-plugin-macros. Key differentiator: it allows using require.context() syntax in Storybook config files without Webpack, enabling automatic file importing for stories. The macro follows symlinks and supports req.resolve(key). Ships TypeScript definitions. Compatible with Storybook 3 and 4.
npm install require-context.macroVerified import paths — ran on the pinned version, not inferred.
Shows how to use require-context.macro to load stories dynamically in Storybook config.
Install babel-plugin-macros: yarn add -D babel-plugin-macros
Add 'macros' to plugins array in your babel config (e.g., .babelrc: { "plugins": ["macros"] })Upgrade to v1.2.1 or later which fixes this bug.
For Storybook 5+, consider using @storybook/addon-storysource or other methods.
Upgrade to v1.2.1 or later: yarn upgrade require-context.macro@1.2.1
Import requireContext from 'require-context.macro' and use that function.