Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack DefinePlugin from async function

I'd like to define some constants that are returned from an asynchronous resource. Is there anyway to do this in Webpack?

/* webpack.config.js */
module.exports = {
    ...,
    plugins: [
        new webpack.DefinePlugin({
            someVar: /* RETURN VARIABLE FROM ASYNC FUNCTION */
        })
    ]
}
like image 203
Bill Johnston Avatar asked Mar 17 '16 16:03

Bill Johnston


Video Answer


1 Answers

A good way to do this is to return a Promise from webpack configuration.

webpack.config.js

...

module.exports = () => {
  return getYourAsyncResource().then((someVar) => {
    // Resolve as webpack configuration
    return {
      ...
      plugins: [
        new webpack.DefinePlugin({ someVar })
      ]
    };
  }) 
};
like image 102
Juho Vepsäläinen Avatar answered Oct 22 '22 04:10

Juho Vepsäläinen