Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

New example: code minify.

  • Loading branch information...
commit 9f28a445070d0acd33df978e6fcdbece455feb31 1 parent 550b084
Ariya Hidayat authored September 15, 2012
72  demo/minify.html
... ...
@@ -0,0 +1,72 @@
  1
+<!DOCTYPE html>
  2
+<html lang="en">
  3
+<head>
  4
+<meta charset="utf-8">
  5
+<title>Esprima: Code Minify Demo</title>
  6
+<script src="../test/3rdparty/platform.js"></script>
  7
+<script src="../test/3rdparty/escodegen.js"></script>
  8
+<script src="checkenv.js"></script>
  9
+<script src="minify.js"></script>
  10
+<script src="../esprima.js"></script>
  11
+<script src="../assets/codemirror/codemirror.js"></script>
  12
+<script src="../assets/codemirror/javascript.js"></script>
  13
+<link rel="stylesheet" type="text/css" href="../assets/codemirror/codemirror.css"/>
  14
+<link rel="stylesheet" type="text/css" href="../assets/style.css"/>
  15
+<style>
  16
+.CodeMirror-scroll {
  17
+    height: 300px;
  18
+}
  19
+</style>
  20
+</head>
  21
+<body>
  22
+<div class="container">
  23
+
  24
+<div class="topbar">
  25
+<ul class="nav">
  26
+<li><a href="../index.html">&larr; Home</a></li>
  27
+<li><a href="http://github.com/ariya/esprima">Code</a></li>
  28
+<li><a href="../doc/index.html">Documentation</a></li>
  29
+<li><a href="http://issues.esprima.org">Issues</a></li>
  30
+</ul>
  31
+</div>
  32
+
  33
+<h1>Minify <small>compacts everything</small></h1>
  34
+
  35
+<p>Type ECMAScript code in the editor. Press <b>Minify</b> button to shorten the code
  36
+using <a href="https://github.com/Constellation/escodegen">Escodegen project</a>.</p>
  37
+
  38
+<p><textarea id="code" autofocus="autofocus" cols="70" rows="25" spellcheck="false">
  39
+function bubbleSort    (list) {
  40
+var items = list.slice(0), swapped =false,
  41
+        p,   q;
  42
+   for ( p= 1;p &lt;   items.length; ++p) {
  43
+       for (q=0; q &lt; items.length -    p; ++q) {
  44
+        if (items[q + 1  ] &lt; items[q]) {
  45
+            swapped =true;
  46
+        let temp = items[q];
  47
+         items[q] = items[ q+1]; items[q+1] = temp;
  48
+            }
  49
+      }
  50
+        if (!swapped)
  51
+        break;
  52
+    }
  53
+       return items;
  54
+}
  55
+</textarea></p>
  56
+
  57
+<p><input type="button" value="Minify" id="minify"></p>
  58
+
  59
+<p id="codemirror" align="right"><small>The above code editor is based on <a href="http://codemirror.net" target="_blank">CodeMirror</a>.</small></p>
  60
+
  61
+<p id="error"></p>
  62
+
  63
+<p style="margin-top: 50px;" id="version"></p>
  64
+
  65
+<div class="footer"><strong>Esprima</strong> is created by
  66
+<a href="http://ariya.ofilabs.com/about" target="_blank">Ariya Hidayat</a>. Follow <a href="http://twitter.com/ariyahidayat">@ariyahidayat</a> on Twitter.
  67
+</div>
  68
+
  69
+<p id="testbox" style="visibility: hidden;"><textarea id="test"></textarea></p>
  70
+</div>
  71
+</body>
  72
+</html>
122  demo/minify.js
... ...
@@ -0,0 +1,122 @@
  1
+/*
  2
+  Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
  3
+
  4
+  Redistribution and use in source and binary forms, with or without
  5
+  modification, are permitted provided that the following conditions are met:
  6
+
  7
+    * Redistributions of source code must retain the above copyright
  8
+      notice, this list of conditions and the following disclaimer.
  9
+    * Redistributions in binary form must reproduce the above copyright
  10
+      notice, this list of conditions and the following disclaimer in the
  11
+      documentation and/or other materials provided with the distribution.
  12
+
  13
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14
+  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15
+  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16
+  ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  17
+  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18
+  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19
+  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20
+  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21
+  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22
+  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23
+*/
  24
