Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Feature: Per type newLine separator #263

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/13.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<head>
<!-- build:js scripts/combined.js -->
<script type="text/javascript" src="scripts/this.js"></script>
<script type="text/javascript" src="scripts/that.js"></script>
<!-- endbuild -->
<!-- build:css /css/combined.css -->
<link href="/css/one.css" rel="stylesheet">
<link href="/css/two.css" rel="stylesheet">
<!-- endbuild -->
</head>
</html>
45 changes: 45 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down