Skip to content

Commit

Permalink
Add targetPrefix option and update tests/readme
Browse files Browse the repository at this point in the history
  • Loading branch information
pgilad committed Jun 5, 2014
1 parent c020e70 commit e5ed3db
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
5 changes: 3 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
'use strict';

module.exports = function(grunt) {
module.exports = function (grunt) {
// load all npm grunt tasks
require('load-grunt-tasks')(grunt);
// Project configuration.
Expand Down Expand Up @@ -63,7 +63,8 @@ module.exports = function(grunt) {
css: ['concat', 'cssmin']
},
dirTasks: ['filerev'],
prefix: 'test/'
prefix: 'test/',
targetPrefix: 'test/'
},
files: [{
src: 'test/fixtures/layout.jade',
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ as it will output a file with different name as the original target.
This adds some flexibility to where you keep your public folder. It
allows you to add a prefix to the path.

#### targetPrefix
**String** `Default: ''`

Same as the `prefix` but used for target location. If you specify a string here it will be prefixed
to the output of the target file.

#### replacePath
**Object** `Default: {}`

Expand Down Expand Up @@ -293,6 +299,7 @@ grunt.initConfig({
},
dirTasks: 'filerev', //optional
prefix: '', //optional
targetPrefix: '', //optional
replacePath: { //optional
'#{env}': 'dist'
}
Expand Down
11 changes: 11 additions & 0 deletions tasks/jade_usemin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = function (grunt) {
//DEPRECATION NOTICE: 0.6.0
//remove uglify from tasks.js if not specificed
if (!options.uglify) {
grunt.log.subhead('Uglify option was deprecated in version 0.5.0. Please see the tasks option');
options.tasks.js = _.without(options.tasks.js, 'uglify');
}

Expand All @@ -31,6 +32,16 @@ module.exports = function (grunt) {
options.dirTasks = [options.dirTasks];
}

if (options.targetPrefix && !_.isString(options.targetPrefix)) {
grunt.fail.warn('Option targetPrefix must be a string');
options.targetPrefix = null;
}

if (options.prefix && !_.isString(options.prefix)) {
grunt.fail.warn('Option prefix must be a string');
options.prefix = null;
}

var extractedTargets = jadeUsemin.iterateFiles(this.files, options);

//rules:
Expand Down
35 changes: 20 additions & 15 deletions tasks/lib/jade_usemin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ var os = require('os');
var _ = require('lodash');

var getFileSrc = function (str, type) {
var result;
if (type === 'js') {
return str.match(/script.+src\s*=\s*['"]([^"']+)['"]/mi);
result = str.match(/script.+src\s*=\s*['"]([^"']+)['"]/mi);
}
if (type === 'css') {
return str.match(/link.+href\s*=\s*['"]([^"']+)['"]/mi);
result = str.match(/link.+href\s*=\s*['"]([^"']+)['"]/mi);
}
return null;

return (result || [])[1];
};

//set up default tasks options
Expand Down Expand Up @@ -232,6 +234,8 @@ exports.task = function (grunt) {
var replacePath = options.replacePath;
var location = options.location;
var output = options.output;
var targetPrefix = options.targetPrefix;

var buildPattern, target, type, insideBuildFirstItem = {}, optimizedSrc = [];
var insideBuild = false;
var tempExtraction = {};
Expand All @@ -252,10 +256,15 @@ exports.task = function (grunt) {
target = buildPattern.target;
//TODO altPath = buildPattern.altPath;

if (type !== 'css' && type !== 'js') {
if (!_.contains(['css', 'js'], type)) {
grunt.log.warn('Unsupported build type: ' + type);
}

//add prefix to target as well
if (targetPrefix) {
target = path.join(targetPrefix, target);
}

grunt.verbose.writelns('Found build:<type> pattern in line:', lineIndex);
//default empty target
tempExtraction[target] = {
Expand Down Expand Up @@ -288,9 +297,7 @@ exports.task = function (grunt) {
}

var src = getFileSrc(line, type);

if (src && src[1]) {
src = src[1];
if (src) {
//assign first source
insideBuildFirstItem.src = insideBuildFirstItem.src || src;
insideBuildFirstItem.line = insideBuildFirstItem.line || line;
Expand All @@ -300,7 +307,7 @@ exports.task = function (grunt) {
src = src.substr(1);
}

//if prefix option exists than concat it
//add prefix to src
if (prefix) {
src = path.join(prefix, src);
}
Expand All @@ -311,16 +318,14 @@ exports.task = function (grunt) {
} else {
//attempt to resolve path relative to location (where jade file is)
//TODO: use altPath
var locationPath = path.dirname(location);
var newSrcPath = path.resolve(locationPath, src);
grunt.verbose.writelns('Src file ' + src + " wasn't found. Looking for it relative to jade file");

//let's see if we found it
var newSrcPath = path.resolve(path.dirname(location), src);
if (grunt.file.exists(newSrcPath)) {
//new src path exists
addSrcToTarget(tempExtraction, target, newSrcPath);
}
//src file wasn't found
else {
} else {
grunt.verbose.writelns('Src file ' + newSrcPath + " wasn't found relative to jade file as well.");
//src file wasn't found
grunt.log.warn("Found script src that doesn't exist: " + src);
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/layout.jade
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//-<!-- build:js test/compiled/jquery.min.js -->
//-<!-- build:js compiled/jquery.min.js -->
script(src='/src/script1.js')
//-<!-- endbuild -->

0 comments on commit e5ed3db

Please sign in to comment.