0.2.8 • Published 9 years ago
st8less v0.2.8
What is this
St8less
allows to extract functions by specified criteria from your source files. These functions could be placed to separate file and will be addressed via some globally accessed object.
It could be useful for hot-swap of those functions, for example - LiveReload.
How it looks like
File1:
var Page1 = {
controller: function () {
this.count = 5;
},
view: function (c) {
'__stateless';
return m('div', 'count is ' + c.count);
}
};
File2:
function myRand() {
'__stateless';
return Math.random();
};
After applying extractor with default settings we get the following:
File1:
var Page1 = {
controller: function () {
this.count = 5;
},
view: function (c) {
return MyGlobal.abc.view0.apply(this, [c, m]);
}
};
File2:
function myRand() {
return MyGlobal.abc.myRand1.apply(this, [Math]);
}
Extracted:
MyGlobal.abc = {};
MyGlobal.abc.view0 = function (c, m){
'__stateless';
return m('div', 'count is ' + c.count);
}
MyGlobal.abc.myRand1 = function (Math){
'__stateless';
return Math.random();
}
How to use
var ex = new Extractor();
ex.parse(fs.readFileSync('file1.js'), function (err, changed) {
fs.writeFileSync('file1_changed.js', changed);
ex.parse(fs.readFileSync('file2.js'), {prefix: "functions_from_file2_"}, function (err, changed) {
fs.writeFileSync('file2_changed.js', changed);
ex.done(function (err, extracted) {
fs.writeFileSync('extracted.js', extracted);
});
});
});
Extractor options:
criteria
- which functions shall be extracted. Is a function which acceptsfunctionDefinition
parameter, which is an object with following properties:name
- function name (or property/variable name that holds the function)attribute
- attribute defined as string expression at begining of function, like__stateless
in example aboveparamNames
- argument namesexternalVariables
- variable/fn names used in function but not declared inside By default extractor looks for functions with__stateless
attribute
objectName
- object ot store extracted functions. Default isSt8less
globalName
- global object to use in calls. Default iswindow
Usage
Used by mithril reload plugin