Skip to content

Commit

Permalink
Merge d27377e into 53efa13
Browse files Browse the repository at this point in the history
  • Loading branch information
lfurzewaddock committed Mar 7, 2018
2 parents 53efa13 + d27377e commit e686eef
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
26 changes: 23 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ function handleAdditionalStreams(additionalStreams) {
});
}

function isNewLineExemptFileExt(newLineExemptFileExts, pathsName) {
var regex = new RegExp('\\b.?(' + path.extname(pathsName).replace(/^\./, '') + '\\b)', 'gi');

return regex.test(newLineExemptFileExts.toString());
}

function addNewLineText(options, pathsName) {
var newLine = null;

if (options.hasOwnProperty('newLine') && typeof options.newLine === 'string') {
// pass newLine string argument to gulp-concat
newLine = options.newLine;
}

if (options.hasOwnProperty('newLineExemptFileExts') && isNewLineExemptFileExt(options.newLineExemptFileExts, pathsName)) {
// pass newLine null argument to gulp-concat
newLine = null;
}

return newLine;
}

function addAssetsToStream(paths, files) {
var self = this,
gulpif = require('gulp-if'),
Expand Down Expand Up @@ -74,9 +96,7 @@ function addAssetsToStream(paths, files) {
});

// option for newLine in gulp-concat
if (options.hasOwnProperty('newLine')) {
gulpConcatOptions.newLine = options.newLine;
}
gulpConcatOptions.newLine = addNewLineText(options, name);

// Add assets to the stream
// If noconcat option is false, concat the files first.
Expand Down
7 changes: 7 additions & 0 deletions test/expected/css/concat-new-line-exempt-file-exts.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.a {
/* */
}

.z {
/* */
}
4 changes: 4 additions & 0 deletions test/fixtures/concat-css-new-line-exempt-file-exts.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- build:css /css/concat-new-line-exempt-file-exts.css -->
<link href="/css/a.css" rel="stylesheet">
<link href="/css/z.css" rel="stylesheet">
<!-- endbuild -->
64 changes: 64 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,70 @@ describe('useref()', function() {
stream.end();
});

it('should concat JS assets with newLine option when newLineExemptFileExts does not contain variant of js file extension', function(done) {
var a = 0,

testFile = getFixture('02.html'),
separator = ';',
newLineString = ';',
exemptfileExts = ['.css','.CSS', 'css', 'CSS'],

stream = useref({newLine: newLineString, newLineExemptFileExts: exemptfileExts}),

buffer1 = new Buffer(fs.readFileSync(path.join('test', 'fixtures', 'scripts', 'this.js'))),
buffer2 = new Buffer(separator),
buffer3 = new Buffer(fs.readFileSync(path.join('test', 'fixtures', 'scripts', 'that.js'))),
bufferFinal = Buffer.concat([buffer1, buffer2, buffer3]),

fileFinal = new Vinyl({ contents: bufferFinal });

stream.on('data', function(newFile){
if (a === 1) {
newFile.path.should.equal(path.normalize('./test/fixtures/scripts/combined.js'));
newFile.contents.toString().should.equal(fileFinal.contents.toString());
};
++a;
});

stream.once('end', function () {
a.should.equal(2);
done();
});

stream.write(testFile);

stream.end();
});

it('should concat CSS assets and skip newLine option if newLineExemptFileExts contains variant of css file extension', function(done) {
var a = 0,

testFile = getFixture('concat-css-new-line-exempt-file-exts.html'),
newLineString = ';',
exemptfileExts = ['.css','.CSS', 'css', 'CSS'],

stream = useref({newLine: newLineString, newLineExemptFileExts: exemptfileExts});

stream.on('data', function(newFile){
should.exist(newFile.contents);
if (a === 1) {
newFile.path.should.equal(path.normalize('./test/fixtures/css/concat-new-line-exempt-file-exts.css'));
newFile.contents.toString().should.equal(getExpected('css/concat-new-line-exempt-file-exts.css').contents.toString());
}
++a;
});


stream.once('end', function () {
a.should.equal(2);
done();
});

stream.write(testFile);

stream.end();
});

it('should skip concatenation and pass JS assets through with noconcat option', function(done) {
var a = 0;

Expand Down

0 comments on commit e686eef

Please sign in to comment.