Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gulp-usemin

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gulp-usemin - npm Package Compare versions

Comparing version 0.3.7 to 0.3.8

test/expected/many-blocks-removal.html

269

index.js

@@ -10,163 +10,166 @@ var path = require('path');

module.exports = function(options) {
options = options || {};
options = options || {};
var startReg = /<!--\s*build:(\w+)(?:\(([^\)]+?)\))?\s+(\/?([^\s]+?))?\s*-->/gim;
var endReg = /<!--\s*endbuild\s*-->/gim;
var jsReg = /<\s*script\s+.*?src\s*=\s*("|')([^"']+?)\1.*?><\s*\/\s*script\s*>/gi;
var cssReg = /<\s*link\s+.*?href\s*=\s*("|')([^"']+)\1.*?>/gi;
var startCondReg = /<!--\[[^\]]+\]>/gim;
var endCondReg = /<!\[endif\]-->/gim;
var basePath, mainPath, mainName, alternatePath;
var startReg = /<!--\s*build:(\w+)(?:\(([^\)]+?)\))?\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 startCondReg = /<!--\[[^\]]+\]>/gim;
var endCondReg = /<!\[endif\]-->/gim;
var basePath, mainPath, mainName, alternatePath;
function createFile(name, content) {
var filePath = path.join(path.relative(basePath, mainPath), name)
var isStatic = name.split('.').pop() === 'js' || name.split('.').pop() === 'css'
function createFile(name, content) {
var filePath = path.join(path.relative(basePath, mainPath), name)
var isStatic = name.split('.').pop() === 'js' || name.split('.').pop() === 'css'
if (options.outputRelativePath && isStatic)
filePath = options.outputRelativePath + name;
if (options.outputRelativePath && isStatic)
filePath = options.outputRelativePath + name;
return new gutil.File({
path: filePath,
contents: new Buffer(content)
})
}
return new gutil.File({
path: filePath,
contents: new Buffer(content)
})
}
function getBlockType(content) {
return jsReg.test(content) ? 'js' : 'css';
}
function getBlockType(content) {
return jsReg.test(content) ? 'js' : 'css';
}
function getFiles(content, reg) {
var paths = [];
var files = [];
function getFiles(content, reg) {
var paths = [];
var files = [];
content
.replace(startCondReg, '')
.replace(endCondReg, '')
.replace(/<!--(?:(?:.|\r|\n)*?)-->/gim, '')
.replace(reg, function (a, quote, b) {
var filePath = path.resolve(path.join(alternatePath || mainPath, b));
content
.replace(startCondReg, '')
.replace(endCondReg, '')
.replace(/<!--(?:(?:.|\r|\n)*?)-->/gim, '')
.replace(reg, function (a, b) {
var filePath = path.resolve(path.join(alternatePath || mainPath, b));
if (options.assetsDir)
filePath = path.resolve(path.join(options.assetsDir, path.relative(basePath, filePath)));
if (options.assetsDir)
filePath = path.resolve(path.join(options.assetsDir, path.relative(basePath, filePath)));
paths.push(filePath);
});
paths.push(filePath);
});
for (var i = 0, l = paths.length; i < l; ++i) {
var filepaths = glob.sync(paths[i]);
if(filepaths[0] === undefined) {
throw new gutil.PluginError('gulp-usemin', 'Path ' + paths[i] + ' not found!');
}
filepaths.forEach(function (filepath) {
files.push(new gutil.File({
path: filepath,
contents: fs.readFileSync(filepath)
}));
});
}
for (var i = 0, l = paths.length; i < l; ++i) {
var filepaths = glob.sync(paths[i]);
if(filepaths[0] === undefined) {
throw new gutil.PluginError('gulp-usemin', 'Path ' + paths[i] + ' not found!');
}
filepaths.forEach(function (filepath) {
files.push(new gutil.File({
path: filepath,
contents: fs.readFileSync(filepath)
}));
});
}
return files;
}
return files;
}
function concat(files, name) {
var buffer = [];
function concat(files, name) {
var buffer = [];
files.forEach(function(file) {
buffer.push(String(file.contents));
});
files.forEach(function(file) {
buffer.push(String(file.contents));
});
return createFile(name, buffer.join(EOL));
}
return createFile(name, buffer.join(EOL));
}
function processTask(index, tasks, name, files, callback) {
var newFiles = [];
function processTask(index, tasks, name, files, callback) {
var newFiles = [];
if (tasks[index] == 'concat') {
newFiles = [concat(files, name)];
}
else {
var stream = tasks[index];
if (tasks[index] == 'concat') {
newFiles = [concat(files, name)];
}
else {
var stream = tasks[index];
function write(file) {
newFiles.push(file);
}
function write(file) {
newFiles.push(file);
}
stream.on('data', write);
files.forEach(function(file) {
stream.write(file);
});
stream.removeListener('data', write);
}
stream.on('data', write);
files.forEach(function(file) {
stream.write(file);
});
stream.removeListener('data', write);
}
if (tasks[++index])
processTask(index, tasks, name, newFiles, callback);
else
newFiles.forEach(callback);
}
if (tasks[++index])
processTask(index, tasks, name, newFiles, callback);
else
newFiles.forEach(callback);
}
function process(name, files, pipelineId, callback) {
var tasks = options[pipelineId] || [];
if (tasks.indexOf('concat') == -1)
tasks.unshift('concat');
function process(name, files, pipelineId, callback) {
var tasks = options[pipelineId] || [];
if (tasks.indexOf('concat') == -1)
tasks.unshift('concat');
processTask(0, tasks, name, files, callback);
}
processTask(0, tasks, name, files, callback);
}
function processHtml(content, push, callback) {
var html = [];
var sections = content.split(endReg);
function processHtml(content, push, callback) {
var html = [];
var sections = content.split(endReg);
for (var i = 0, l = sections.length; i < l; ++i) {
if (sections[i].match(startReg)) {
var section = sections[i].split(startReg);
alternatePath = section[2];
for (var i = 0, l = sections.length; i < l; ++i)
if (sections[i].match(startReg)) {
var section = sections[i].split(startReg);
alternatePath = section[2];
html.push(section[0]);
html.push(section[0]);
var startCondLine = section[5].match(startCondReg);
var endCondLine = section[5].match(endCondReg);
if (startCondLine && endCondLine)
html.push(startCondLine[0]);
var startCondLine = section[5].match(startCondReg);
var endCondLine = section[5].match(endCondReg);
if (startCondLine && endCondLine)
html.push(startCondLine[0]);
if (section[1] !== 'remove') {
if (getBlockType(section[5]) == 'js') {
process(section[4], getFiles(section[5], jsReg), section[1], function(name, file) {
push(file);
if (path.extname(file.path) == '.js')
html.push('<script src="' + name.replace(path.basename(name), path.basename(file.path)) + '"></script>');
}.bind(this, section[3]));
} else {
process(section[4], getFiles(section[5], cssReg), section[1], function(name, file) {
push(file);
html.push('<link rel="stylesheet" href="' + name.replace(path.basename(name), path.basename(file.path)) + '"/>');
}.bind(this, section[3]));
}
}
if (getBlockType(section[5]) == 'js')
process(section[4], getFiles(section[5], jsReg), section[1], function(name, file) {
push(file);
if (path.extname(file.path) == '.js')
html.push('<script src="' + name.replace(path.basename(name), path.basename(file.path)) + '"></script>');
}.bind(this, section[3]));
else
process(section[4], getFiles(section[5], cssReg), section[1], function(name, file) {
push(file);
html.push('<link rel="stylesheet" href="' + name.replace(path.basename(name), path.basename(file.path)) + '"/>');
}.bind(this, section[3]));
if (startCondLine && endCondLine) {
html.push(endCondLine[0]);
}
} else {
html.push(sections[i]);
}
}
process(mainName, [createFile(mainName, html.join(''))], 'html', function(file) {
push(file);
callback();
});
}
if (startCondLine && endCondLine)
html.push(endCondLine[0]);
}
else
html.push(sections[i]);
return through.obj(function(file, enc, callback) {
if (file.isNull()) {
this.push(file); // Do nothing if no contents
callback();
}
else if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-usemin', 'Streams are not supported!'));
callback();
}
else {
basePath = file.base;
mainPath = path.dirname(file.path);
mainName = path.basename(file.path);
process(mainName, [createFile(mainName, html.join(''))], 'html', function(file) {
push(file);
callback();
});
}
return through.obj(function(file, enc, callback) {
if (file.isNull()) {
this.push(file); // Do nothing if no contents
callback();
}
else if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-usemin', 'Streams are not supported!'));
callback();
}
else {
basePath = file.base;
mainPath = path.dirname(file.path);
mainName = path.basename(file.path);
processHtml(String(file.contents), this.push.bind(this), callback);
}
});
processHtml(String(file.contents), this.push.bind(this), callback);
}
});
};
{
"name": "gulp-usemin",
"version": "0.3.7",
"version": "0.3.8",
"description": "Replaces references to non-optimized scripts or stylesheets into a set of HTML files (or any templates/views).",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -46,3 +46,3 @@ # gulp-usemin

- **pipelineId**: pipeline id for options
- **pipelineId**: pipeline id for options or *remove* to remove a section
- **alternate search path**: (optional) By default the input files are relative to the treated file. Alternate search path allows one to change that

@@ -70,2 +70,6 @@ - **path**: the file path of the optimized file, the target output

<!-- endbuild -->
<!-- build:remove -->
<script src="js/localhostDependencies.js"></script>
<!-- endbuild -->
```

@@ -159,2 +163,6 @@

#####0.3.8
- allow removal option (by tejohnso)
- added support for single quotes (by adicirstei)
#####0.3.7

@@ -161,0 +169,0 @@ - ouputRelativePath renamed outputRelativePath

@@ -20,635 +20,664 @@ /* jshint node: true */

function getFile(filePath) {
return new gutil.File({
path: filePath,
base: path.dirname(filePath),
contents: fs.readFileSync(filePath)
});
return new gutil.File({
path: filePath,
base: path.dirname(filePath),
contents: fs.readFileSync(filePath)
});
}
function getFixture(filePath) {
return getFile(path.join('test', 'fixtures', filePath));
return getFile(path.join('test', 'fixtures', filePath));
}
function getExpected(filePath) {
return getFile(path.join('test', 'expected', filePath));
return getFile(path.join('test', 'expected', filePath));
}
describe('gulp-usemin', function() {
describe('negative test:', function() {
it('shouldn\'t work in stream mode', function(done) {
var stream = usemin();
var t;
var fakeStream = new PassThrough();
var fakeFile = new gutil.File({
contents: fakeStream
});
fakeStream.end();
describe('allow removal sections', function() {
function compare(name, expectedName, done) {
var stream = usemin({html: [htmlmin({empty: true, quotes: true})]});
stream.on('error', function() {
clearTimeout(t);
done();
});
stream.on('data', function(newFile) {
if (path.basename(newFile.path) === name)
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
});
stream.on('end', function() {
done();
});
t = setTimeout(function() {
assert.fail('', '', 'Should throw error', '');
done();
}, 1000);
stream.write(getFixture(name));
stream.end();
}
stream.write(fakeFile);
stream.end();
});
it('simple js block', function(done) {
compare('simple-js-removal.html', 'simple-js-removal.html', done);
});
it('html without blocks', function(done) {
var stream = usemin();
var content = '<div>content</div>';
var fakeFile = new gutil.File({
path: 'test.file',
contents: new Buffer(content)
});
it('minified js block', function(done) {
compare('min-html-simple-removal.html', 'min-html-simple-removal.html', done);
});
stream.on('data', function(newFile) {
assert.equal(content, String(newFile.contents));
});
it('many blocks', function(done) {
compare('many-blocks-removal.html', 'many-blocks-removal.html', done);
});
});
stream.on('end', function() {
done();
});
describe('negative test:', function() {
it('shouldn\'t work in stream mode', function(done) {
var stream = usemin();
var t;
var fakeStream = new PassThrough();
var fakeFile = new gutil.File({
contents: fakeStream
});
fakeStream.end();
stream.write(fakeFile);
stream.end();
});
});
stream.on('error', function() {
clearTimeout(t);
done();
});
describe('should work in buffer mode with', function() {
describe('minified HTML:', function() {
function compare(name, expectedName, done) {
var stream = usemin({html: [htmlmin({empty: true})]});
t = setTimeout(function() {
assert.fail('', '', 'Should throw error', '');
done();
}, 1000);
stream.on('data', function(newFile) {
if (path.basename(newFile.path) === name)
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
});
stream.on('end', function() {
done();
});
stream.write(fakeFile);
stream.end();
});
stream.write(getFixture(name));
stream.end();
}
it('html without blocks', function(done) {
var stream = usemin();
var content = '<div>content</div>';
var fakeFile = new gutil.File({
path: 'test.file',
contents: new Buffer(content)
});
it('simple js block', function(done) {
compare('simple-js.html', 'min-simple-js.html', done);
});
stream.on('data', function(newFile) {
assert.equal(content, String(newFile.contents));
});
it('simple js block with path', function(done) {
compare('simple-js-path.html', 'min-simple-js-path.html', done);
});
stream.on('end', function() {
done();
});
it('simple css block', function(done) {
compare('simple-css.html', 'min-simple-css.html', done);
});
stream.write(fakeFile);
stream.end();
});
});
it('simple css block with path', function(done) {
compare('simple-css-path.html', 'min-simple-css-path.html', done);
});
describe('should work in buffer mode with', function() {
describe('minified HTML:', function() {
function compare(name, expectedName, done) {
var stream = usemin({html: [htmlmin({empty: true})]});
it('complex (css + js)', function(done) {
compare('complex.html', 'min-complex.html', done);
});
stream.on('data', function(newFile) {
if (path.basename(newFile.path) === name)
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
});
stream.on('end', function() {
done();
});
it('complex with path (css + js)', function(done) {
compare('complex-path.html', 'min-complex-path.html', done);
});
});
stream.write(getFixture(name));
stream.end();
}
describe('not minified HTML:', function() {
function compare(name, expectedName, done) {
var stream = usemin();
it('simple js block', function(done) {
compare('simple-js.html', 'min-simple-js.html', done);
});
stream.on('data', function(newFile) {
if (path.basename(newFile.path) === name)
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
});
stream.on('end', function() {
done();
});
it('simple js block with path', function(done) {
compare('simple-js-path.html', 'min-simple-js-path.html', done);
});
stream.write(getFixture(name));
stream.end();
}
it('simple css block', function(done) {
compare('simple-css.html', 'min-simple-css.html', done);
});
it('simple (js block)', function(done) {
compare('simple-js.html', 'simple-js.html', done);
});
it('simple css block with path', function(done) {
compare('simple-css-path.html', 'min-simple-css-path.html', done);
});
it('simple (js block) (html minified)', function(done) {
compare('min-html-simple-js.html', 'min-html-simple-js.html', done);
});
it('complex (css + js)', function(done) {
compare('complex.html', 'min-complex.html', done);
});
it('simple with path (js block)', function(done) {
compare('simple-js-path.html', 'simple-js-path.html', done);
});
it('complex with path (css + js)', function(done) {
compare('complex-path.html', 'min-complex-path.html', done);
});
});
it('simple (css block)', function(done) {
compare('simple-css.html', 'simple-css.html', done);
});
describe('not minified HTML:', function() {
function compare(name, expectedName, done) {
var stream = usemin();
it('simple (css block) (html minified)', function(done) {
compare('min-html-simple-css.html', 'min-html-simple-css.html', done);
});
stream.on('data', function(newFile) {
if (path.basename(newFile.path) === name)
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
});
stream.on('end', function() {
done();
});
it('simple with path (css block)', function(done) {
compare('simple-css-path.html', 'simple-css-path.html', done);
});
stream.write(getFixture(name));
stream.end();
}
it('complex (css + js)', function(done) {
compare('complex.html', 'complex.html', done);
});
it('simple (js block)', function(done) {
compare('simple-js.html', 'simple-js.html', done);
});
it('complex with path (css + js)', function(done) {
compare('complex-path.html', 'complex-path.html', done);
});
it('simple (js block) (html minified)', function(done) {
compare('min-html-simple-js.html', 'min-html-simple-js.html', done);
});
it('multiple alternative paths', function(done) {
compare('multiple-alternative-paths.html', 'multiple-alternative-paths.html', done);
});
});
it('simple with path (js block)', function(done) {
compare('simple-js-path.html', 'simple-js-path.html', done);
});
describe('minified CSS:', function() {
function compare(name, callback, end) {
var stream = usemin({css: ['concat', cssmin()]});
it('simple (css block)', function(done) {
compare('simple-css.html', 'simple-css.html', done);
});
stream.on('data', callback);
stream.on('end', end);
it('simple (css block) (html minified)', function(done) {
compare('min-html-simple-css.html', 'min-html-simple-css.html', done);
});
stream.write(getFixture(name));
stream.end();
}
it('simple with path (css block)', function(done) {
compare('simple-css-path.html', 'simple-css-path.html', done);
});
it('simple (css block)', function(done) {
var name = 'style.css';
var expectedName = 'min-style.css';
var exist = false;
it('complex (css + js)', function(done) {
compare('complex.html', 'complex.html', done);
});
compare(
'simple-css.html',
function(newFile) {
if (newFile.path === name) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
it('complex with path (css + js)', function(done) {
compare('complex-path.html', 'complex-path.html', done);
});
it('simple with path (css block)', function(done) {
var name = path.join('data', 'css', 'style.css');
var expectedName = path.join('data', 'css', 'min-style.css');
var exist = false;
it('multiple alternative paths', function(done) {
compare('multiple-alternative-paths.html', 'multiple-alternative-paths.html', done);
});
});
compare(
'simple-css-path.html',
function(newFile) {
if (newFile.path === name) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
describe('minified CSS:', function() {
function compare(name, callback, end) {
var stream = usemin({css: ['concat', cssmin()]});
it('simple with alternate path (css block)', function(done) {
var name = path.join('data', 'css', 'style.css');
var expectedName = path.join('data', 'css', 'min-style.css');
var exist = false;
stream.on('data', callback);
stream.on('end', end);
compare(
'simple-css-alternate-path.html',
function(newFile) {
if (newFile.path === name) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
});
stream.write(getFixture(name));
stream.end();
}
describe('not minified CSS:', function() {
function compare(name, callback, end) {
var stream = usemin();
it('simple (css block)', function(done) {
var name = 'style.css';
var expectedName = 'min-style.css';
var exist = false;
stream.on('data', callback);
stream.on('end', end);
compare(
'simple-css.html',
function(newFile) {
if (newFile.path === name) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
stream.write(getFixture(name));
stream.end();
}
it('simple with path (css block)', function(done) {
var name = path.join('data', 'css', 'style.css');
var expectedName = path.join('data', 'css', 'min-style.css');
var exist = false;
it('simple (css block)', function(done) {
var expectedName = 'style.css';
var exist = false;
compare(
'simple-css-path.html',
function(newFile) {
if (newFile.path === name) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
compare(
'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 alternate path (css block)', function(done) {
var name = path.join('data', 'css', 'style.css');
var expectedName = path.join('data', 'css', 'min-style.css');
var exist = false;
it('simple (css block) (minified html)', function(done) {
var expectedName = 'style.css';
var exist = false;
compare(
'simple-css-alternate-path.html',
function(newFile) {
if (newFile.path === name) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
});
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();
}
);
});
describe('not minified CSS:', function() {
function compare(name, callback, end) {
var stream = usemin();
it('simple with path (css block)', function(done) {
var expectedName = path.join('data', 'css', 'style.css');
var exist = false;
stream.on('data', callback);
stream.on('end', end);
compare(
'simple-css-path.html',
function(newFile) {
if (newFile.path === expectedName) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
stream.write(getFixture(name));
stream.end();
}
it('simple with alternate path (css block)', function(done) {
var expectedName = path.join('data', 'css', 'style.css');
var exist = false;
it('simple (css block)', function(done) {
var expectedName = 'style.css';
var exist = false;
compare(
'simple-css-alternate-path.html',
function(newFile) {
if (newFile.path === expectedName) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
});
compare(
'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();
}
);
});
describe('minified JS:', function() {
function compare(name, callback, end) {
var stream = usemin({js: [jsmin()]});
it('simple (css block) (minified html)', function(done) {
var expectedName = 'style.css';
var exist = false;
stream.on('data', callback);
stream.on('end', end);
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();
}
);
});
stream.write(getFixture(name));
stream.end();
}
it('simple with path (css block)', function(done) {
var expectedName = path.join('data', 'css', 'style.css');
var exist = false;
it('simple (js block)', function(done) {
var name = 'app.js';
var expectedName = 'min-app.js';
var exist = false;
compare(
'simple-css-path.html',
function(newFile) {
if (newFile.path === expectedName) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
compare(
'simple-js.html',
function(newFile) {
if (newFile.path === name) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
it('simple with alternate path (css block)', function(done) {
var expectedName = path.join('data', 'css', 'style.css');
var exist = false;
it('simple with path (js block)', function(done) {
var name = path.join('data', 'js', 'app.js');
var expectedName = path.join('data', 'js', 'min-app.js');
var exist = false;
compare(
'simple-css-alternate-path.html',
function(newFile) {
if (newFile.path === expectedName) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
});
compare(
'simple-js-path.html',
function(newFile) {
if (newFile.path === name) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
describe('minified JS:', function() {
function compare(name, callback, end) {
var stream = usemin({js: [jsmin()]});
it('simple with alternate path (js block)', function(done) {
var name = path.join('data', 'js', 'app.js');
var expectedName = path.join('data', 'js', 'min-app.js');
var exist = false;
stream.on('data', callback);
stream.on('end', end);
compare(
'simple-js-alternate-path.html',
function(newFile) {
if (newFile.path === name) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
});
stream.write(getFixture(name));
stream.end();
}
describe('not minified JS:', function() {
function compare(name, callback, end) {
var stream = usemin();
it('simple (js block)', function(done) {
var name = 'app.js';
var expectedName = 'min-app.js';
var exist = false;
stream.on('data', callback);
stream.on('end', end);
compare(
'simple-js.html',
function(newFile) {
if (newFile.path === name) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
stream.write(getFixture(name));
stream.end();
}
it('simple with path (js block)', function(done) {
var name = path.join('data', 'js', 'app.js');
var expectedName = path.join('data', 'js', 'min-app.js');
var exist = false;
it('simple (js block)', function(done) {
var expectedName = 'app.js';
var exist = false;
compare(
'simple-js-path.html',
function(newFile) {
if (newFile.path === name) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
compare(
'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 alternate path (js block)', function(done) {
var name = path.join('data', 'js', 'app.js');
var expectedName = path.join('data', 'js', 'min-app.js');
var exist = false;
it('simple (js block) (minified html)', function(done) {
var expectedName = 'app.js';
var exist = false;
compare(
'simple-js-alternate-path.html',
function(newFile) {
if (newFile.path === name) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
});
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();
}
);
});
describe('not minified JS:', function() {
function compare(name, callback, end) {
var stream = usemin();
it('simple with path (js block)', function(done) {
var expectedName = path.join('data', 'js', 'app.js');
var exist = false;
stream.on('data', callback);
stream.on('end', end);
compare(
'simple-js-path.html',
function(newFile) {
if (newFile.path === expectedName) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
stream.write(getFixture(name));
stream.end();
}
it('simple with alternate path (js block)', function(done) {
var expectedName = path.join('data', 'js', 'app.js');
var exist = false;
it('simple (js block)', function(done) {
var expectedName = 'app.js';
var exist = false;
compare(
'simple-js-alternate-path.html',
function(newFile) {
if (newFile.path === expectedName) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
});
compare(
'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('many blocks', function(done) {
var stream = usemin({
css1: ['concat', cssmin()],
js1: [jsmin(), 'concat', rev()]
});
it('simple (js block) (minified html)', function(done) {
var expectedName = 'app.js';
var exist = false;
var nameCss = path.join('data', 'css', 'style.css');
var expectedNameCss = path.join('data', 'css', 'min-style.css');
var nameJs = path.join('data', 'js', 'app.js');
var expectedNameJs = path.join('data', 'js', 'app.js');
var nameJs1 = 'app1';
var expectedNameJs1 = path.join('data', 'js', 'app_min_concat.js');
var cssExist = false;
var jsExist = false;
var js1Exist = false;
var htmlExist = 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();
}
);
});
stream.on('data', function(newFile) {
if (newFile.path === nameCss) {
cssExist = true;
assert.equal(String(getExpected(expectedNameCss).contents), String(newFile.contents));
}
else if (newFile.path === nameJs) {
jsExist = true;
assert.equal(String(getExpected(expectedNameJs).contents), String(newFile.contents));
}
else if (newFile.path.indexOf(nameJs1) != -1) {
js1Exist = true;
assert.equal(String(getExpected(expectedNameJs1).contents), String(newFile.contents));
}
else {
htmlExist = true;
}
});
stream.on('end', function() {
assert.ok(cssExist);
assert.ok(jsExist);
assert.ok(js1Exist);
assert.ok(htmlExist);
done();
});
it('simple with path (js block)', function(done) {
var expectedName = path.join('data', 'js', 'app.js');
var exist = false;
stream.write(getFixture('many-blocks.html'));
stream.end();
});
compare(
'simple-js-path.html',
function(newFile) {
if (newFile.path === expectedName) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
describe('assetsDir option:', function() {
function compare(assetsDir, done) {
var stream = usemin({assetsDir: assetsDir});
var expectedName = 'style.css';
var exist = false;
var callback = function(newFile) {
if (newFile.path === expectedName) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
};
var end = function() {
assert.ok(exist);
done();
};
it('simple with alternate path (js block)', function(done) {
var expectedName = path.join('data', 'js', 'app.js');
var exist = false;
stream.on('data', callback);
stream.on('end', end);
compare(
'simple-js-alternate-path.html',
function(newFile) {
if (newFile.path === expectedName) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
});
stream.write(getFixture('simple-css.html'));
stream.end();
}
it('many blocks', function(done) {
var stream = usemin({
css1: ['concat', cssmin()],
js1: [jsmin(), 'concat', rev()]
});
it('absolute path', function(done) {
compare(path.join(process.cwd(), 'test', 'fixtures'), done);
});
var nameCss = path.join('data', 'css', 'style.css');
var expectedNameCss = path.join('data', 'css', 'min-style.css');
var nameJs = path.join('data', 'js', 'app.js');
var expectedNameJs = path.join('data', 'js', 'app.js');
var nameJs1 = 'app1';
var expectedNameJs1 = path.join('data', 'js', 'app_min_concat.js');
var cssExist = false;
var jsExist = false;
var js1Exist = false;
var htmlExist = false;
it('relative path', function(done) {
compare(path.join('test', 'fixtures'), done);
});
});
stream.on('data', function(newFile) {
if (newFile.path === nameCss) {
cssExist = true;
assert.equal(String(getExpected(expectedNameCss).contents), String(newFile.contents));
}
else if (newFile.path === nameJs) {
jsExist = true;
assert.equal(String(getExpected(expectedNameJs).contents), String(newFile.contents));
}
else if (newFile.path.indexOf(nameJs1) != -1) {
js1Exist = true;
assert.equal(String(getExpected(expectedNameJs1).contents), String(newFile.contents));
}
else {
htmlExist = true;
}
});
stream.on('end', function() {
assert.ok(cssExist);
assert.ok(jsExist);
assert.ok(js1Exist);
assert.ok(htmlExist);
done();
});
describe('conditional comments:', function() {
function compare(name, expectedName, done) {
var stream = usemin();
stream.write(getFixture('many-blocks.html'));
stream.end();
});
stream.on('data', function(newFile) {
if (path.basename(newFile.path) === name)
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
});
stream.on('end', function() {
done();
});
describe('assetsDir option:', function() {
function compare(assetsDir, done) {
var stream = usemin({assetsDir: assetsDir});
var expectedName = 'style.css';
var exist = false;
var callback = function(newFile) {
if (newFile.path === expectedName) {
exist = true;
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
}
};
var end = function() {
assert.ok(exist);
done();
};
stream.write(getFixture(name));
stream.end();
}
stream.on('data', callback);
stream.on('end', end);
it('conditional (js block)', function(done) {
compare('conditional-js.html', 'conditional-js.html', done);
});
stream.write(getFixture('simple-css.html'));
stream.end();
}
it('conditional (css block)', function(done) {
compare('conditional-css.html', 'conditional-css.html', done);
});
it('absolute path', function(done) {
compare(path.join(process.cwd(), 'test', 'fixtures'), done);
});
it('conditional (css + js)', function(done) {
compare('conditional-complex.html', 'conditional-complex.html', done);
});
});
it('relative path', function(done) {
compare(path.join('test', 'fixtures'), done);
});
});
describe('globbed files:', function() {
function compare(name, callback, end) {
var stream = usemin();
describe('conditional comments:', function() {
function compare(name, expectedName, done) {
var stream = usemin();
stream.on('data', callback);
stream.on('end', end);
stream.on('data', function(newFile) {
if (path.basename(newFile.path) === name)
assert.equal(String(getExpected(expectedName).contents), String(newFile.contents));
});
stream.on('end', function() {
done();
});
stream.write(getFixture(name));
stream.end();
}
stream.write(getFixture(name));
stream.end();
}
it('glob (js block)', function(done) {
var expectedName = 'app.js';
var exist = false;
it('conditional (js block)', function(done) {
compare('conditional-js.html', 'conditional-js.html', done);
});
compare(
'glob-js.html',
function(newFile) {
if (newFile.path === expectedName) {
exist = true;
assert.equal(String(newFile.contents), String(getExpected(expectedName).contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
it('conditional (css block)', function(done) {
compare('conditional-css.html', 'conditional-css.html', done);
});
it('glob (css block)', function(done) {
var expectedName = 'style.css';
var exist = false;
it('conditional (css + js)', function(done) {
compare('conditional-complex.html', 'conditional-complex.html', done);
});
});
compare(
'glob-css.html',
function(newFile) {
if (newFile.path === expectedName) {
exist = true;
assert.equal(String(newFile.contents), String(getExpected(expectedName).contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
});
});
describe('globbed files:', function() {
function compare(name, callback, end) {
var stream = usemin();
stream.on('data', callback);
stream.on('end', end);
stream.write(getFixture(name));
stream.end();
}
it('glob (js block)', function(done) {
var expectedName = 'app.js';
var exist = false;
compare(
'glob-js.html',
function(newFile) {
if (newFile.path === expectedName) {
exist = true;
assert.equal(String(newFile.contents), String(getExpected(expectedName).contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
it('glob (css block)', function(done) {
var expectedName = 'style.css';
var exist = false;
compare(
'glob-css.html',
function(newFile) {
if (newFile.path === expectedName) {
exist = true;
assert.equal(String(newFile.contents), String(getExpected(expectedName).contents));
}
},
function() {
assert.ok(exist);
done();
}
);
});
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc