Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update replace.js #95

Merged
merged 3 commits into from Apr 17, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 34 additions & 22 deletions lib/replace.js
Expand Up @@ -12,6 +12,7 @@ exports.init = function(grunt) {
// External libs.
var Q = require('q');
var cheerio = require('cheerio');
var util = grunt.util;

return function(config) {
var deferred = Q.defer();
Expand All @@ -22,9 +23,11 @@ exports.init = function(grunt) {
}

// iterate over all modules that are configured for replacement
config.replaceRequireScript.forEach(function (entry, idx) {
var files = grunt.file.expand(entry.files);
config.replaceRequireScript.forEach(function (file, idx) {
var files = grunt.file.expand(file.files);
var filesEvaluated = 0;
var filesToEvaluate = files.length;
var modulePath = file.modulePath;

// iterate over found html files
files.forEach(function (file) {
Expand All @@ -34,27 +37,36 @@ exports.init = function(grunt) {
var script_re = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
matches = contents.match(script_re);

[].slice.call(matches).forEach(function(match, i){
var $ = cheerio.load(match),
elm = $('script');
if (elm.attr('data-main')){
var insertScript = (entry.modulePath || elm.attr('data-main'));
elm.attr('src', insertScript + '.js');
elm.removeAttr('data-main');
// replace i'th occurrence in content
var j = 0;
contents = contents.replace(script_re, function(match){
j++;
return (j-1 === i) ? $.html() : match;
});
}
});
if(util._.isNull(matches)){
// decrement the amount of files we needed to evaluated
// since one has nothing for us.
filesToEvaluate--;
} else {

grunt.log.writeln('Updating requirejs script tag for file', file);
grunt.file.write(file, contents);
// only resolve after all files have been evaluated
filesEvaluated++;
if (filesEvaluated >= files.length) {
[].slice.call(matches).forEach(function(match, i){
var $ = cheerio.load(match),
elm = $('script');
if (elm.attr('data-main')){
var insertScript = (modulePath || elm.attr('data-main'));
elm.attr('src', insertScript + '.js');
elm.removeAttr('data-main');
// replace i'th occurrence in content
var j = 0;
contents = contents.replace(script_re, function(match){
j++;
return (j-1 === i) ? $.html() : match;
});
}
});

grunt.log.writeln('Updating requirejs script tag for file', file);
grunt.file.write(file, contents);
// increment the amount of files successfully evaluated.
filesEvaluated++;
}

if (filesEvaluated >= filesToEvaluate) {
// only resolve after all files have been evaluated
deferred.resolve(config);
}

Expand Down