shkuznetsov / gulp-minify-inline

gulp plugin that minifies inline JS and CSS
MIT License
28 stars 5 forks source link

Minify inline JSON #10

Closed VaclavSynacek closed 4 years ago

VaclavSynacek commented 8 years ago

Uglify has a CLI parameter --expr or parser oprion expression for parsing JSON. How should I set up options for gulp-minify-inline so that it invokes Uglify in this JSON mode?

I'm trying to minify HTML file that contains JSON data:

<body>
<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [{
    "@type": "ListItem",
    "position": 1,
    "item": {
      "@id": "http://example.org/blog/",
      "name": "Blog"
    }
  }]
}
</script>
...
</body>

I have tried invoking gulp-minify-inline with options like this:

var options = {
  js: {
      expr: true
  },
  jsSelector: 'script[type="application/ld+json"]'
};

gulp.task('minify', function() {
  gulp.src('src/*.html')
    .pipe(minifyInline(options))
    .pipe(gulp.dest('dest/'))
});

But it fails with JS_Parse_Error probably because the expr option is not passed down to Uglify.

I have tried several versions of the options such as expression: true or parser: {expression: true} but never got it right. Is this supported? What is the correct way to write options for JSON minification?

shkuznetsov commented 8 years ago

Hey Vaclav! This plugin uses UglifyJS.minify(), which significantly limits number of options one can pass to the .parse() method: https://github.com/mishoo/UglifyJS2/blob/master/tools/node.js#L70

That's why your parameter gets lost! To sort that out the code invoking UglifyJS2 will have to be re-written, basically I'll need to drop in my own version of .minify() which would support that parameter -- that is neither elegant, nor backward-compatible.

Having said that I feel like it would be quite a convenient feature to have... let me think about it.

shkuznetsov commented 8 years ago

For now, to make it less ambiguous, I'll mention the fact that UglifyJS2.minify() is used in the plugin code in README.

stephengardner commented 6 years ago

I used this and it works great: https://github.com/haensl/gulp-minify-inline-json

shkuznetsov commented 5 years ago

Alright, we're now migrated to Terser and, guess what, it has got exactly the same problem as UgligyJS2! Thinking about it, I can add another minification rail, specifically for JSON, but would it not violate a "do one thing well" principle too much?

VaclavSynacek commented 5 years ago

@shkuznetsov thank you for the effort. However I have long since stopped needing this. So if this stays open just for myself, it can be closed now.