diff --git a/README.md b/README.md index 91fdd47..fd62bcb 100644 --- a/README.md +++ b/README.md @@ -182,10 +182,19 @@ Skip concatenation and add all assets to the stream instead. #### options.newLine -Type: `String` +Type: `String` or `Object` Default: `none` -Add a string that should separate the concatenated files. +Add a string that should separate the concatenated files. To specify a newLine per type of file processed, provide an object with types as keys and the newLine options as values. For example: + +```javascript +useref({ + newLine: { + js: ';\n', + css: '\n' + } +}) +``` #### options.additionalStreams diff --git a/index.js b/index.js index 2c0fd04..ecf066e 100644 --- a/index.js +++ b/index.js @@ -73,7 +73,9 @@ function addAssetsToStream(paths, files) { // option for newLine in gulp-concat if (Object.prototype.hasOwnProperty.call(options, 'newLine')) { - if (options.newLine === ';' && type === 'css') { + if (typeof options.newLine === 'object') { + options.newLine = options.newLine[type]; + } else if (options.newLine === ';' && type === 'css') { options.newLine = null; } gulpConcatOptions.newLine = options.newLine; diff --git a/test/fixtures/13.html b/test/fixtures/13.html new file mode 100644 index 0000000..9ac4864 --- /dev/null +++ b/test/fixtures/13.html @@ -0,0 +1,12 @@ + +
+ + + + + + + + + + diff --git a/test/test.js b/test/test.js index b31baba..8344c4b 100644 --- a/test/test.js +++ b/test/test.js @@ -335,6 +335,51 @@ describe('useref()', function() { stream.end(); }); + it('should support per type newLine options', done => { + let a = 0; + const testFile = getFixture('13.html'); + const jsSeparator = ';'; + const cssSeparator = '\n'; + const stream = useref({ + newLine: { + js: jsSeparator, + css: cssSeparator + } + }); + const bufferJS1 = Buffer.from(fs.readFileSync(path.join('test', 'fixtures', 'scripts', 'this.js'))); + const bufferJS2 = Buffer.from(jsSeparator); + const bufferJS3 = Buffer.from(fs.readFileSync(path.join('test', 'fixtures', 'scripts', 'that.js'))); + const bufferJSFinal = Buffer.concat([bufferJS1, bufferJS2, bufferJS3]); + const fileJSFinal = new Vinyl({ contents: bufferJSFinal }); + + const bufferCSS1 = Buffer.from(fs.readFileSync(path.join('test', 'fixtures', 'css', 'one.css'))); + const bufferCSS2 = Buffer.from(cssSeparator); + const bufferCSS3 = Buffer.from(fs.readFileSync(path.join('test', 'fixtures', 'css', 'two.css'))); + const bufferCSSFinal = Buffer.concat([bufferCSS1, bufferCSS2, bufferCSS3]); + const fileCSSFinal = new Vinyl({ contents: bufferCSSFinal }); + + stream.on('data', newFile => { + if (a === 1) { + newFile.path.should.equal(path.normalize('./test/fixtures/css/combined.css')); + newFile.contents.toString().should.equal(fileCSSFinal.contents.toString()); + } + if (a === 2) { + newFile.path.should.equal(path.normalize('./test/fixtures/scripts/combined.js')); + newFile.contents.toString().should.equal(fileJSFinal.contents.toString()); + } + ++a; + }); + + stream.once('end', () => { + a.should.equal(3); + done(); + }); + + stream.write(testFile); + + stream.end(); + }); + it('should skip concatenation and pass JS assets through with noconcat option', done => { let a = 0;