Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to minify JS script inside HTML #1076

Closed
ynwd opened this issue Sep 12, 2020 · 2 comments
Closed

How to minify JS script inside HTML #1076

ynwd opened this issue Sep 12, 2020 · 2 comments

Comments

@ynwd
Copy link

ynwd commented Sep 12, 2020

for example:

 const minified = minify(html, {
    collapseWhitespace: true,
    conservativeCollapse: true,
    keepClosingSlash: true,
    minifyCSS: true,
    minifyJS: true,
    minifyURLs: true,
    removeScriptTypeAttributes: true,
    removeStyleLinkTypeAttributes:true,
    removeComments: true
  });
<html>
<form action="http://localhost:3000/update" method="post">
  <label for="email">Email</label><br>
  <input type="email" id="email" name="email"><br><br>
  <label for="firstName">First name</label><br>
  <input type="text" id="firstName" name="firstName"><br><br>
  <label for="lastName">Last name</label><br>
  <input type="text" id="lastName" name="lastName"><br><br>
  <label for="birthDate">Birth date</label><br>
  <input type="date" id="birthDate" name="birthDate"><br><br>
  <label for="id">IC number</label><br>
  <input type="text" id="icNumber" name="icNumber"><br><br>
  <button type="submit">Save</button>
</form>
<script>
  fetch('/api/user/{{userId}}')
    .then(response => response.json())
    .then(data => {
      const bod = new Date(data.birthDate).toJSON().slice(0, 10)
      document.getElementById("email").value = data.email;
      document.getElementById("firstName").value = data.firstName
      document.getElementById("lastName").value = data.lastName
      document.getElementById("birthDate").value = bod
      document.getElementById("icNumber").value = data.icNumber
    })
</script>

</html>
@ynwd ynwd changed the title How to minify JS script inis How to minify JS script inside HTML Sep 12, 2020
@DanielRuf
Copy link

DanielRuf commented Sep 13, 2020

It's because uglify-js (used for minifying the JS code) does not support this (ES6).

You might want to try https://github.com/terser/html-minifier-terser

Bildschirmfoto 2020-09-13 um 22 47 25

Bildschirmfoto 2020-09-13 um 22 48 04

@ynwd ynwd closed this as completed Sep 14, 2020
@ynwd
Copy link
Author

ynwd commented Sep 14, 2020

Thank you @DanielRuf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants