0

Some pieces of HTML are injected in the DOM by one of our js library compiled by Closure Compiler.

We use template literals/strings to dynamically change the HTML and also to ease maintenance by keeping indented HTML in our js files.

Unfortunately, it means that our minified file contains a lot of useless '\n' and spaces in the strings corresponding to these template literals (no surprise, this is as per ES6)

Is there a way to not have all the useless \n and spaces in the minified file?

Example of source code:

this.root.append(`
  <div>
    <a href="#">
      <span>${someText}</span>
    </a>
    <div class="dialog"></div>
  </div>`);

The corresponding compiled code looks like:

...;this.h.append("\n  <div>\n    <a href="#">\n      <span>"+z+"</span>\n    </a>\n    <div class="dialog"></div>\n  </div>");...
J.P. Duvet
  • 630
  • 6
  • 16
  • [ES6 Template Literals - remove \n from the string](https://stackoverflow.com/questions/40142512) and [es6 multiline template strings with no new lines and allow indents](https://stackoverflow.com/questions/40672651) – adiga Nov 24 '20 at 10:11
  • @adiga: I haven't found an answer in these 2 links. A 'replace' function associated to each template literals removes useless char at execution time, not in the minified js file – J.P. Duvet Nov 25 '20 at 02:56
  • have you considered using https://github.com/google/closure-templates – Tiago Coelho Mar 04 '21 at 11:34

1 Answers1

1

I think you would be missing some config which tells complier not to include spaces and new line.

I found the below config which you can try while compiling:

compilation_level: 'ADVANCED'

Vishal Bartakke
  • 294
  • 1
  • 9
  • We use "--compilation_level ADVANCED": it affects the js code obfuscation level but unfortunately not the spaces and new line in template literals. – J.P. Duvet Nov 25 '20 at 02:59
  • 1
    the spaces are inside a string, that's why they cannot be removed.... they are actual data, not code in itself – Tiago Coelho Mar 04 '21 at 11:18