From 133a3911c1e9012d2b238c650cfdde3fe47840d0 Mon Sep 17 00:00:00 2001 From: Adam Dierkens Date: Mon, 14 Aug 2017 13:13:49 -0700 Subject: [PATCH] Pass call context to loader fn --- README.md | 4 ++++ src/webpack-inject-plugin.loader.js | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) 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; }