Skip to content

Commit

Permalink
Merge pull request #2 from adierkens/promise-support
Browse files Browse the repository at this point in the history
Pass call context to loader fn
  • Loading branch information
adierkens authored Aug 14, 2017
2 parents 130039d + 133a391 commit 160d515
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 10 additions & 1 deletion src/webpack-inject-plugin.loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 160d515

Please sign in to comment.