-
-
Notifications
You must be signed in to change notification settings - Fork 210
Add ability to uglify generated code #36
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
const fastSafeStringify = require('fast-safe-stringify') | ||
|
||
var uglify = null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems inconsistent between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
let isLong | ||
try { | ||
isLong = require('long').isLong | ||
|
@@ -74,6 +75,11 @@ function build (schema, options) { | |
; | ||
return ${main} | ||
` | ||
|
||
if (options.uglify) { | ||
code = uglifyCode(code) | ||
} | ||
|
||
if (hasAdditionalPropertiesTrue(schema)) { | ||
return (new Function('fastSafeStringify', code))(fastSafeStringify) | ||
} | ||
|
@@ -457,4 +463,36 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema) { | |
} | ||
} | ||
|
||
function uglifyCode (code) { | ||
if (!uglify) { | ||
loadUglify() | ||
} | ||
|
||
const uglified = uglify.minify(code, { parse: { bare_returns: true } }) | ||
|
||
if (uglified.error) { | ||
throw uglified.error | ||
} | ||
|
||
return uglified.code | ||
} | ||
|
||
function loadUglify () { | ||
try { | ||
uglify = require('uglify-es') | ||
const uglifyVersion = require('uglify-es/package.json').version | ||
|
||
if (uglifyVersion[0] !== '3') { | ||
throw new Error('Only version 3 of uglify-es is supported') | ||
} | ||
} catch (e) { | ||
uglify = null | ||
if (e.code === 'MODULE_NOT_FOUND') { | ||
throw new Error('In order to use uglify, you have to manually install `uglify-es`') | ||
} | ||
|
||
throw e | ||
} | ||
} | ||
|
||
module.exports = build |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,8 @@ | |
"long": "^3.2.0", | ||
"pre-commit": "^1.1.3", | ||
"standard": "^10.0.0", | ||
"tap": "^10.3.0" | ||
"tap": "^10.3.0", | ||
"uglify-es": "^3.0.9" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need the es variant to support |
||
}, | ||
"dependencies": { | ||
"fast-safe-stringify": "^1.1.11" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is worse, but that's #15, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we must definitely fix that issue :S