Files
meteor/packages/coffeescript/plugin/compile-coffeescript.js
David Glasser b68483e31b Implement package-level shared variables for CoffeeScript.
CoffeeScript doesn't allow you to assign to global variables using variable
assignment syntax (though you can assign properties to the global object), which
is the way to create package-level shared variables in Meteor. This commit
provides a CoffeeScript-specific to share variables between files in a package
without adding a redundant alternative for JavaScript files: simply assign
properties on the symbol `shared`.
2013-06-18 09:59:42 -07:00

44 lines
1.4 KiB
JavaScript

var fs = Npm.require('fs');
var path = Npm.require('path');
var coffee = Npm.require('coffee-script');
var handler = function (compileStep) {
var source = compileStep.read().toString('utf8');
var options = {
bare: true,
filename: compileStep.inputPath,
literate: path.extname(compileStep.inputPath) === '.litcoffee'
};
try {
var output = coffee.compile(source, options);
} catch (e) {
// XXX better error handling, once the Plugin interface support it
throw new Error(
compileStep.inputPath + ':' +
(e.location ? (e.location.first_line + ': ') : ' ') +
e.message
);
}
// We want the symbol "share" to be visible to all CoffeeScript files in the
// package (and shared between them), but not visible to JavaScript
// files. (That's because we don't want to introduce two competing ways to
// make package-local variables into JS ("share" vs assigning to non-var
// variables).) The following hack accomplishes that: "__coffeescriptShare"
// will be visible at the package level and "share" at the file level.
output = ("__coffeescriptShare = __coffeescriptShare || {}; " +
"var share = __coffeescriptShare;" + output);
compileStep.addJavaScript({
path: compileStep.inputPath + ".js",
sourcePath: compileStep.inputPath,
data: output,
lineForLine: false
});
};
Plugin.registerSourceHandler("coffee", handler);
Plugin.registerSourceHandler("litcoffee", handler);