Allow option of either astring (normal) or UglifyJS (minified) output
authorNick Downing <nick@ndcode.org>
Wed, 12 Jan 2022 07:16:10 +0000 (18:16 +1100)
committerNick Downing <nick@ndcode.org>
Wed, 12 Jan 2022 07:17:58 +0000 (18:17 +1100)
cli.js
jst.js

diff --git a/cli.js b/cli.js
index 47ea01f..2111408 100755 (executable)
--- a/cli.js
+++ b/cli.js
@@ -8,8 +8,11 @@ commander
   .version('0.1.0', '-v, --version')
   .option('-s, --signature <str>', 'function signature for --wrap', 'async (_require, _pathname, _root)')
   .option('-b, --bare-returns', 'allow return in top level code', false)
-  .option('-i, --indent <n>', 'indent per level', '2')
-  .option('-j, --initial-indent <n>', 'initial indent', '0')
+  .option('-o, --output <astring|uglify>', 'use astring or UglifyJS for output', 'astring')
+  .option('-i, --indent <n>', 'indent per level (astring only)', '2')
+  .option('-j, --initial-indent <n>', 'initial indent (astring only)', '0')
+  .option('-c, --compress', 'compress the logic (UglifyJS only)', true)
+  .option('-m, --mangle', 'mangle symbol names (UglifyJS only)', true)
   .option('-w, --wrap', 'wrap as jst_server CommonJS module', false)
   .parse(process.argv)
 
@@ -22,8 +25,15 @@ fs.writeSync(
     text,
     {
       bare_returns: commander.bareReturns,
-      indent: parseInt(commander.indent),
-      initial_indent: parseInt(commander.initialIndent)
+      output: commander.output,
+      astring_options: {
+        indent: ' '.repeat(parseInt(commander.indent)),
+        startingIndentLevel: parseInt(commander.initialIndent)
+      },
+      uglify_options: {
+        compress: commander.compress,
+        mangle: commander.mangle
+      }
     }
   ),
   {encoding: 'utf-8'}
diff --git a/jst.js b/jst.js
index 5fbf41b..4ef8997 100644 (file)
--- a/jst.js
+++ b/jst.js
@@ -25,32 +25,36 @@ let acorn = require('./dist/acorn')
 let astring = require('astring')
 let transform = require('./transform')
 let visitors = require('./visitors')
+let uglify_js = require('uglify-js')
 
 let jst = (text, options) => {
   options = Object.assign(
     {
       bare_returns: true,
       ecma_version: 'latest', // or 11, 12, 13, etc
-      indent: 2,
-      initial_indent: 0
+      output: 'astring',
+      astring_options: {indent: '  ', startingIndentLevel: 0},
+      uglify_options: {compress: true, mangle: true}
     },
     options || {}
   )
-  return astring.generate(
-    transform.transform(
-      visitors,
-      acorn.parse(
-        text,
-        {
-          allowReturnOutsideFunction: options.bare_returns,
-          ecmaVersion: options.ecma_version
-        }
-      )
-    ),
-    {
-      indent: ' '.repeat(options.indent),
-      startingIndentLevel: options.initial_indent
-    }
+  let ast = transform.transform(
+    visitors,
+    acorn.parse(
+      text,
+      {
+        allowReturnOutsideFunction: options.bare_returns,
+        ecmaVersion: options.ecma_version
+      }
+    )
+  )
+  return (
+    options.output === 'astring' ?
+      astring.generate(ast, options.astring_options) :
+      uglify_js.minify(
+        uglify_js.AST_Node.from_mozilla_ast(ast),
+        options.uglify_options
+      ).code
   )
 }