ESBuild plugin (v1.1.0, active development) that executes TypeScript/JavaScript functions at compile time and replaces their calls with the computed result, enabling compile-time code generation and optimization. Differentiates from Babel macros by being esbuild-native and ESM-only, supporting async macros, and serializing complex types like Map, Set, Date, RegExp, and web API objects. Released on npm, with TypeScript type declarations included.
npm install use-macroNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Basic esbuild integration: registers the plugin and uses a macro to inline a compile-time Date.
Use ESM imports ('import' instead of 'require') and ensure project is configured for ESM (e.g., type: 'module' in package.json or .mjs extension).Add 'use macro'; as the first statement in any function intended as a macro. The directive is a string literal, not a pragma comment.
Move all required imports inside the macro function. The macro cannot reference variables from the enclosing module scope.
Update to version 1.1.0 or later.
Use import statement instead: import { esbuildPluginUseMacro } from 'use-macro'Use named import: import { esbuildPluginUseMacro } from 'use-macro' and then call it: esbuildPluginUseMacro(options)Ensure esbuild is set to bundle the code. If using tsup, enable esbuild bundling. For standalone transformer, provide a custom module resolution.