diff --git a/index.js b/index.js index 45e5c49..f0c732a 100644 --- a/index.js +++ b/index.js @@ -34,12 +34,14 @@ function addAssetsToStream(paths, files) { concat = require('gulp-concat'), isRelativeUrl = require('is-relative-url'), vfs = require('vinyl-fs'), + extend = require('extend'), src, globs, name = paths.name, basePath = paths.basePath, filepaths = files[name].assets, - options = pluginOptions, + type = paths.type, + options = extend({}, pluginOptions), gulpConcatOptions = {}; if (!filepaths.length) { @@ -75,6 +77,9 @@ function addAssetsToStream(paths, files) { // option for newLine in gulp-concat if (options.hasOwnProperty('newLine')) { + if (options.newLine === ';' && type === 'css') { + options.newLine = null; + } gulpConcatOptions.newLine = options.newLine; } @@ -121,7 +126,8 @@ function processAssets(file, basePath, data) { basePath: basePath, searchPath: pluginOptions.searchPath, cwd: file.cwd, - transformPath: pluginOptions.transformPath + transformPath: pluginOptions.transformPath, + type: type }, files); } }); diff --git a/package.json b/package.json index a0b574b..2cfde8e 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ }, "dependencies": { "event-stream": "^3.3.4", + "extend": "^3.0.1", "glob": "^7.1.2", "gulp-concat": "^2.6.1", "gulp-if": "^2.0.2", diff --git a/test/test.js b/test/test.js index 04aba0a..b32d365 100644 --- a/test/test.js +++ b/test/test.js @@ -165,6 +165,72 @@ describe('useref()', function() { stream.end(); }); + it('should concat CSS assets with newLine option', function (done) { + var a = 0, + + testFile = getFixture('01.html'), + separator = '\r\n', + + stream = useref({newLine: separator}), + + buffer1 = new Buffer(fs.readFileSync(path.join('test', 'fixtures', 'css', 'one.css'))), + buffer2 = new Buffer(separator), + buffer3 = new Buffer(fs.readFileSync(path.join('test', 'fixtures', 'css', 'two.css'))), + 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/css/combined.css')); + 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 but skip newLine option if semicolon', function (done) { + var a = 0, + + testFile = getFixture('01.html'), + separator = ';', + + stream = useref({newLine: separator}), + + buffer1 = new Buffer(fs.readFileSync(path.join('test', 'fixtures', 'css', 'one.css'))), + buffer2 = new Buffer('\n'), + buffer3 = new Buffer(fs.readFileSync(path.join('test', 'fixtures', 'css', 'two.css'))), + 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/css/combined.css')); + 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 skip concatenation and pass CSS assets through with noconcat option', function(done) { var a = 0; @@ -253,6 +319,39 @@ describe('useref()', function() { stream.end(); }); + it('should concat JS assets with newLine option if semicolon', function(done) { + var a = 0, + + testFile = getFixture('02.html'), + separator = ';', + + stream = useref({newLine: separator}), + + 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 skip concatenation and pass JS assets through with noconcat option', function(done) { var a = 0; diff --git a/yarn.lock b/yarn.lock index e48ac98..c76ef25 100644 --- a/yarn.lock +++ b/yarn.lock @@ -847,7 +847,7 @@ extend-shallow@^3.0.2: assign-symbols "^1.0.0" is-extendable "^1.0.1" -extend@^3.0.0, extend@~3.0.1: +extend@^3.0.0, extend@^3.0.1, extend@~3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.1.tgz#a755ea7bc1adfcc5a31ce7e762dbaadc5e636444"