node-compiler
Version:
Single page application compiler that supports a number of popular integrations.
64 lines (55 loc) • 2.99 kB
JavaScript
var Helpers = require("../helpers");
var Plugin = require("../lib/Plugin");
try { var CoffeeScript = require("coffee-script"); } catch(e) {}
var FS = require("fs");
/* ------------------------------------------------------------------------------------------------------------------ *\
* @description: The Coffee plugin supports 3 options. Files placed in the watch directory with a .coffee extension
are processed and compiled with the coffee-script module before output.
* @options:
* header - Prints a 'Generated by CoffeeScript VERSION' header at the top of each compiled file.
* bare - Compile the JavaScript without the top-level function safety wrapper.
* paths - Include absolute paths of the compiled files within the output as comments.
* @requires:
* helpers - Used for reading from and writing to cache.
* plugin - Used to inherit common methods among the plugins.
* coffee-script - Used to compile CoffeeScript into JavaScript.
* fs - Used to read the contents of the files that aren't within cache and don't require minifying.
\* ------------------------------------------------------------------------------------------------------------------ */
function Coffee() {
this.filePattern = /\.coffee$/i;
this.outputExtension = ".js";
Plugin.apply(this, arguments);
}
Coffee.prototype = Object.create(Plugin.prototype);
Coffee.prototype.constructor = Coffee;
Coffee.title = "CoffeeScript";
Coffee.dependency = "coffee-script@1.8.0";
/* ------------------------------------------------------------------------------------------------------------------ *\
* @description: This method first checks the cache for the particular file. If it's not fresh or found, the file
will be compiled before adding to cache.
* @parameters:
* path: The absolute path of the file.
* stat: The stat object of the file.
* startup: This parameter is only true when the compiler is first launched otherwise it's falsy.
* @returns: The contents of the compiled file will be returned unless an error occurs. If this is the case null will
instead be returned.
\* ------------------------------------------------------------------------------------------------------------------ */
Coffee.prototype.compile = function _compile(path, stat, startup) {
var cache = Helpers.getCache(this.target, path);
var contents = (cache || "");
var options = this.options;
if (!cache) {
try {
contents = CoffeeScript.compile(FS.readFileSync(path, "utf-8"), options);
if (options.paths) { contents = (("// " + path + "\n") + contents); }
Helpers.cache(this.target, path, contents);
this.logger.debug("[Cached] " + path);
this.log(path);
} catch (e) {
this.error(path, e);
return null;
}
}
return contents;
};
module.exports = Coffee;