{"id":22525,"library":"unplugin-parcel-macros","title":"unplugin-parcel-macros","description":"Unplugin plugin that brings Parcel's macro system to webpack, Vite, Rollup, esbuild, Next.js, and more. Macros are JavaScript/TypeScript functions that execute at build time, returning values that get inlined into the bundle, enabling compile-time computation, code generation, and asset creation without custom plugins. Version 0.2.0, actively maintained, requires Node >= 18 and an import attribute (with {type: 'macro'}). Compared to babel macros or webpack loader-based approaches, it leverages Unplugin for universal integration and follows the TC39 import attributes proposal.","status":"active","version":"0.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/devongovett/unplugin-parcel-macros","tags":["javascript","typescript"],"install":[{"cmd":"npm install unplugin-parcel-macros","lang":"bash","label":"npm"},{"cmd":"yarn add unplugin-parcel-macros","lang":"bash","label":"yarn"},{"cmd":"pnpm add unplugin-parcel-macros","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Unplugin is the core library providing the plugin interface for all bundlers","package":"unplugin","optional":false}],"imports":[{"note":"ESM-only; default export is an object with .webpack(), .vite(), .rollup(), .esbuild() methods.","wrong":"const macros = require('unplugin-parcel-macros')","symbol":"default","correct":"import macros from 'unplugin-parcel-macros'"},{"note":"Call the method to get the plugin instance. Used in both plain Webpack and Next.js.","wrong":"macros.webpack","symbol":"webpack","correct":"macros.webpack()"},{"note":"Call the method to get the Vite plugin.","wrong":"macros.vite","symbol":"vite","correct":"macros.vite()"}],"quickstart":{"code":"// vite.config.js\nimport macros from 'unplugin-parcel-macros';\nimport { defineConfig } from 'vite';\n\nexport default defineConfig({\n  plugins: [\n    macros.vite()\n  ]\n});\n\n// src/index.js\nimport regexgen from 'regexgen' with { type: 'macro' };\nconst regex = regexgen(['foo', 'bar', 'baz']);\nexport default regex;","lang":"typescript","description":"Shows how to add the Vite plugin and use a macro with the import attribute to generate a regex at build time."},"warnings":[{"fix":"Ensure your toolchain supports the syntax or use a transpiler like SWC or Babel that strips the attribute.","message":"Import attributes syntax (with {type: 'macro'}) is not supported in older bundlers or Node versions without transpilation.","severity":"gotcha","affected_versions":"< Node 18, bundlers without import attributes support"},{"fix":"Use with {type: 'macro'} as per the import attributes proposal.","message":"Macros must be imported with the exact attribute with {type: 'macro'}. Using a non-standard syntax like assert {type: 'macro'} will not work.","severity":"breaking","affected_versions":"all"},{"fix":"Ensure macro arguments are compile-time constants or expressions evaluable at build time.","message":"The macro module is executed at build time and cannot access runtime variables from the calling module.","severity":"gotcha","affected_versions":"all"},{"fix":"Keep the library updated to adapt to spec changes.","message":"Import attributes are still a Stage 3 proposal and may change in the future.","severity":"deprecated","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Use import syntax or convert your project to ESM (type:'module' in package.json).","cause":"Using require() to import the ESM-only package","error":"Error: require() of ES Module not supported"},{"fix":"Use a tool that supports import attributes (e.g., Node >= 18, modern bundlers) or transpile with SWC/Babel plugin.","cause":"The import attribute syntax is not parsed by the current bundler/transpiler","error":"Missing: with {type: 'macro'} - Unexpected token","error_s":"SyntaxError: Unexpected token '{'"},{"fix":"Run npm install unplugin-parcel-macros --save-dev","cause":"Package not installed","error":"Error: Cannot find module 'unplugin-parcel-macros'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}