From f4ac3dbce414a9a4f155433f61bf53a17aa71860 Mon Sep 17 00:00:00 2001 From: Lu Nelson Date: Sun, 16 Dec 2018 18:06:50 +0100 Subject: [PATCH] allow functions option to be a function of loaderContext; add test --- lib/normalizeOptions.js | 5 +++++ test/index.test.js | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/lib/normalizeOptions.js b/lib/normalizeOptions.js index 3dde5253..1e59a6d2 100644 --- a/lib/normalizeOptions.js +++ b/lib/normalizeOptions.js @@ -23,6 +23,11 @@ function normalizeOptions(loaderContext, content, webpackImporter) { const options = cloneDeep(utils.getOptions(loaderContext)) || {}; const { resourcePath } = loaderContext; + // allow opt.functions to be configured WRT loaderContext + if (typeof options.functions === 'function') { + options.functions = options.functions(loaderContext); + } + let { data } = options; if (typeof options.data === 'function') { diff --git a/test/index.test.js b/test/index.test.js index 58d20dea..fcdb51b6 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -190,6 +190,13 @@ implementations.forEach((implementation) => { execTest('custom-functions', { functions: customFunctions(implementation), })); + it('should expose custom functions if the option is a function', () => + execTest('custom-functions', { + functions: (loaderContext) => { + should.exist(loaderContext); + return customFunctions(implementation); + }, + })); }); describe('prepending data', () => { it('should extend the data option if present and it is string', () =>