Skip to content

Commit

Permalink
Merge pull request #3 from haensl/feature/2
Browse files Browse the repository at this point in the history
Add option to specify mime types.
  • Loading branch information
haensl authored Jan 24, 2018
2 parents 01427ca + 42e8a7e commit 99d479b
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 1.2.0
* Add option to specify mime types

# 1.1.0
* Add travis-ci integration

Expand Down
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,60 @@ gulp.task('minifyInlineJSON', () =>
.pipe(gulp.dest('dist/')));
```

## Options

### mimeTypes `Array<string>`

Provide custom mime types to specify which `<script>` tags to minify.

##### default: `[ 'application/json', 'application/ld+json' ]`

##### Example: Minify only tags with `type="application/ld+json"`

###### HTML Layout
```html
<html>
<head><!-- ... --></head>
<body>
<!-- ... -->
<script type="application/json">{
"some": "json"
}</script>
<script type="application/ld+json">{
"foo": "bar"
}</script>
</body>
</html>
```

###### Gulp task
```javascript
const minifyJSON = require('gulp-minify-inline-json');

gulp.task('minifyJSON', () =>
gulp.src('*.html')
.pipe(minifyJSON({
mimeTypes: [
'application/ld+json'
]
}))
.pipe(gulp.dest('dist/')));
```

###### Output
```html
<html>
<head><!-- ... --></head>
<body>
<!-- ... -->
<script type="application/json">{
"some": "json"
}</script>
<script type="application/ld+json">{"foo":"bar"}</script>
</body>
</html>
```

## [Changelog](CHANGELOG.md)

## [License](LICENSE)
Expand Down
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ const cheerio = require('cheerio');
const gutil = require('gulp-util');

const PLUGIN_NAME = require('./package').name;
const mimeTypes = [
'application/json',
'application/ld+json'
];

module.exports = () =>
const DEFAULTS = {
mimeTypes: [
'application/json',
'application/ld+json'
]
};

module.exports = (options = {}) =>
through.obj((file, encoding, callback) => {
if (file.isNull()) {
return callback(null, file);
Expand All @@ -21,6 +23,10 @@ module.exports = () =>
const $ = cheerio.load(file.contents.toString());
let didMinify = false;

const mimeTypes = Array.isArray(options.mimeTypes)
? options.mimeTypes
: DEFAULTS.mimeTypes;

$(mimeTypes.map((type) => `script[type="${type}"]`).join(','))
.each(function() {
const script = $(this);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-minify-inline-json",
"version": "1.1.0",
"version": "1.2.0",
"description": "Gulp plugin to minify inline JSON.",
"main": "index.js",
"scripts": {
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/json+jsonld.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html>
<head>
<script type="application/json">
{
"type": "json",
"parent": "head"
}
</script>
</head>
<body>
<script type="application/ld+json">
{
"type": "jsonld",
"parent": "body"
}
</script>
</body>
</html>
50 changes: 50 additions & 0 deletions test/specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,54 @@ describe('gulp-minify-inline-json', () => {
expect(minifier()).not.to.throw;
});
});

describe('options', () => {
let output;

describe('mimeTypes', () => {
describe('application/json', () => {
beforeEach((done) => {
gulp.src(fixtures('json+jsonld.html'))
.pipe(minifier({
mimeTypes: [
'application/json'
]
})).pipe(through.obj((file) => {
output = file.contents.toString();
done();
}));
});

it('minifies application/json', () => {
expect(/{"type":"json","parent":"head"}/.test(output)).to.be.true;
});

it('does not minify application/ld+json', () => {
expect(/{"type":"jsonld","parent":"body"}/.test(output)).to.be.false;
});
});

describe('application/ld+json', () => {
beforeEach((done) => {
gulp.src(fixtures('json+jsonld.html'))
.pipe(minifier({
mimeTypes: [
'application/ld+json'
]
})).pipe(through.obj((file) => {
output = file.contents.toString();
done();
}));
});

it('minifies application/json', () => {
expect(/{"type":"json","parent":"head"}/.test(output)).to.be.false;
});

it('does not minify application/ld+json', () => {
expect(/{"type":"jsonld","parent":"body"}/.test(output)).to.be.true;
});
});
});
});
});

0 comments on commit 99d479b

Please sign in to comment.