Primary compilation functions for transforming CoffeeScript source code into JavaScript with full control over compilation options and output formats.
Main compilation function that converts CoffeeScript source code to JavaScript.
/**
* Compiles CoffeeScript source code to JavaScript
* @param code - CoffeeScript source code as string
* @param options - Compilation options object
* @returns Compiled JavaScript string, or object with js/sourceMap if sourceMap option enabled
*/
function compile(code, options);Parameters:
code (string): CoffeeScript source code to compileoptions (object, optional): Compilation configuration optionsReturns:
{js, sourceMap, v3SourceMap} if options.sourceMap is trueUsage Examples:
const CoffeeScript = require('coffee-script');
// Basic compilation
const js = CoffeeScript.compile('square = (x) -> x * x');
console.log(js);
// Compilation with options
const result = CoffeeScript.compile(`
class Animal
constructor: (@name) ->
speak: -> "#{@name} makes a sound"
`, {
bare: true, // Don't wrap in function
header: true, // Add "Generated by CoffeeScript" header
sourceMap: true // Generate source map
});
console.log(result.js);
console.log(result.v3SourceMap);Lexical tokenization function that converts CoffeeScript source into an array of tokens.
/**
* Tokenizes CoffeeScript source code into lexical tokens
* @param code - CoffeeScript source code as string
* @param options - Tokenization options object
* @returns Array of token objects
*/
function tokens(code, options);Parameters:
code (string): CoffeeScript source code to tokenizeoptions (object, optional): Tokenization optionsReturns:
[type, value, locationData]Usage Examples:
const CoffeeScript = require('coffee-script');
// Tokenize simple expression
const tokens = CoffeeScript.tokens('x = 42');
console.log(tokens);
// Output: [['IDENTIFIER', 'x', {...}], ['=', '=', {...}], ['NUMBER', '42', {...}], ...]
// Tokenize with options
const tokens2 = CoffeeScript.tokens('console.log "hello"', {
rewrite: false, // Skip token rewriting phase
literate: false // Not literate CoffeeScript
});Parser function that converts CoffeeScript source or tokens into an Abstract Syntax Tree.
/**
* Parses CoffeeScript source or tokens into AST nodes
* @param source - CoffeeScript source string or token array
* @param options - Parsing options object
* @returns Root AST node (typically Block)
*/
function nodes(source, options);Parameters:
source (string|array): CoffeeScript source code or pre-tokenized arrayoptions (object, optional): Parsing optionsReturns:
Usage Examples:
const CoffeeScript = require('coffee-script');
// Parse from source string
const ast = CoffeeScript.nodes('x = 42\nconsole.log x');
console.log(ast.constructor.name); // 'Block'
console.log(ast.expressions.length); // 2
// Parse from tokens
const tokens = CoffeeScript.tokens('square = (x) -> x * x');
const ast2 = CoffeeScript.nodes(tokens);
console.log(ast2.expressions[0].constructor.name); // 'Assign'
// Access AST structure
ast.expressions.forEach((expr, i) => {
console.log(`Expression ${i}: ${expr.constructor.name}`);
});Internal function for compiling CoffeeScript files from disk with source map support.
/**
* Compiles a CoffeeScript file from disk
* @param filename - Path to .coffee file
* @param sourceMap - Whether to generate source map
* @param inlineMap - Whether to inline source map in output
* @returns Compiled JavaScript or object with source map
*/
function _compileFile(filename, sourceMap, inlineMap);Parameters:
filename (string): File path to compilesourceMap (boolean): Generate separate source mapinlineMap (boolean): Include inline source mapReturns:
{js, sourceMap, v3SourceMap} (if source maps enabled)interface CompileOptions {
/** Compile without top-level function wrapper */
bare?: boolean;
/** Add "Generated by CoffeeScript" header comment */
header?: boolean;
/** Generate source map for debugging */
sourceMap?: boolean;
/** Include source map inline in JavaScript output */
inlineMap?: boolean;
/** Filename for source map and error reporting */
filename?: string;
/** Treat input as literate CoffeeScript */
literate?: boolean;
/** Array of referenced variable names */
referencedVars?: string[];
/** Source file paths for source map */
sourceFiles?: string[];
/** Line number offset for source map */
shiftLine?: number;
}
interface TokenizeOptions {
/** Skip token rewriting phase */
rewrite?: boolean;
/** Treat input as literate CoffeeScript */
literate?: boolean;
}/** CoffeeScript version string */
const VERSION: string;
/** Supported file extensions for CoffeeScript files */
const FILE_EXTENSIONS: string[]; // ['.coffee', '.litcoffee', '.coffee.md']
/** Reference to helpers utility module */
const helpers: object;