I'm looking for a way to minify the white space in template literals. Since regular js minification wouldn't remove white space on a template literal I was expecting that maybe polymer-cli had a way to minify those.
An example of the result of minification:
import{PolymerElement,html}from'../node_modules/@polymer/polymer/polymer-element.js';export default class MyApp extends PolymerElement{static get is(){return'my-app'}static get template(){return html`
<style>
:host {
display: block;
height: 100vh;
}
.app {
height: 100vh;
}
</style>
<div class="app">
My App
</div>
`}}customElements.define(MyApp.is,MyApp);
polymer-clicurrently doesn't support minification of tagged template strings. Internally, it uses Babel plugins to minify JavaScript, so ideally we'd be able to insert thebabel-plugin-minify-template-stringsplugin into the pipeline when minification is enabled.For now, we could use
babel-clialong with that plugin to process the build output ofpolymer-cli:Start with a Polymer 3 template project, e.g.,
PolymerLabs/start-polymer3.Install
babel-cliand thebabel-plugin-minify-template-stringsplugin. Your project may need other Babel plugins. In this case, this template project needsbabel-plugin-syntax-dynamic-importfor Babel to handle the dynamic imports in the code.Add a
.babelrcconfig file with the following file contents:Add a
buildNPM script topackage.jsonto runbabel-cli(with.babelrcabove) on the JavaScript output ofpolymer build:Run
npm run build(oryarn build). The command output (running withpolymer-cli (1.7.0-pre.13),zsh, macOS High Sierra) should look similar to this:See GitHub repo with the above changes, and its sample output