unplugin-parcel-macros
raw JSON → 0.2.0 verified Mon Apr 27 auth: no javascript
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.
Common errors
error Error: require() of ES Module not supported ↓
cause Using require() to import the ESM-only package
fix
Use import syntax or convert your project to ESM (type:'module' in package.json).
error Missing: with {type: 'macro'} - Unexpected token ↓
cause The import attribute syntax is not parsed by the current bundler/transpiler
fix
Use a tool that supports import attributes (e.g., Node >= 18, modern bundlers) or transpile with SWC/Babel plugin.
error Error: Cannot find module 'unplugin-parcel-macros' ↓
cause Package not installed
fix
Run npm install unplugin-parcel-macros --save-dev
Warnings
gotcha Import attributes syntax (with {type: 'macro'}) is not supported in older bundlers or Node versions without transpilation. ↓
fix Ensure your toolchain supports the syntax or use a transpiler like SWC or Babel that strips the attribute.
breaking Macros must be imported with the exact attribute with {type: 'macro'}. Using a non-standard syntax like assert {type: 'macro'} will not work. ↓
fix Use with {type: 'macro'} as per the import attributes proposal.
gotcha The macro module is executed at build time and cannot access runtime variables from the calling module. ↓
fix Ensure macro arguments are compile-time constants or expressions evaluable at build time.
deprecated Import attributes are still a Stage 3 proposal and may change in the future. ↓
fix Keep the library updated to adapt to spec changes.
Install
npm install unplugin-parcel-macros yarn add unplugin-parcel-macros pnpm add unplugin-parcel-macros Imports
- default wrong
const macros = require('unplugin-parcel-macros')correctimport macros from 'unplugin-parcel-macros' - webpack wrong
macros.webpackcorrectmacros.webpack() - vite wrong
macros.vitecorrectmacros.vite()
Quickstart
// vite.config.js
import macros from 'unplugin-parcel-macros';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
macros.vite()
]
});
// src/index.js
import regexgen from 'regexgen' with { type: 'macro' };
const regex = regexgen(['foo', 'bar', 'baz']);
export default regex;