From be625d2b12fd29bafe8f8caa6e5ce475e2831544 Mon Sep 17 00:00:00 2001 From: Lu Nelson Date: Sun, 16 Dec 2018 18:23:35 +0100 Subject: [PATCH 1/2] feat(options): allow `functions` option to be a function Allows the `functions` option for node-sass or dart-sass to be a function, in which case it will be passed the Loader Context as argument --- lib/normalizeOptions.js | 5 +++++ 1 file changed, 5 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') { From fc6c1957a3005236bd37e6e156b81b4513c21ab6 Mon Sep 17 00:00:00 2001 From: Lu Nelson Date: Sun, 16 Dec 2018 18:24:50 +0100 Subject: [PATCH 2/2] test(options): add test for `functions` option as function Adds a test for the case where the functions option is supplied as a function. Passes the Loader Context and also checks that it is available. --- test/index.test.js | 7 +++++++ 1 file changed, 7 insertions(+) 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', () =>