grunt-usemin
Advanced tools
Comparing version 0.1.7 to 0.1.8
'use strict'; | ||
var path = require('path'); | ||
@@ -38,3 +37,3 @@ // | ||
// Consider reference from site root | ||
var file = self.revvedfinder.find(src, path.dirname(self.filepath)); | ||
var file = self.revvedfinder.find(src, self.filepath); | ||
var res = match.replace(src, file); | ||
@@ -41,0 +40,0 @@ |
@@ -37,4 +37,12 @@ 'use strict'; | ||
var getBlocks = function (dest, dir, content) { | ||
// start build pattern --> <!-- build:[target] output --> | ||
var regbuild = /<!--\s*build:(\w+)\s*([^\s]+)\s*-->/; | ||
// start build pattern: will match | ||
// * <!-- build:[target] output --> | ||
// * <!-- build:[target](alternate search path) output --> | ||
// The following matching param are set when there's match | ||
// * 0 : the whole matched expression | ||
// * 1 : the target (ie. type) | ||
// * 2 : the alternate search path | ||
// * 3 : the output | ||
// | ||
var regbuild = /<!--\s*build:(\w+)(?:\(([^\)]+)\))?\s*([^\s]+)\s*-->/; | ||
// end build pattern -- <!-- endbuild --> | ||
@@ -48,2 +56,4 @@ var regend = /<!--\s*endbuild\s*-->/; | ||
var originDir = dir; | ||
lines.forEach(function (l) { | ||
@@ -59,9 +69,13 @@ var indent = (l.match(/^\s*/) || [])[0]; | ||
// Handle absolute path (i.e. with respect to the server root) | ||
if (build[2][0] === '/') { | ||
if (build[3][0] === '/') { | ||
startFromRoot = true; | ||
build[2] = build[2].substr(1); | ||
build[3] = build[3].substr(1); | ||
} | ||
if (build[2]) { | ||
// Alternate search path | ||
originDir = build[2]; | ||
} | ||
last = { | ||
type: build[1], | ||
dest: path.join(dest, build[2]), | ||
dest: path.join(dest, build[3]), | ||
startFromRoot: startFromRoot, | ||
@@ -79,2 +93,3 @@ indent: indent, | ||
block = false; | ||
originDir = dir; | ||
} | ||
@@ -85,3 +100,3 @@ | ||
if (asset && asset[2]) { | ||
last.src.push(path.join(dir, asset[2])); | ||
last.src.push(path.join(originDir, asset[2])); | ||
// RequireJS uses a data-main attribute on the script tag to tell it | ||
@@ -97,3 +112,3 @@ // to load up the main entry point of the amp app | ||
last.requirejs.dest = last.dest; | ||
last.requirejs.baseUrl = path.join(dir, path.dirname(main[1])); | ||
last.requirejs.baseUrl = path.join(originDir, path.dirname(main[1])); | ||
last.requirejs.name = path.basename(main[1]); | ||
@@ -213,3 +228,4 @@ last.requirejs.src = last.src.pop(); | ||
'Update the HTML with data-main tags', | ||
function (m) { return m.match(/\.js$/) ? m : m + '.js'; } | ||
function (m) { return m.match(/\.js$/) ? m : m + '.js'; }, | ||
function (m) { return m.replace('.js', ''); } | ||
], | ||
@@ -233,3 +249,4 @@ [/data-[A-Za-z0-9]*=['"]([^"']+)["']/gm, | ||
regexps.forEach(function (rxl) { | ||
var filter = rxl[2] || identity; | ||
var filterIn = rxl[2] || identity; | ||
var filterOut = rxl[3] || identity; | ||
@@ -239,5 +256,5 @@ self.log(rxl[1]); | ||
// Consider reference from site root | ||
var srcfile = filter(src); | ||
var srcfile = filterIn(src); | ||
var file = self.revvedfinder.find(srcfile, self.src); | ||
var res = match.replace(src, file); | ||
var res = match.replace(src, filterOut(file)); | ||
@@ -244,0 +261,0 @@ if (srcfile !== file) { |
@@ -73,3 +73,7 @@ 'use strict'; | ||
var filepath = filepaths.filter(function (f) { | ||
return f.match(re) && (path.dirname(f).match(normalizedDirname)); | ||
var candidateDirname = path.normalize(path.dirname(f)), | ||
endsWith = function (suffix) { | ||
return this.indexOf(suffix, this.length - suffix.length) !== -1; | ||
}; | ||
return f.match(re) && endsWith.call(candidateDirname, normalizedDirname); | ||
})[0]; | ||
@@ -76,0 +80,0 @@ |
{ | ||
"name": "grunt-usemin", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "Grunt task replaces references to non-optimized scripts or stylesheets into a set of HTML files (or any templates/views).", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -5,2 +5,3 @@ # grunt-usemin [![Build Status](https://secure.travis-ci.org/yeoman/grunt-usemin.png?branch=master)](http://travis-ci.org/yeoman/grunt-usemin) | ||
Watch out, this task is designed for Grunt 0.4 and upwards. | ||
@@ -33,3 +34,3 @@ ## Getting Started | ||
```html | ||
<!-- build:<type> <path> --> | ||
<!-- build:<type>(alternate search path) <path> --> | ||
... HTML Markup, list of script / link tags. | ||
@@ -40,2 +41,3 @@ <!-- endbuild --> | ||
- **type**: either `js` or `css` | ||
- ** alternate search path **: (optional) By default the input files are relative to the treated file. Alternate search path allow to change that | ||
- **path**: the file path of the optimized file, the target output | ||
@@ -42,0 +44,0 @@ |
@@ -75,2 +75,6 @@ 'use strict'; | ||
grunt.registerMultiTask('usemin', 'Replaces references to non-minified scripts / stylesheets', function () { | ||
var options = this.options({ | ||
type: this.target | ||
}); | ||
var processors = { | ||
@@ -80,29 +84,29 @@ css: CSSProcessor, | ||
}; | ||
var name = this.target; | ||
var data = this.data; | ||
var options = this.options(); | ||
var files = grunt.file.expand({filter: 'isFile'}, data); | ||
files.map(grunt.file.read).forEach(function (content, i) { | ||
var filepath = files[i]; | ||
var filedir = options.basedir || path.dirname(filepath); | ||
this.files.forEach(function (fileObj) { | ||
var files = grunt.file.expand({nonull: true}, fileObj.src); | ||
grunt.log.subhead('usemin:' + name + ' - ' + filepath); | ||
files.map(grunt.file.read).forEach(function (content, i) { | ||
var filepath = files[i]; | ||
var filedir = options.basedir || path.dirname(filepath); | ||
// make sure to convert back into utf8, `file.read` when used as a | ||
// forEach handler will take additional arguments, and thus trigger the | ||
// raw buffer read | ||
content = content.toString(); | ||
grunt.log.subhead('Processing as ' + options.type.toUpperCase() + ' - ' + filepath); | ||
// Our revved version locator | ||
var revvedfinder = new RevvedFinder(function (p) { return grunt.file.expand({filter: 'isFile'}, p); }, options.dirs); | ||
// make sure to convert back into utf8, `file.read` when used as a | ||
// forEach handler will take additional arguments, and thus trigger the | ||
// raw buffer read | ||
content = content.toString(); | ||
// ext-specific directives handling and replacement of blocks | ||
var proc = new processors[name](filedir, '', content, revvedfinder, function (msg) { | ||
grunt.log.writeln(msg); | ||
// Our revved version locator | ||
var revvedfinder = new RevvedFinder(function (p) { return grunt.file.expand({filter: 'isFile'}, p); }, options.dirs); | ||
// ext-specific directives handling and replacement of blocks | ||
var proc = new processors[options.type](filedir, '', content, revvedfinder, function (msg) { | ||
grunt.log.writeln(msg); | ||
}); | ||
content = proc.process(); | ||
// write the new content to disk | ||
grunt.file.write(filepath, content); | ||
}); | ||
content = proc.process(); | ||
// write the new content to disk | ||
grunt.file.write(filepath, content); | ||
}); | ||
@@ -167,2 +171,3 @@ }); | ||
options.baseUrl = options.baseUrl || block.requirejs.baseUrl; | ||
options.mainConfigFile = path.join(options.baseUrl, options.name) + '.js'; | ||
} else { | ||
@@ -172,3 +177,4 @@ task.options = { | ||
out: block.requirejs.dest, | ||
baseUrl: block.requirejs.baseUrl | ||
baseUrl: block.requirejs.baseUrl, | ||
mainConfigFile: path.join(block.requirejs.baseUrl, block.requirejs.name) + '.js' | ||
}; | ||
@@ -175,0 +181,0 @@ } |
28378
587
131