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
- Compatibility
- Quick Start
- Usage Examples
- Configuration Options
- Advanced Usage
- Error Handling
- Migration Guide
- Troubleshooting
- Why CoffeeScript?
- 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 .litcoffee files — no extra configuration needed:
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()],
outfile: "calculator.js",
});Note: The
literateoption is only needed if your literate CoffeeScript files use a non-standard extension (e.g.,.md).
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', 'john@example.com')
console.log user.greet()build.js
esbuild.build({
entryPoints: ["main.coffee"],
bundle: true,
plugins: [coffeeScriptPlugin({ bare: true })],
outfile: "app.js",
format: "esm",
});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) |
literate |
boolean |
auto-detected | Parse as Literate CoffeeScript (auto-detected for .litcoffee files) |
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...");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
╵ ^
- 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 })],
// ...
});- Check the esbuild documentation
- Review CoffeeScript language guide
- Search existing GitHub issues
While some consider CoffeeScript a thing of the past, it remains actively maintained and widely used:
- Millions of 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