yash
yash

Reputation: 85

How to call a function in build time through webpack?

I have a function which will perform some performance heavy task. The function will store the result in a global variable and in next call, when the result will already exists, I will do other cheap operation.check this link

At this point I want to call this function once while building and storing the result in a global variable, so all calls are cheap due to caching.

reduceFn({
    param1:"good",
    param2:"something"
});

let globalVariable = [];

function reduceFn({param1, param2}) {
    if (param1 in globalVariable) {
        //cheap operation
    } else {
        // some performace heavy task and
        globalVariable.push(param1);
    }
}

Upvotes: 1

Views: 383

Answers (1)

Stav Alfi
Stav Alfi

Reputation: 13953

You can use DefinePlugin which is a webpack plugin to create a global variable which will be accessible from your code:

new webpack.DefinePlugin({ YOUR_GLOBAL_VARAIBLE_NAME: yourFunction() })

Upvotes: 1

Related Questions