Skip to content

Commit

Permalink
Pass call context to loader fn
Browse files Browse the repository at this point in the history
  • Loading branch information
adierkens committed Aug 14, 2017
1 parent 130039d commit 133a391
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 133a391

Please sign in to comment.