-
Notifications
You must be signed in to change notification settings - Fork 9
Examples
//#set _VAR2 1
//#if _VAR1
console.log('var1')
//#elif _VAR2
console.log('var2')
//#else
//#error Please set _VAR1 or _VAR2
//#endif
//#set _FOO 'foo'
let bar = '$_FOO'.toUpperCase();
let baz = { $_FOO: 'bar' };
console.log('$_FOO');
console.log(baz['$_FOO']);
console.log(baz.$_FOO);
Result:
let bar = 'foo'.toUpperCase(); // bar = 'FOO'
let baz = { foo: 'bar' }; // baz = { foo: 'bar' }
console.log('foo'); // outputs 'foo'
console.log(baz['foo']); // outputs 'bar'
console.log(baz.foo); // outputs 'bar'
//#set _OBJ { prop: 1 }
console.log($_OBJ.prop);
console.log($_OBJ);
console.log($_OBJ.foo);
Result:
console.log(1); // outputs 1
console.log({"prop":1}); // outputs "[Object object]"?
console.log({"prop":1}.foo); // outputs undefined
//#set _NUM 10
//#set _STR 'foo'
//#if 0
const $_NUM = 10
const $_STR = 'foo'
//#endif
let n = $_NUM
let s = '$_STR'
Result:
let n = 10
let s = 'foo'
Assume you have a global file src/_types.js
:
// src/_types.js
export default {
TAG: 1,
TEXT: 3
COMMENT: 8
};
...this fragment is in your yat-amazing-parser:
//#set _DATE = new Date().toISOString().slice(0, 10)
/*
yat-amazing-parser v$_VERSION
File: $_FILE
Date: $_DATE
*/
//#if !_T
// fallback to import _types.js
import $_T from './_types';
//#endif
// ...amazing code...
function parse () {
switch (this.type) {
case $_T.TAG: this.parseTag();
break;
case $_T.TEXT: this.parseText();
break;
case $_T.COMMENT: this.parseComment();
break;
}
}
...and this is your rollup config:
// rollup.config.js
import jscc from 'rollup-plugin-jscc';
import types from 'src/_types';
export default {
entry: 'src/parser.js',
plugins: [
jscc({ values: { _T: types } })
]
};
then you make the hard part...
rollup -c -o dist/parser.js
Now, as a result of your efforts you've won this clean, beautiful and performant code:
/*
yat-amazing-parser v1.0
File: src/parser.js
Date: 2016-09-23
*/
// ...amazing code...
function parse () {
switch (this.type) {
case 1: this.parseTag();
break;
case 3: this.parseText();
break;
case 8: this.parseComment();
break;
}
}
* _VERSION
is a predefined varname, new in jscc v0.2.1
ES6 imports can't be declared conditionally, but with JSCC it is possible!
//#if process.env.BROWSER
import mylib from 'browser-lib';
//#else
import mylib from 'node-lib';
//#endif
mylib.doSomething();
...Ops! this code generate issues with linters and syntax highlighters, but using JS multiline comments you can rewrite your code like this:
/*#if process.env.BROWSER
import mylib from 'browser-lib';
//#else */
import mylib from 'node-lib';
//#endif
mylib.doSomething();
Result:
import mylib from 'browser-lib';
mylib.doSomething();
...if "BROWSER" is defined in the node.js environment... oh yes, process.env
is accesible to the expression.
BTW note: the line '//#else */'
don't requires other '//'
before '*/'
because any text after #else
and #endif
is discarded.
This is a nice trick:
//#set _DEBUG 0
/*#if _DEBUG
//#set _DEBUGOUT 'debugout'
function debugout (s) {
console.log('DEBUG: %s', s);
}
//#elif 0 // */
/* global $_DEBUGOUT */
//#else
//#set _DEBUGOUT '//'
//#endif
$_DEBUGOUT('Ops!');
Result:
//('Ops!');
:)