Skip to content

Kreozot/callback-loader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

callback-loader

Webpack loader that parse your JS-file, call specified functions and replace their call expressions with the results.

Installation

npm install callback-loader

Usage

Source file:

var a = multBy2(10);
var b = mult(10, 3);
var c = concat("foo", "bar");

Webpack config:

{
    ...
    callbackLoader: {
        multBy2: function(num) {
            return num * 2;
        },
        mult: function(num1, num2) {
            return num1 * num2;
        },
        concat: function(str1, str2) {
            return '"' + str1 + str2 + '"';
        }
    }
}

Result:

var a = 20;
var b = 30;
var c = "foobar";

Notice that quotes was added in concat function.

Loader parameters

You can choose which functions will be processed in your query:

'callback?mult&multBy2!example.js'

Result for this query will be this:

var a = 20;
var b = 30;
var c = concat("foo", "bar");

Different configs

Webpack config:

{
    ...
    callbackLoader: {
        concat: function(str1, str2) {
            return '"' + str1 + str2 + '"';
        }
    },
    anotherConfig: {
        concat: function(str1, str2) {
            return '"' + str1 + str2 + '-version2"';
        }
    }
}

Loader query:

'callback?config=anotherConfig!example.js'

Result for this query will be this:

var a = multBy2(10);
var b = mult(10, 3);
var c = "foobar-version2";

Restrictions

No expressions in function arguments, sorry. Only raw values.

About

Webpack loader that parses your JS, calls specified functions and replaces their with the results.

Resources

Stars

Watchers

Forks

Packages

No packages published