Add automatic uglification of *.js.min files (they will then be served as *.js)
authorNick Downing <nick@ndcode.org>
Sat, 17 Nov 2018 01:36:29 +0000 (12:36 +1100)
committerNick Downing <nick@ndcode.org>
Sat, 17 Nov 2018 01:36:29 +0000 (12:36 +1100)
Server.js
SiteRoot.js
package.json
site/index.html.jst
site/main.js.min [new file with mode: 0644]

index 7b27ae3..f69479c 100644 (file)
--- a/Server.js
+++ b/Server.js
@@ -8,6 +8,7 @@ let jst = require('@ndcode/jst')
 let less = require('less/lib/less-node')
 let path = require('path')
 var stream_buffers = require('stream-buffers')
+let uglify_es = require('uglify-es')
 let url = require('url')
 let util = require('util')
 let yauzl = require('yauzl')
@@ -28,6 +29,7 @@ let Server = function() {
   this.build_cache_email = new BuildCache()
   this.build_cache_json = new BuildCache()
   this.build_cache_less = new BuildCache()
+  this.build_cache_min = new BuildCache()
   this.build_cache_text = new BuildCache()
   this.build_cache_zet = new BuildCache()
   this.build_cache_zip = new BuildCache()
@@ -103,6 +105,124 @@ Server.prototype.get_less = function(pathname, root) {
   )
 }
 
+Server.prototype.get_min = function(pathname) {
+  return /*await*/ this.build_cache_min.get(
+    pathname,
+    async result => {
+      let files = {}
+      files[pathname] = await fs_readFile(pathname, {encoding: 'utf-8'})
+      console.log('getting', pathname, 'as min')
+      let render = uglify_es.minify(
+        files,
+        {
+          //compress: {
+            //arrows        : true,
+            //booleans      : true,
+            //collapse_vars : true,
+            //comparisons   : true,
+            //computed_props: true,
+            //conditionals  : true,
+            //dead_code     : true,
+            //drop_console  : false,
+            //drop_debugger : true,
+            //ecma          : 5,
+            //evaluate      : true,
+            //expression    : false,
+            //global_defs   : {},
+            //hoist_funs    : false,
+            //hoist_props   : true,
+            //hoist_vars    : false,
+            //ie8           : false,
+            //if_return     : true,
+            //inline        : true,
+            //join_vars     : true,
+            //keep_classnames: false,
+            //keep_fargs    : true,
+            //keep_fnames   : false,
+            //keep_infinity : false,
+            //loops         : true,
+            //negate_iife   : true,
+            //passes        : 1,
+            //properties    : true,
+            //pure_getters  : true && "strict",
+            //pure_funcs    : null,
+            //reduce_funcs  : true,
+            //reduce_vars   : true,
+            //sequences     : true,
+            //side_effects  : true,
+            //switches      : true,
+            //top_retain    : null,
+            //toplevel      : !!(options && options["top_retain"]),
+            //typeofs       : true,
+            //unsafe        : false,
+            //unsafe_arrows : false,
+            //unsafe_comps  : false,
+            //unsafe_Function: false,
+            //unsafe_math   : false,
+            //unsafe_methods: false,
+            //unsafe_proto  : false,
+            //unsafe_regexp : false,
+            //unsafe_undefined: false,
+            //unused        : true,
+            //warnings      : false,
+          //},
+          //ecma: undefined,
+          //ie8: false,
+          //keep_classnames: undefined,
+          //keep_fnames: false,
+          //mangle: {},
+          //nameCache: null,
+          //output: {
+            //ascii_only       : false,
+            //beautify         : false,
+            //bracketize       : false,
+            //comments         : false,
+            //ecma             : 5,
+            //ie8              : false,
+            //indent_level     : 4,
+            //indent_start     : 0,
+            //inline_script    : true,
+            //keep_quoted_props: false,
+            //max_line_len     : false,
+            //preamble         : null,
+            //preserve_line    : false,
+            //quote_keys       : false,
+            //quote_style      : 0,
+            //safari10         : false,
+            //semicolons       : true,
+            //shebang          : true,
+            //shorthand        : undefined,
+            //source_map       : null,
+            //webkit           : false,
+            //width            : 80,
+            //wrap_iife        : false
+          //},
+          //parse: {
+            //bare_returns   : false,
+            //ecma           : 8,
+            //expression     : false,
+            //filename       : null,
+            //html5_comments : true,
+            //shebang        : true,
+            //strict         : false,
+            //toplevel       : null
+          //},
+          //rename: undefined,
+          //safari10: false,
+          //sourceMap: false,
+          //timings: false,
+          toplevel: true //false,
+          //warnings: false,
+          //wrap: false
+        }
+      )
+      if (render.error !== undefined)
+        throw render.error
+      result.value = Buffer.from(render.code)
+    }
+  )
+}
+
 Server.prototype.get_text = function(pathname) {
   return /*await*/ this.build_cache_text.get(
     pathname,
index 6e55c8a..0090a1e 100644 (file)
@@ -27,6 +27,9 @@ SiteRoot.prototype.get_jst = function(pathname) {
 SiteRoot.prototype.get_less = function(pathname) {
   return /*await*/ this.server.get_less(this.root + pathname, this.root)
 }
+SiteRoot.prototype.get_min = function(pathname) {
+  return /*await*/ this.server.get_min(this.root + pathname)
+}
 SiteRoot.prototype.get_text = function(pathname) {
   return /*await*/ this.server.get_text(this.root + pathname)
 }
@@ -97,6 +100,23 @@ SiteRoot.prototype.serve_less = async function(env, pathname) {
   return true
 }
 
+SiteRoot.prototype.serve_min = async function(env, pathname) {
+  if (pathname.slice(-7) !== '.js.min')
+    return false
+  let data 
+  try {
+    data = await this.server.get_min(pathname)
+  }
+  catch (err) {
+    if (!(err instanceof Error) || err.code !== 'ENOENT')
+      throw err
+    return false
+  }
+  this.serve(env, 200, data, 'min')
+  return true
+}
+
 SiteRoot.prototype.serve_fs = async function(env, pathname) {
   let data 
   try {
@@ -133,6 +153,8 @@ SiteRoot.prototype.serve_file = async function(env, pathname) {
     return
   if (await this.serve_less(env, pathname + '.less'))
     return
+  if (await this.serve_min(env, pathname + '.min'))
+    return
   if (await this.serve_fs(env, pathname))
     return
   this.die(env, `file not found ${env.pathname}`)
index 88d589d..35e98e4 100644 (file)
@@ -19,6 +19,7 @@
     "querystring": "^0.2.0",
     "socket.io": "^2.1.1",
     "stream-buffers": "^3.0.2",
+    "uglify-es": "^3.3.9",
     "url": "^0.11.0",
     "xdate": "^0.8.2",
     "yauzl": "^2.10.0"
index 3db710a..a9bcebd 100644 (file)
@@ -10,6 +10,7 @@ return async env => {
         body {
           p {'Hello, world'}
         }
+        script(src="main.js") {}
       }
     ),
     'index.html.jst'
diff --git a/site/main.js.min b/site/main.js.min
new file mode 100644 (file)
index 0000000..8038850
--- /dev/null
@@ -0,0 +1,6 @@
+setTimeout(
+  () => {
+    alert('I am an alert box!')
+  },
+  5000
+)