gulp-usemin
Advanced tools
Comparing version
56
index.js
@@ -7,2 +7,3 @@ var path = require('path'); | ||
var gutil = require('gulp-util'); | ||
var rev = require('gulp-rev'); | ||
@@ -12,12 +13,19 @@ module.exports = function (options) { | ||
var startReg = /<!--\s*build:(css|js)(?:\(([^\)]+)\))?\s+(\/?([^\s]+))\s*-->/gim; | ||
var startReg = /<!--\s*build:(css|js)(?:\(([^\)]+?)\))?\s+(\/?([^\s]+?))\s*-->/gim; | ||
var endReg = /<!--\s*endbuild\s*-->/gim; | ||
var jsReg = /<\s*script\s+.*src\s*=\s*"([^"]+)".*><\s*\/\s*script\s*>/gi; | ||
var cssReg = /<\s*link\s+.*href\s*=\s*"([^"]+)".*>/gi; | ||
var jsReg = /<\s*script\s+.*?src\s*=\s*"([^"]+?)".*?><\s*\/\s*script\s*>/gi; | ||
var cssReg = /<\s*link\s+.*?href\s*=\s*"([^"]+)".*?>/gi; | ||
var basePath, mainPath, mainName, alternatePath; | ||
var filesCount = 0; | ||
function createFile(name, content) { | ||
function createFile(name, content, asset) { | ||
var filepath = path.join(path.relative(basePath, mainPath), name) | ||
if (asset === true && options.assetsDir) | ||
{ | ||
filepath = path.relative(basePath,path.join(options.assetsDir,filepath)); | ||
} | ||
return new gutil.File({ | ||
path: path.join(path.relative(basePath, mainPath), name), | ||
path: filepath, | ||
contents: new Buffer(content) | ||
@@ -34,3 +42,3 @@ }); | ||
.replace(reg, function (a, b) { | ||
paths.push(path.join(alternatePath || mainPath, b)); | ||
paths.push(path.resolve(path.join(alternatePath || mainPath, b))); | ||
}); | ||
@@ -72,9 +80,35 @@ | ||
if (section[1] == 'js') { | ||
html.push('<script src="' + section[3] + '"></script>'); | ||
jsFiles.push(createFile(section[4], concat(section[5], jsReg, ';' + EOL + EOL))); | ||
var newFile = createFile(section[4], concat(section[5], jsReg, ';' + EOL + EOL), true); | ||
if (options.rev === true) | ||
{ | ||
var stream = rev(); | ||
stream.write(newFile); | ||
stream.end(); | ||
html.push('<script src="' + section[3].replace(path.basename(section[3]), path.basename(newFile.path)) + '"></script>'); | ||
} | ||
else | ||
{ | ||
html.push('<script src="' + section[3] + '"></script>'); | ||
} | ||
jsFiles.push(newFile); | ||
filesCount++; | ||
} | ||
else { | ||
html.push('<link rel="stylesheet" href="' + section[3] + '"/>'); | ||
cssFiles.push(createFile(section[4], concat(section[5], cssReg, EOL + EOL))); | ||
else | ||
{ | ||
var newFile = createFile(section[4], concat(section[5], cssReg, EOL + EOL), true); | ||
if (options.rev === true) | ||
{ | ||
var stream = rev(); | ||
stream.write(newFile); | ||
stream.end(); | ||
html.push('<link rel="stylesheet" href="' + section[3].replace(path.basename(section[3]), path.basename(newFile.path)) + '"/>'); | ||
} | ||
else | ||
{ | ||
html.push('<link rel="stylesheet" href="' + section[3] + '"/>'); | ||
} | ||
cssFiles.push(newFile); | ||
filesCount++; | ||
@@ -81,0 +115,0 @@ } |
{ | ||
"name": "gulp-usemin", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Replaces references to non-optimized scripts or stylesheets into a set of HTML files (or any templates/views).", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
"gulp-util": "latest", | ||
"through2": "latest" | ||
"through2": "latest", | ||
"gulp-rev": "latest" | ||
}, | ||
@@ -11,0 +12,0 @@ "devDependencies": { |
@@ -5,3 +5,3 @@ # gulp-usemin | ||
This task is designed for gulp 3. | ||
> Attention: v0.2.0 dont minify files by default. | ||
> Attention: v0.2.0 does not minify the files by default. | ||
@@ -147,2 +147,34 @@ ## Usage | ||
<script src="js/optimized.js"></script> | ||
``` | ||
``` | ||
## Changelog | ||
#####0.2.2 | ||
- allow gulp-usemin to work with minified source HTML (by CWSpear) | ||
- fixed alternate path bug (by CWSpear) | ||
- add assetDir option (by pursual) | ||
- add rev option (by pursual) | ||
#####0.2.1 | ||
- fixed subfolders bug | ||
#####0.2.0 | ||
- no minification by default. New options API | ||
#####0.1.4 | ||
- add alternate search path support | ||
#####0.1.3 | ||
- add support for absolute URLs (by vasa-chi) | ||
#####0.1.1 | ||
- fixed aggressive replace comments | ||
#####0.1.0 | ||
- fixed some bugs. Add tests. | ||
#####0.0.2 | ||
- add minification by default | ||
#####0.0.1 | ||
- initial release |
@@ -141,2 +141,6 @@ /* jshint node: true */ | ||
it('simple (js block) (html minified)', function(done) { | ||
compare('min-html-simple-js.html', 'min-html-simple-js.html', done); | ||
}); | ||
it('simple with path (js block)', function(done) { | ||
@@ -150,2 +154,6 @@ compare('simple-js-path.html', 'simple-js-path.html', done); | ||
it('simple (css block) (html minified)', function(done) { | ||
compare('min-html-simple-css.html', 'min-html-simple-css.html', done); | ||
}); | ||
it('simple with path (css block)', function(done) { | ||
@@ -266,2 +274,21 @@ compare('simple-css-path.html', 'simple-css-path.html', done); | ||
it('simple (css block) (minified html)', function(done) { | ||
var expectedName = 'style.css'; | ||
var exist = false; | ||
compare( | ||
'min-html-simple-css.html', | ||
function(newFile) { | ||
if (newFile.path === expectedName) { | ||
exist = true; | ||
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents)); | ||
} | ||
}, | ||
function() { | ||
assert.ok(exist); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('simple with path (css block)', function(done) { | ||
@@ -408,2 +435,21 @@ var expectedName = path.join('data', 'css', 'style.css'); | ||
it('simple (js block) (minified html)', function(done) { | ||
var expectedName = 'app.js'; | ||
var exist = false; | ||
compare( | ||
'min-html-simple-js.html', | ||
function(newFile) { | ||
if (newFile.path === expectedName) { | ||
exist = true; | ||
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents)); | ||
} | ||
}, | ||
function() { | ||
assert.ok(exist); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('simple with path (js block)', function(done) { | ||
@@ -410,0 +456,0 @@ var expectedName = path.join('data', 'js', 'app.js'); |
25116
13.69%43
10.26%567
13.86%179
22.6%3
50%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added