Search code examples
javascriptcommentsdust.js

Get dust.js to strip JavaScript comments


From the github readme:

Take special care if you have a JavaScript code block and have comments of the form // message. When all the newlines are removed, this will comment out the following statement. Use the /* message */ form instead.

Ugh. I understand syntactically why the removal of newlines disallows // style comments.

Is there a way to tell dust to just remove all comments?

If not, then external is probably how I'll roll (and would for production anyway - it's just annoying while doing early development work to remember to use c-style comments).


Solution

  • Updated answer: as of Dust 2.5.0 simply set

    dust.config.whitespace = true;
    

    Dust is just a string parser-- it doesn't grok the file that it's processing lexically.

    You mentioned early development work, so a better option is probably to tell Dust to not strip whitespace, at least while you're developing.

    After Dust has been loaded, simply override the behavior of the format optimizer:

    dust.optimizers.format = function(ctx, node) { return node };
    

    And Dust will stop stripping whitespace. In production, you probably want to use uglify or similar to remove comments anyways.

    We are working on a better way to put this behind a flag in a future Dust version.