A Rollup plugin that uses esbuild as a fast TypeScript/JavaScript compiler and minifier, replacing traditional tools like rollup-plugin-typescript2 and rollup-plugin-terser
—
The experimental dependency optimization feature provides pre-bundling capabilities for external dependencies using esbuild's fast bundling engine, eliminating the need for additional CommonJS and Node.js resolution plugins.
Configure which dependencies should be pre-bundled and optimized during the build process.
interface OptimizeDepsOptions {
/** List of dependency names to include in optimization */
include: string[];
/** List of dependency names to exclude from optimization */
exclude?: string[];
/** Working directory for dependency resolution */
cwd: string;
/** Additional esbuild options for the optimization build */
esbuildOptions?: EsbuildBuildOptions;
/** Enable source map generation for optimized dependencies */
sourceMap: boolean;
}
interface OptimizeDepsResult {
/** Map of optimized dependencies to their bundled file paths */
optimized: Map<string, { file: string }>;
/** Directory where optimized dependencies are cached */
cacheDir: string;
}
// Re-exported from esbuild for reference
interface EsbuildBuildOptions {
entryPoints?: string[];
bundle?: boolean;
splitting?: boolean;
format?: "iife" | "cjs" | "esm";
target?: string | string[];
outdir?: string;
outfile?: string;
platform?: "browser" | "node" | "neutral";
external?: string[];
loader?: { [ext: string]: Loader };
define?: { [key: string]: string };
inject?: string[];
banner?: { [type: string]: string };
footer?: { [type: string]: string };
metafile?: boolean;
write?: boolean;
plugins?: any[];
}Usage Examples:
import esbuild from "rollup-plugin-esbuild";
// Basic dependency optimization
export default {
plugins: [
esbuild({
optimizeDeps: {
include: ["react", "react-dom"]
}
})
]
};
// Advanced optimization with exclusions
export default {
plugins: [
esbuild({
optimizeDeps: {
include: ["lodash", "moment", "axios"],
exclude: ["moment/locale/*"],
esbuildOptions: {
target: "es2020",
format: "esm"
}
}
})
]
};
// Optimization with custom esbuild options
export default {
plugins: [
esbuild({
optimizeDeps: {
include: ["vue", "vue-router"],
esbuildOptions: {
define: {
"process.env.NODE_ENV": '"production"'
},
external: ["node:*"]
}
}
})
]
};Specify which dependencies should be optimized and pre-bundled.
interface IncludeOptions {
/** Array of dependency names to optimize */
include: string[];
}Behavior:
node_modulesnode_modules/.optimize_depsExamples:
{
include: [
"react", // Regular package
"react-dom",
"@babel/core", // Scoped package
"lodash/debounce" // Specific export
]
}Specify dependencies that should be excluded from optimization even if they're dependencies of included packages.
interface ExcludeOptions {
/** Array of dependency names to exclude from optimization */
exclude?: string[];
}Usage:
{
include: ["react", "react-dom"],
exclude: [
"react-dom/server", // Exclude server-side rendering
"react/jsx-dev-runtime" // Exclude development runtime
]
}The optimization process creates a cache directory for optimized dependencies.
interface CacheInfo {
/** Default cache directory location */
cacheDir: string; // "node_modules/.optimize_deps"
}Behavior:
react.js, vue.js)Provide additional esbuild configuration for the optimization build process.
interface OptimizationBuildOptions {
/** Additional esbuild options for optimization */
esbuildOptions?: {
/** Target environment for optimized code */
target?: string | string[];
/** Output format for optimized bundles */
format?: "esm" | "cjs";
/** Global replacements for optimized code */
define?: { [key: string]: string };
/** External dependencies that shouldn't be bundled */
external?: string[];
/** Custom loaders for specific file types */
loader?: { [ext: string]: Loader };
/** Code injection for polyfills or setup */
inject?: string[];
/** Platform-specific optimizations */
platform?: "browser" | "node" | "neutral";
};
}Common configurations:
// Browser-optimized dependencies
{
esbuildOptions: {
target: ["es2020", "chrome80"],
format: "esm",
platform: "browser",
define: {
"process.env.NODE_ENV": '"production"'
}
}
}
// Node.js optimized dependencies
{
esbuildOptions: {
target: "node16",
format: "cjs",
platform: "node",
external: ["node:*"]
}
}The optimization integrates seamlessly with the Rollup build process through plugin lifecycle hooks.
interface OptimizationLifecycle {
/** Runs during buildStart phase */
buildStart(): Promise<void>;
/** Intercepts module resolution */
resolveId(id: string, importer?: string): string | null;
}Behavior:
buildStartDependency optimization provides several performance improvements:
Build Time Benefits:
Runtime Benefits:
Bundle Size Benefits:
Experimental Status:
Compatibility:
Troubleshooting:
exclude option for problematic dependenciesInstall with Tessl CLI
npx tessl i tessl/npm-rollup-plugin-esbuild