+
  25
+/*jslint browser:true */
  26
+/*global esprima:true, escodegen:true */
  27
+
  28
+function id(i) {
  29
+    'use strict';
  30
+    return document.getElementById(i);
  31
+}
  32
+
  33
+function setText(id, str) {
  34
+    'use strict';
  35
+    var el = document.getElementById(id);
  36
+    if (typeof el.innerText === 'string') {
  37
+        el.innerText = str;
  38
+    } else {
  39
+        el.textContent = str;
  40
+    }
  41
+}
  42
+
  43
+function minify() {
  44
+    'use strict';
  45
+
  46
+    var code, syntax, option, before, after;
  47
+
  48
+    setText('error', '');
  49
+    if (typeof window.editor !== 'undefined') {
  50
+        // Using CodeMirror.
  51
+        code = window.editor.getValue();
  52
+    } else {
  53
+        // Plain textarea, likely in a situation where CodeMirror does not work.
  54
+        code = id('code').value;
  55
+    }
  56
+
  57
+    option = {
  58
+        format: {
  59
+            indent: {
  60
+                style: ''
  61
+            },
  62
+            quotes: 'auto',
  63
+            compact: true
  64
+        }
  65
+    };
  66
+
  67
+    try {
  68
+        before = code.length;
  69
+        syntax = window.esprima.parse(code, { raw: true, range: true });
  70
+        code = window.escodegen.generate(syntax, option);
  71
+        if (typeof window.editor !== 'undefined') {
  72
+            window.editor.setValue(code);
  73
+        } else {
  74
+            id('code').value = code;
  75
+        }
  76
+        after = code.length;
  77
+        if (before > after) {
  78
+            setText('error', 'No error. Minifying ' + before + ' bytes to ' + after + ' bytes.');
  79
+        } else {
  80
+            setText('error', 'Can not minify further, code is already optimized.');
  81
+        }
  82
+    } catch (e) {
  83
+        setText('error', e.toString());
  84
+    } finally {
  85
+    }
  86
+}
  87
+
  88
+/*jslint sloppy:true browser:true */
  89
+/*global minify:true, CodeMirror:true */
  90
+window.onload = function () {
  91
+    var version, el;
  92
+
  93
+    version = 'Using Esprima version ' + esprima.version;
  94
+    version += ' and Escodegen version ' + escodegen.version + '.';
  95
+
  96
+    el = id('version');
  97
+    if (typeof el.innerText === 'string') {
  98
+        el.innerText = version;
  99
+    } else {
  100
+        el.textContent = version;
  101
+    }
  102
+
  103
+    id('minify').onclick = minify;
  104
+
  105
+    try {
  106
+        window.checkEnv();
  107
+
  108
+        // This is just testing, to detect whether CodeMirror would fail or not
  109
+        window.editor = CodeMirror.fromTextArea(id("test"));
  110
+
  111
+        window.editor = CodeMirror.fromTextArea(id("code"), {
  112
+            lineNumbers: true,
  113
+            matchBrackets: true,
  114
+            lineWrapping: true
  115
+        });
  116
+    } catch (e) {
  117
+        // CodeMirror failed to initialize, possible in e.g. old IE.
  118
+        id('codemirror').innerHTML = '';
  119
+    } finally {
  120
+        id('testbox').parentNode.removeChild(id('testbox'));
  121
+    }
  122
+};
1  index.html
@@ -84,6 +84,7 @@
84 84
 <h3>Source transformation</h3>
85 85
 <ul>
86 86
     <li><a href="demo/rewrite.html">Source rewrite</a></li>
  87
+    <li><a href="demo/minify.html">Minify</a></li>
87 88
 </ul>
88 89
 <h3>Harness tests</h3>
89 90
 <ul>

0 notes on commit 9f28a44

Please sign in to comment.
Something went wrong with that request. Please try again.