Skip to content

Commit

Permalink
Yeah, everything in order
Browse files Browse the repository at this point in the history
  • Loading branch information
frostney committed Feb 23, 2015
1 parent af76eb8 commit 9fc9ffa
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions tasks/commonjs_tamer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ var beautify = require('js-beautify').js_beautify;
var coffeeFormatter = require('coffee-formatter');
var SourceMapConcat = require('sourcemap-concat').SourceMapConcatenator;

module.exports = function(grunt) {
module.exports = function (grunt) {

// Please see the Grunt documentation for more information regarding task
// creation: http://gruntjs.com/creating-tasks

grunt.registerMultiTask('commonjs_tamer', 'Tame your CommonJS modules', function() {
grunt.registerMultiTask('commonjs_tamer', 'Tame your CommonJS modules', function () {
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
register: 'require.register',
Expand All @@ -26,10 +26,10 @@ module.exports = function(grunt) {
base: null,
namespace: null,
sourcemap: true,
processName: function(name /*, basename */) {
processName: function (name /*, basename */) {
return name;
},
process: function(content /*, moduleName */) {
process: function (content /*, moduleName */) {
return content;
},
blacklist: [],
Expand All @@ -38,12 +38,12 @@ module.exports = function(grunt) {
});

var quotes = (options.doubleQuotes) ? '"' : '\'';

// Iterate over all specified file groups.
this.files.forEach(function(f) {
this.files.forEach(function (f) {

var sourceMapConcat = new SourceMapConcat({file: f.dest});

var src = '';

if (options.banner) {
Expand All @@ -53,60 +53,62 @@ module.exports = function(grunt) {
src += options.banner;
}
}

// Concat specified files.
src = f.src.filter(function(filepath) {
var filtered = f.src.filter(function (filepath) {
// Warn on and remove invalid source files (if nonull was set).
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
}).map(function(filepath) {
});

src += filtered.map(function (filepath, index) {
// Read file source.
var source = grunt.file.read(filepath);
var extension = path.extname(filepath);

var isCoffeeScript = extension === '.coffee';

var moduleName = filepath.split(extension)[0];
if (options.base && moduleName.indexOf(options.base) === 0) {
moduleName = moduleName.split(options.base)[1];
}

if (options.normalizeIndexFile && moduleName.indexOf('/index') > 0) {
moduleName = moduleName.replace('/index', '');
}

if (options.namespace) {
// If index.js is in the root folder and a namespace has been defined
// use the namespace as the module name
if (options.normalizeIndexFile && moduleName === 'index') {
moduleName = options.namespace;
} else {
moduleName = options.namespace + '/' + moduleName;
moduleName = options.namespace + '/' + moduleName;
}
}

moduleName = options.processName(moduleName, path.basename(moduleName));

if (options.blacklist.indexOf(moduleName) >= 0) {
return;
}

if (isCoffeeScript) {
source = options.register + ' ' + quotes + moduleName + quotes + ', (require, module, exports) ->' +
options.separator + (function(s) {
return s.split(options.separator).map(function(l) {
options.separator + (function (s) {
return s.split(options.separator).map(function (l) {
return '\t' + l;
}).join(options.separator);
})(source) + options.separator + options.separator;
} else {
source = options.register + '(' + quotes + moduleName + quotes + ', function(require, module, exports) {' +
options.separator + source + options.separator + '});';
}

if (options.beautify) {
if (!isCoffeeScript) {
var beautifyOptions = {};
Expand All @@ -124,7 +126,7 @@ module.exports = function(grunt) {
source = coffeeFormatter.formatTwoSpaceOperator(source);
}
}

source = options.process(source, moduleName);

if (options.sourceMap) {
Expand Down Expand Up @@ -172,7 +174,7 @@ module.exports = function(grunt) {
src += options.footer;
}
}

// Write the destination file.
grunt.file.write(f.dest, src);

Expand Down

0 comments on commit 9fc9ffa

Please sign in to comment.