handlebars-lang / handlebars.js

Minimal templating on steroids.
http://handlebarsjs.com
MIT License
17.85k stars 2.04k forks source link

Handlebars cli fails to minimize output if input is provided by stdin #1525

Open ljani opened 5 years ago

ljani commented 5 years ago

If I try to minify output provided from stdin, uglify-js seems to fail to minimize it and there are no errors or warnings. Here's a test case:

handlebars/bin> echo test | node .\handlebars -m -i-
undefined

Without -m it works:

handlebars/bin> echo test | node .\handlebars -i-
{"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) {
    return "test\r\n";
},"useData":true}

uglify-js seems to return this error if I log output in precompiler.js:

handlebars/bin> echo test | node .\handlebars -m -i-
{ error:
   { JS_Parse_Error [SyntaxError]: Unexpected token: punc «:», expected: punc «;»
       at JS_Parse_Error.get (eval at <anonymous> (C:\...\node_modules\handlebars\node_modules\uglify-js\tools\node.js:20:1), <anonymous>:71:23)
       at formatError (internal/util/inspect.js:810:19)
       at formatRaw (internal/util/inspect.js:646:14)
       at formatValue (internal/util/inspect.js:529:10)
       at formatProperty (internal/util/inspect.js:1266:11)
       at formatRaw (internal/util/inspect.js:744:9)
       at formatValue (internal/util/inspect.js:529:10)
       at inspect (internal/util/inspect.js:187:10)
       at formatWithOptions (internal/util/inspect.js:1408:12)
       at Console.(anonymous function) (internal/console/constructor.js:277:10)
     message: 'Unexpected token: punc «:», expected: punc «;»',
     filename: '0',
     line: 1,
     col: 11,
     pos: 11 } }

I'm on Windows 10 build 17763.437 with PowerShell Core 6.2.0, handlebars@4.1.2, and NodeJS v11.14.0.

Also, is stdin only handled by -i or should --string also support passing -, since the latter does not seem to work?

Thanks for your work!

papasmile commented 4 years ago

Hi, same for me. Just looking into code, it seems like minifying is not supported for standard input? Using -i- per above triggers 'simple mode':

https://github.com/wycats/handlebars.js/blob/fd3ca85d136d9f7f11faeda2fbcb3fd56f05cf42/lib/precompiler.js#L175

Which skips adding surrounding function(), resulting in invalid JavaScript like what you see in output above (instead it returns a regular object which I guess uglify is not expecting to see).

E.g. running node ./handlebars cheese.txt (cheese.txt contains same text, test) results in non-simple mode and parseable JavaScript:

(function() {
  var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
templates['cheese.txt'] = template({"compiler":[8,">= 4.3.0"],"main":function(container,depth0,helpers,partials,data) {
    return "test\n";
},"useData":true});
})();

But is that a bug or a feature (request)?