Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

refactoring - all replaces can be done by one function #9

Merged
merged 1 commit into from

2 participants

@krzychukula

For me there is no regression and this is much cleaner.

For now I will work on regexp solutions for usemin.js.

@mklabs mklabs merged commit b510662 into mklabs:dev
@mklabs
Owner

Looks good. Thanks again! Merged. I like this grunt helper addition.

That being said, we really need to setup a bunch of assertions related to these usemin replacements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 17 additions and 44 deletions.
  1. +17 −44 tasks/usemin.js
View
61 tasks/usemin.js
@@ -19,10 +19,7 @@ var fs = require('fs'),
task.registerBasicTask('usemin', 'Replaces references to non-minified scripts / stylesheets', function(data, name) {
var files = file.expand(data),
- output = path.resolve(config('output')),
- cssdir = path.join(output, 'css'),
- imgdir = path.join(output, 'img'),
- jsdir = path.join(output, 'js');
+ output = path.resolve(config('output'));
files.map(file.read).forEach(function(content, i) {
var p = files[i];
@@ -30,7 +27,22 @@ task.registerBasicTask('usemin', 'Replaces references to non-minified scripts /
log.writeln('switch from a regular jquery to minified');
log.writeln('Update the HTML to reference our concat/min/revved script files');
- content = content.replace(/<script.+src=['"](.+)["'][\/>]?><[\\]?\/script>/gm, function(match, src) {
+ content = task.helper('replace', content, /<script.+src=['"](.+)["'][\/>]?><[\\]?\/script>/gm);
+
+ log.writeln('Update the HTML with the new css filename');
+ content = task.helper('replace', content, /<link rel=["']?stylesheet["']?\shref=['"](.+)["']\s*>/gm);
+
+ log.writeln('Update the HTML with the new img filename');
+ content = task.helper('replace', content, /<img.+src=['"](.+)["'][\/>]?>/);
+
+ // write the new content to disk
+ file.write(p, content);
+ });
+
+});
+
+task.registerHelper('replace', function(content, regexp) {
+ return content.replace(regexp, function(match, src) {
//do not touch external files
if(src.match(/\/\//)) return match;
var basename = path.basename(src);
@@ -47,43 +59,4 @@ task.registerBasicTask('usemin', 'Replaces references to non-minified scripts /
// if file not exists probaly was concatenated into another file so skip it
return filename ? match.replace(src, filename) : '';
});
-
- log.writeln('Update the HTML with the new css filename');
- content = content.replace(/<link rel=["']?stylesheet["']?\shref=['"](.+)\/([^\/"']+)["']\s*>/gm, function(match, prefix, src) {
- //do not touch external files
- if(prefix.match(/\/\//)) return match;
-
- var filename = path.basename((file.expand(path.join(config('output'), '**/*') + src)[0] || ''));
-
- // replace the output dir prefix
- filename = filename.replace(config('output'), '');
-
- // handle the relative prefix
- filename = [prefix, filename].join('/');
-
- // if file not exists probaly was concatenated into another file so skip it
- return filename ? '<link rel="stylesheet" href=":file">'.replace(':file', filename) : '';
- });
-
- log.writeln('Update the HTML with the new img filename');
- content = content.replace(/<img.+src=['"](.+)\/([^\/"']+)["'][\/>]?>/, function(match, prefix, src) {
- //do not touch external files
- if(prefix.match(/\/\//)) return match;
-
- var filename = path.basename((file.expand(path.join(config('output'), '**/*') + src)[0] || ''));
-
- // replace the output dir prefix
- filename = filename.replace(config('output'), '');
-
- // handle the relative prefix
- filename = [prefix, filename].join('/');
-
- // if file not exists probaly, just leave off the actual content
- return filename ? '<img src=":src">'.replace(':src', filename) : match;
- });
-
- // write the new content to disk
- file.write(p, content);
- });
-
});
Something went wrong with that request. Please try again.