A very small ECMAScript parser, tokenizer and minify written in JavaScript.
chiffon.min.js
is 3KB now.
<script src="chiffon.js"></script>
or
<script src="chiffon.min.js"></script>
Object Chiffon will defined in the global scope.
npm install chiffon
var Chiffon = require('chiffon');
bower install chiffon
Tokenize a string code.
- {Array} Chiffon.tokenize ( code [, options ] )
@param {string} code Target code.
@param {Object} [options] Tokenize options.
@return {Array} Return an array of the parsed tokens.
var tokens = Chiffon.tokenize('var a = 1');
console.log(tokens);
/*
[ { type: 'Keyword', value: 'var' },
{ type: 'Identifier', value: 'a' },
{ type: 'Punctuator', value: '=' },
{ type: 'Numeric', value: '1' } ]
*/
JavaScript AST is not currently supported.
Chiffon work simply tokenizing.
- Comment
- LineTerminator
- Template
- String
- Punctuator
- RegularExpression
- Numeric
- UnicodeEscapeSequence
- Identifier
- Null
- Boolean
- Keyword
-
comment : {boolean} default=false
true = Keep comment tokens. -
lineTerminator : {boolean} default=false
true = Keep line terminator tokens.
Concatenate to string from the parsed tokens.
- {string} Chiffon.untokenize ( tokens )
@param {Array} tokens An array of the parsed tokens.
@return {string} Return a concatenated string.
Minify JavaScript code.
- {string} Chiffon.minify ( code )
@param {string} code Target code.
@return {string} Return a minified code.
var min = Chiffon.minify('var a = 1 + 1; // comment');
console.log(min); // var a=1+1;
Minify is a simple implementation as following.
function minify(code) {
return untokenize(tokenize(code, { lineTerminator: true }));
}
MIT