@anatine/esbuild-decorators
raw JSON → 1.0.0 verified Mon Apr 27 auth: no javascript
An esbuild plugin for TypeScript decorator metadata (emitDecoratorMetadata and experimentalDecorators). Current stable version 1.0.0, maintained on GitHub by reconbot. It handles the tsconfig setting `emitDecoratorMetadata: true` by inspecting .ts files for decorators and transpiling them with the TypeScript compiler, since esbuild does not natively support TypeScript experimental decorators. Releases are infrequent; the plugin is a fork of the anatine organization's esbuildnx plugin with fixed peer dependencies and ESM/CJS module support. Alternatives include `esbuild-plugin-decorator` and `esbuild-decorators-loader`, but this package focuses on simplicity and direct tsconfig integration.
Common errors
error Error: The module 'esbuild-decorators' does not provide a default export ↓
cause Using `import esbuildDecorators from 'esbuild-decorators'` instead of named import.
fix
Use
import { esbuildDecorators } from '@anatine/esbuild-decorators'. error Error: Could not find a declaration file for module '@anatine/esbuild-decorators' ↓
cause Types are not installed or module resolution is misconfigured.
fix
Install the package as a devDependency and ensure 'typescript' is installed. The package ships types.
error TypeError: esbuildDecorators is not a function ↓
cause Using `require('@anatine/esbuild-decorators')` without destructuring.
fix
Use
const { esbuildDecorators } = require('@anatine/esbuild-decorators'). Warnings
breaking This plugin overrides esbuild's TypeScript handling; files with decorators are transpiled by tsc, not esbuild, which breaks bundling speed and may cause mismatches in module resolution. ↓
fix Use only for projects that require experimental decorators; avoid mixing with other esbuild TypeScript plugins.
gotcha The plugin uses a regex to detect decorators, which may not match all edge cases (e.g., decorators with complex arguments, multiline decorators). Test thoroughly. ↓
fix Review the regex pattern and file an issue if your decorator syntax is not detected.
gotcha When `force: true`, all .ts files are transpiled via tsc, which significantly slows down builds. Use only for debugging or small projects. ↓
fix Set `force: false` (default) to let the plugin decide which files need tsc.
deprecated The package name `esbuild-decorators` is used by the npm registry but the import is from `@anatine/esbuild-decorators`. The original `esbuild-decorators` package may be abandoned. ↓
fix Import from '@anatine/esbuild-decorators'.
Install
npm install esbuild-decorators yarn add esbuild-decorators pnpm add esbuild-decorators Imports
- esbuildDecorators wrong
import esbuildDecorators from '@anatine/esbuild-decorators'correctimport { esbuildDecorators } from '@anatine/esbuild-decorators' - esbuildDecorators wrong
const esbuildDecorators = require('@anatine/esbuild-decorators')correctconst { esbuildDecorators } = require('@anatine/esbuild-decorators') - EsbuildDecoratorsOptions
import type { EsbuildDecoratorsOptions } from '@anatine/esbuild-decorators'
Quickstart
import { build } from 'esbuild';
import { esbuildDecorators } from '@anatine/esbuild-decorators';
async function main() {
await build({
entryPoints: ['src/index.ts'],
outfile: 'dist/index.js',
bundle: true,
platform: 'node',
target: 'node14',
plugins: [
esbuildDecorators({
tsconfig: './tsconfig.json',
cwd: process.cwd(),
force: false,
tsx: false,
}),
],
});
}
main().catch(console.error);