Webpack loader that compiles CoffeeScript files to JavaScript during the build process
npx @tessl/cli install tessl/npm-coffee-loader@5.0.0Coffee-loader is a webpack loader that compiles CoffeeScript files to JavaScript during the webpack build process. It integrates seamlessly with webpack's module system, enabling developers to use CoffeeScript in their web applications while maintaining full compatibility with modern webpack features.
npm install --save-dev coffeescript coffee-loaderThe coffee-loader is imported and configured through webpack configuration:
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.coffee$/,
loader: "coffee-loader",
},
],
},
};Alternative inline usage:
import coffee from "coffee-loader!./file.coffee";CoffeeScript file (file.coffee):
# Assignment:
number = 42
opposite = true
# Conditions:
number = -42 if opposite
# Functions:
square = (x) -> x * x
# Arrays:
list = [1, 2, 3, 4, 5]
# Objects:
math =
root: Math.sqrt
square: square
cube: (x) -> x * square xWebpack configuration:
module.exports = {
module: {
rules: [
{
test: /\.coffee$/,
loader: "coffee-loader",
options: {
bare: false,
transpile: {
presets: ["@babel/env"],
},
},
},
],
},
};The main loader function that compiles CoffeeScript source code to JavaScript within the webpack build process.
/**
* Main webpack loader function for CoffeeScript compilation
* @param source - CoffeeScript source code string
* @returns Compiled JavaScript via webpack async callback
*/
function loader(source: string): void;This function:
getOptions() to retrieve loader configurationCustom error class for CoffeeScript compilation errors.
/**
* Custom error class for CoffeeScript compilation errors
* Wraps original CoffeeScript errors for webpack error reporting
*/
class CoffeeScriptError extends Error {
constructor(error: Error);
name: "CoffeeScriptError";
}All CoffeeScript compiler options are supported. Key options include:
interface CoffeeLoaderOptions {
/** Compile without function wrapper (default: true) */
bare?: boolean;
/** Generate source maps (inherits from webpack devtool by default) */
sourceMap?: boolean;
/** Enable literate CoffeeScript mode */
literate?: boolean;
/** Babel transpilation options for modern JS support */
transpile?: {
presets?: string[];
plugins?: string[];
};
/** Source filename (automatically set by webpack, user value ignored) */
filename?: string;
/** All other CoffeeScript compiler options are supported */
[key: string]: any;
}module.exports = {
module: {
rules: [
{
test: /\.coffee$/,
loader: "coffee-loader",
},
],
},
};For modern JavaScript features and browser compatibility:
module.exports = {
module: {
rules: [
{
test: /\.coffee$/,
loader: "coffee-loader",
options: {
bare: false,
transpile: {
presets: ["@babel/env"],
},
},
},
],
},
};For .litcoffee files:
module.exports = {
module: {
rules: [
{
test: /\.litcoffee$/,
loader: "coffee-loader",
options: {
literate: true,
},
},
],
},
};transpile optionCompilation errors are wrapped in the CoffeeScriptError class and passed to webpack's error handling system. Common errors include:
Errors maintain original CoffeeScript error information while providing webpack-compatible error reporting.
/**
* Webpack loader context interface (subset relevant to coffee-loader)
*/
interface WebpackLoaderContext {
/** Get validated loader options */
getOptions(schema?: object): any;
/** Async callback for loader results */
async(): (error: Error | null, content?: string, sourceMap?: object) => void;
/** Source map setting from webpack devtool */
sourceMap: boolean;
/** Current resource file path */
resourcePath: string;
}
/**
* CoffeeScript compilation result structure
*/
interface CoffeeScriptResult {
/** Compiled JavaScript code */
js: string;
/** Source map in v3 format (when sourceMap enabled) */
v3SourceMap?: string;
}