st8less

Allows to extract some functions from javascript sources by provided criteria and put them in separate js - to simplify live reload

Usage no npm install needed!

<script type="module">
  import st8less from 'https://cdn.skypack.dev/st8less';
</script>

README

Coverage Status Code Climate

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 accepts functionDefinition 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 above
    • paramNames - argument names
    • externalVariables - 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 is St8less
  • globalName - global object to use in calls. Default is window

Usage

Used by mithril reload plugin