Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added wrap option for wrapping js with closures

  • Loading branch information...
commit b0eea6cd7d537a05522d0562b0aa74701b84c789 1 parent 0c7076e
Tim authored
Showing with 35 additions and 12 deletions.
  1. +19 −7 lib/node-asset/index.js
  2. +16 −5 src/index.coffee
26 lib/node-asset/index.js
View
@@ -12,7 +12,7 @@
return sys.puts(("[node-asset][" + (new Date().toLocaleTimeString()) + "] " + message));
};
Package = function(output, input, options) {
- var _a, _b, _c;
+ var _a, _b, _c, _d;
this.mtime = 0;
this.filename = output;
this.contents = input;
@@ -20,6 +20,7 @@
this.compile = options.compile === true || !(typeof (_b = options.compile) !== "undefined" && _b !== null) ? true : false;
this.watch = options.watch === true || !(typeof (_c = options.watch) !== "undefined" && _c !== null) ? true : false;
this.type = options.type ? options.type : 'js';
+ this.wrap = options.wrap === true || !(typeof (_d = options.wrap) !== "undefined" && _d !== null) ? true : false;
return this;
};
Package.TYPES = {
@@ -134,11 +135,10 @@
coffee_buffer = null;
return read_task.run((function(__this) {
var __func = function(filename, err, data) {
- err ? log(error) : null;
if (filename === null) {
string_buffer.length > 0 ? (string_buffer = string_buffer.join('\n') + '\n') : (string_buffer = '');
coffee_buffer ? string_buffer += require('coffee-script').compile(coffee_buffer.join('\n'), {
- no_wrap: true
+ noWrap: true
}) : null;
if (this.compile) {
return compile(string_buffer);
@@ -147,10 +147,20 @@
} else {
return write(string_buffer);
}
+ } else if (err) {
+ return log(err.message);
} else if ('.coffee' === path.extname(filename)) {
- coffee_buffer = coffee_buffer || [];
- coffee_buffer[contents_index[filename]] = data.toString();
- return coffee_buffer[contents_index[filename]];
+ if (this.wrap) {
+ string_buffer[contents_index[filename]] = require('coffee-script').compile(data.toString());
+ return string_buffer[contents_index[filename]];
+ } else {
+ coffee_buffer = coffee_buffer || [];
+ coffee_buffer[contents_index[filename]] = data.toString();
+ return coffee_buffer[contents_index[filename]];
+ }
+ } else if (this.wrap && '.js' === path.extname(filename)) {
+ string_buffer[contents_index[filename]] = '(function () {' + data.toString() + '\n})();';
+ return string_buffer[contents_index[filename]];
} else {
string_buffer[contents_index[filename]] = data.toString();
return string_buffer[contents_index[filename]];
@@ -247,7 +257,9 @@
return package.contents.forEach(function(asset) {
return fs.watchFile(asset.path, function(stat, prev) {
log(("Updating a " + package.type + " package"));
- return package.make();
+ return setTimeout(function() {
+ return package.make();
+ }, 1000);
});
});
};
21 src/index.coffee
View
@@ -27,6 +27,8 @@ class Package
@type: if options.type then options.type else 'js'
+ @wrap: if options.wrap is true or !options.wrap? then true else false
+
@TYPES: {
'coffee': ['js', 'coffee']
'js': ['js']
@@ -86,20 +88,27 @@ class Package
string_buffer: []
coffee_buffer: null
read_task.run (filename, err, data) =>
- if err then log error
if filename is null
if string_buffer.length > 0 then string_buffer: string_buffer.join('\n') + '\n'
else string_buffer: ''
if coffee_buffer
string_buffer: + require('coffee-script').compile coffee_buffer.join('\n'), {
- no_wrap: true
+ noWrap: yes
}
if @compile then compile string_buffer
else if @compress then compress string_buffer
else write string_buffer
+ else if err then log err.message
else if '.coffee' is path.extname filename
- coffee_buffer: || []
- coffee_buffer[contents_index[filename]]: data.toString()
+ if @wrap
+ string_buffer[contents_index[filename]]: require('coffee-script').compile data.toString()
+ else
+ coffee_buffer: || []
+ coffee_buffer[contents_index[filename]]: data.toString()
+ else if @wrap and '.js' is path.extname filename
+ string_buffer[contents_index[filename]]: '(function () {' +
+ data.toString() +
+ '\n})();'
else
string_buffer[contents_index[filename]]: data.toString()
@@ -165,4 +174,6 @@ watch: (package) ->
package.contents.forEach (asset) ->
fs.watchFile asset.path, (stat, prev) ->
log "Updating a $package.type package"
- package.make()
+ setTimeout ->
+ package.make()
+ , 1000
Please sign in to comment.
Something went wrong with that request. Please try again.