webpack-inject-plugin icon indicating copy to clipboard operation
webpack-inject-plugin copied to clipboard

async injection

Open stefanovualto opened this issue 6 years ago • 1 comments

Can create an example for the async case?

You can either return the raw content to load, or a Promise which resolves to the content, if you wish to be async.

Because from my understanding the loader needs always to return a string and not a Promise.

My use case is that I would like to inject a third party library that has no npm package but available through a cdn and which is required by my library. I have at the moment a homemade plugin, which injects the text inside the bundle, but I would like to find a cleaner alternative...

Thank you in advance

stefanovualto avatar Oct 21 '19 16:10 stefanovualto

Loaders can either be sync or async: https://webpack.js.org/api/loaders/#asynchronous-loaders.

In the case where the function passed to webpack-inject-plugin returns a Promise we simply call this.async() on the loader and wire up the callback for you. https://github.com/adierkens/webpack-inject-plugin/blob/master/src/webpack-inject-plugin.loader.ts#L16

I'll add in an example of the async case and a test to go with it.

adierkens avatar Oct 21 '19 17:10 adierkens