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

Online terser minifier? #106

Closed
xem opened this issue Sep 12, 2018 · 5 comments
Closed

Online terser minifier? #106

xem opened this issue Sep 12, 2018 · 5 comments

Comments

@xem
Copy link

xem commented Sep 12, 2018

Hello,
I'd like to make an online Terser minifier, with just HTML + JS, similar to those that existed for uglifyjs/es, but I'm not sure where to start.
Can the JS files of your project be used in the browser, without using node/npm?
Would loading all the JS files in the lib folder be enough to replace "require('terser')" ?
Thanks!

@kzc
Copy link
Contributor

kzc commented Sep 12, 2018

Each Terser release creates a browser bundle in the terser/dist/ directory that can be pulled into a script tag. Or you can clone the repo and run npm run prepare to create the bundle yourself. Alternatively, you can grab the bundle from a CDN.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script src="https://unpkg.com/terser@3.10.11/dist/bundle.js"></script>
<script>
  function Minify() {
    var code = document.getElementById("code").value;
    var options = {}, output = "";
    try {
      options = eval("(" + document.getElementById("options").value + ")");
    } catch (ex) {
      output = "// invalid terser minify options - using defaults\n";
    }
    var result = Terser.minify(code, options);
    var out = document.getElementById("out");
    if (result.error) {
      output += JSON.stringify(result.error, null, 2);
    } else {
      output += result.code;
    }
    out.value = output;
  }
</script>
</head>
<body>
<textarea id="options" rows="14" cols="80" onchange="Minify()"
  onkeypress="this.onchange()" onpaste="this.onchange()" oninput="this.onchange()">
// terser minify options
{
  toplevel: true,
  compress: {
    passes: 3,
    //pure_getters: true,
    //unsafe: true,
  },
  mangle: true,
  output: {
    //beautify: true,
  },
}
</textarea>
<br>
<textarea id="code" rows="15" cols="80" onchange="Minify()"
  onkeypress="this.onchange()" onpaste="this.onchange()" oninput="this.onchange()">
// ECMAScript input

function app(x, y) {
    const message = (text) => {
        console.log(text);
    }
    message(`Hello ES${x + y}`);
}
let five = 5, one = 1;
app(five, one);
</textarea>
<br>
<input type="button" id="ok" value="minify" onclick="Minify()" />
<br>
<textarea id="out" rows="15" cols="80"></textarea>
</body>
<script>Minify()</script>
</html>

This information should probably go in the README. Pull request welcome.

@xem
Copy link
Author

xem commented Sep 12, 2018

Thanks a lot!
In the meantime I managed to make a bundle.js file using browserify but your solution is even simpler.
Still sorting out what options I'm supposed to use to have the best possible ES6 compression (is "{toplevel:true, compress: {passes: 3}}" enough? do I have to add another option to enhance compression?), then I'll suggest an edit in the readme indeed!
Cheers

@kzc
Copy link
Contributor

kzc commented Sep 12, 2018

If you want to use Terser.minify in the browser with sourceMap support you'll have to use a bundler like browserify or webpack in order to get the require("source-map") references in dist/browser.bundle.js resolved.

If you want to explore more aggressive optimization options see unsafe and pure_getters - mind you, they will not necessarily work with all code. Consult the README. Every option is well documented.

@xem
Copy link
Author

xem commented Sep 12, 2018

Thanks! I'll make all these options commented and editable in a separate textarea, like uglifyjs-online did :)
image

@xem
Copy link
Author

xem commented Sep 12, 2018

Here's a first demo: https://xem.github.io/terser-online
Thanks for your help!

@xem xem closed this as completed Sep 12, 2018
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