.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)
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'}
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
)
}