This plugin lets you import CoffeeScript files when bundling with Esbuild
Transform and bundle .coffee and .litcoffee files seamlessly with esbuild's lightning-fast build system.
- Installation
- Quick Start
- Usage Examples
- Configuration Options
- Advanced Usage
- Error Handling
- Migration Guide
- Troubleshooting
- Contributing
- License
# npm
npm install --save-dev esbuild-coffeescript esbuild
# pnpm
pnpm add --dev esbuild-coffeescript esbuildThis plugin is tested against Node.js versions 20.x, 22.x, and 24.x.
Create a CoffeeScript file:
main.coffee
answer = 42
console.log("the answer is #{answer}")build.js
import coffeeScriptPlugin from "esbuild-coffeescript";
import esbuild from "esbuild";
esbuild
.build({
entryPoints: ["main.coffee"],
bundle: true,
outfile: "out.js",
plugins: [coffeeScriptPlugin()],
})
.catch(() => process.exit(1));The above CoffeeScript compiles to:
(function () {
var answer;
answer = 42;
return console.log("the answer is " + answer);
}).call(this);The plugin automatically detects and processes .litcoffee files:
docs.litcoffee
# My Application
This is documentation written in Markdown with embedded CoffeeScript:
class Calculator
add: (a, b) -> a + b
multiply: (a, b) -> a * b
calc = new Calculator()
console.log calc.add(5, 3)build.js
esbuild.build({
entryPoints: ["docs.litcoffee"],
bundle: true,
plugins: [coffeeScriptPlugin({ literate: true })],
outfile: "calculator.js",
});models/User.coffee
class User
constructor: (@name, @email) ->
greet: -> "Hello, I'm #{@name}"
module.exports = { User }main.coffee
{User} = require('./models/User.coffee')
user = new User('John', '[email protected]')
console.log user.greet()build.js
esbuild.build({
entryPoints: ["main.coffee"],
bundle: true,
plugins: [coffeeScriptPlugin({ bare: true })],
outfile: "app.js",
format: "esm",
});import coffeeScriptPlugin from "esbuild-coffeescript";
esbuild.build({
entryPoints: ["src/app.coffee"],
bundle: true,
outdir: "dist",
plugins: [coffeeScriptPlugin()],
format: "esm",
target: "es2020",
minify: true,
sourcemap: true,
});Pass configuration options to the plugin constructor:
coffeeScriptPlugin({
bare: true,
inlineMap: true,
// ... other options
});| Option | Type | Default | Description |
|---|---|---|---|
inlineMap |
boolean |
false |
Output source map as base64-encoded string in comment |
filename |
string |
auto-detected | Filename for source map (can include path) |
bare |
boolean |
false |
Output without top-level function safety wrapper |
header |
boolean |
false |
Output "Generated by CoffeeScript" header |
transpile |
object |
undefined |
Babel transpilation options (see CoffeeScript docs) |
ast |
boolean |
false |
Return abstract syntax tree instead of compiled code |
literate |
boolean |
auto-detected | Parse as Literate CoffeeScript |
coffeeScriptPlugin({
inlineMap: true,
bare: true,
header: true,
});coffeeScriptPlugin({
bare: true,
header: false,
});coffeeScriptPlugin({
bare: true,
transpile: {
presets: ["@babel/preset-env"],
targets: { node: "18" },
},
});Use alongside esbuild's built-in TypeScript support:
esbuild.build({
entryPoints: ["src/main.ts", "src/utils.coffee"],
bundle: true,
plugins: [coffeeScriptPlugin({ bare: true })],
outdir: "dist",
format: "esm",
});const context = await esbuild.context({
entryPoints: ["src/app.coffee"],
bundle: true,
plugins: [coffeeScriptPlugin()],
outdir: "dist",
});
await context.watch();
console.log("Watching for changes...");While not directly configurable, you can pre-process files with custom extensions:
// For .cjsx or other extensions, rename or copy files first
import { copyFile } from "fs/promises";
await copyFile("src/component.cjsx", "src/component.coffee");
esbuild.build({
entryPoints: ["src/component.coffee"],
plugins: [coffeeScriptPlugin()],
// ...
});The plugin provides detailed error reporting with location information:
invalid.coffee
hello . 8 # Syntax errorError Output:
✘ [ERROR] unexpected .
invalid.coffee:1:6:
1 │ hello . 8
╵ ^
try {
await esbuild.build({
entryPoints: ["main.coffee"],
plugins: [coffeeScriptPlugin()],
logLevel: "silent", // Suppress esbuild's error output
});
} catch (error) {
console.error("Build failed:", error.message);
// Custom error handling logic
}- Breaking Change: Now requires Node.js 20+
- New: ESM-first approach
- Updated: Latest CoffeeScript compiler (2.7.0)
- Updated: esbuild peer dependency to ^0.25.9
Before (v2.x):
const coffeeScriptPlugin = require("esbuild-coffeescript");After (v3.x):
import coffeeScriptPlugin from "esbuild-coffeescript";✘ [ERROR] Could not resolve "./utils.coffee"
Solution: Ensure the file exists and the path is correct. esbuild resolves paths relative to the importing file.
This usually indicates a CoffeeScript compilation error.
Solution: Check your CoffeeScript syntax. Use bare: true to remove the function wrapper if needed.
Solution: Use inlineMap: true option or configure esbuild's sourcemap option:
esbuild.build({
sourcemap: true, // or 'inline'
plugins: [coffeeScriptPlugin({ inlineMap: true })],
// ...
});- Use
bare: truefor smaller output when the function wrapper isn't needed - Enable minification in production builds
- Use
bundle: trueto reduce the number of HTTP requests - Consider code splitting for large applications
- Check the esbuild documentation
- Review CoffeeScript language guide
- Search existing GitHub issues
- Create a minimal reproduction when reporting bugs
While some consider CoffeeScript a thing of the past, it remains actively maintained and widely used:
- 1.6+ million weekly downloads on npm
- Concise, readable syntax that compiles to clean JavaScript
- Excellent for rapid prototyping and functional programming patterns
- Mature ecosystem with extensive tooling support
This plugin provides an excellent migration path for CoffeeScript projects wanting to leverage esbuild's speed and modern bundling features.
We welcome contributions! Please see our Contributing Guide for details.
# Clone the repository
git clone https://github.com/johnie/esbuild-coffeescript.git
cd esbuild-coffeescript
# Install dependencies
pnpm install
# Run tests
pnpm test
# Build the project
pnpm build# Run all tests
pnpm test
# Watch mode
pnpm test:watch
# Coverage report
pnpm test:coverageMIT © Johnie Hjelm
Tip: ⭐ Star this repo if you find it helpful!