diff --git a/README.md b/README.md index d847bb8..3f94ea1 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,10 @@ module.exports = { This webpack plugin accepts a single argument, a function to which returns the code to inject into the bundle. +The function is called using the same context as the loader, so everything [here](https://webpack.js.org/api/loaders/#the-loader-context) applies. + +You can either return the raw content to load, or a `Promise` which resolves to the content, if you wish to be async. + Though this could be used as a standalone plugin, you could also use it to create other webpack plugins, such as injecting code into the build based on a config file. Example: diff --git a/src/webpack-inject-plugin.loader.js b/src/webpack-inject-plugin.loader.js index 402f687..4bf4cea 100644 --- a/src/webpack-inject-plugin.loader.js +++ b/src/webpack-inject-plugin.loader.js @@ -11,5 +11,14 @@ export default function (source) { func = registry[options.id]; } - return func(source); + const rtn = func.call(this, source); + + if (rtn instanceof Promise) { + const callback = this.async(); + rtn.then((result, err) => { + callback(err, result); + }); + } + + return rtn; }