Unplugin TypeSugar Bundler Integrations
unplugin-typesugar provides essential bundler integrations for `typesugar`, a TypeScript macro expansion library. It enables compile-time macro processing across various build tools, including Vite, Webpack, esbuild, and Rollup. Leveraging the `unplugin` ecosystem, it ensures broad compatibility and simplifies integration into existing JavaScript and TypeScript projects. The current stable version is 0.1.1, marked as an initial release candidate and first stable patch, indicating active, early-stage development with frequent updates addressing stability and feature enhancements. A key differentiator is its ability to expand TypeScript macros before the code enters the bundler's optimization pipeline, ensuring that macro-transformed code is fully optimized. It requires TypeScript 5.0 or newer as a peer dependency, aligning with modern TypeScript tooling practices.
Common errors
-
Plugin 'unplugin-typesugar' failed to load. Cannot find module 'unplugin-typesugar/vite' (or /webpack, /esbuild, /rollup)
cause The specific bundler integration path is incorrect, or the `unplugin-typesugar` package is not correctly installed.fixVerify that `unplugin-typesugar` is installed (`npm install unplugin-typesugar` or `pnpm add unplugin-typesugar`) and that the import path matches your bundler (e.g., `import typesugar from 'unplugin-typesugar/vite';`). -
TypeError: Cannot read properties of undefined (reading 'default') in Webpack configuration
cause Attempting to use an ESM import statement (`import typesugar from '...'`) or incorrectly accessing the CommonJS module's export in a Webpack configuration file, which typically runs in a CommonJS environment.fixFor Webpack, use `const typesugar = require('unplugin-typesugar/webpack').default;` to correctly import the plugin. -
Property 'getDiagnostics' does not exist on type 'Program' (or similar TypeScript API errors during build)
cause The installed TypeScript version is incompatible with `unplugin-typesugar`, which requires TypeScript 5.0 or newer.fixUpgrade your project's `typescript` dependency to `typescript@>=5.0.0` to ensure compatibility with the plugin's internal TypeScript program creation.
Warnings
- gotcha unplugin-typesugar is in its early development stages (current version 0.1.1). APIs may undergo rapid changes in minor or patch releases prior to reaching a stable 1.0 version.
- breaking This plugin requires TypeScript version 5.0.0 or higher due to its reliance on newer TypeScript compiler APIs for macro transformation.
- breaking Initial release candidates (prior to 0.1.1) experienced a build crash with a 'start < 0' error. This was resolved in version 0.1.1.
Install
-
npm install unplugin-typesugar -
yarn add unplugin-typesugar -
pnpm add unplugin-typesugar
Imports
- typesugar
const typesugar = require("unplugin-typesugar/vite");import typesugar from "unplugin-typesugar/vite";
- typesugar
import typesugar from "unplugin-typesugar/webpack";
const typesugar = require("unplugin-typesugar/webpack").default; - TypesugarPluginOptions
import type { TypesugarPluginOptions } from 'unplugin-typesugar';
Quickstart
import typesugar from "unplugin-typesugar/vite";
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
typesugar({
verbose: true,
include: /src\/.*\.tsx?$/,
exclude: /\.test\.ts$/,
diskCache: true, // Enable disk cache for faster rebuilds
strict: true // Typecheck expanded output at build end
}),
],
});