diff --git a/src/index.js b/src/index.js index 78b7cde..acb6153 100644 --- a/src/index.js +++ b/src/index.js @@ -9,7 +9,11 @@ import LessError from "./LessError"; async function lessLoader(source) { const options = this.getOptions(schema); const callback = this.async(); - const lessOptions = getLessOptions(this, options); + const webpackContextSymbol = Symbol("loaderContext"); + const lessOptions = getLessOptions(this, { + ...options, + webpackContextSymbol, + }); const useSourceMap = typeof options.sourceMap === "boolean" ? options.sourceMap : this.sourceMap; @@ -44,9 +48,7 @@ async function lessLoader(source) { return; } - if ("webpackLoaderContext" in less) { - delete less.webpackLoaderContext; - } + delete less[webpackContextSymbol]; const { css, imports } = result; diff --git a/src/utils.js b/src/utils.js index dcd7f40..125b3b0 100644 --- a/src/utils.js +++ b/src/utils.js @@ -182,26 +182,27 @@ function getLessOptions(loaderContext, loaderOptions) { lessOptions.plugins.unshift(createWebpackLessPlugin(loaderContext)); } - const webpackContextDeprecated = util.deprecate( - (context) => context, - "less.webpackLoaderContext is deprecated and will be removed in next major release. Instead use pluginManager.webpackLoaderContext (https://webpack.js.org/loaders/less-loader/#plugins)" - ); - lessOptions.plugins.unshift({ install(lessProcessor, pluginManager) { // eslint-disable-next-line no-param-reassign pluginManager.webpackLoaderContext = loaderContext; - // Todo remove in next major release - if (typeof lessProcessor.webpackLoaderContext === "undefined") { - Object.defineProperty(lessProcessor, "webpackLoaderContext", { - configurable: true, + // Todo remove in next major release `lessProcessor[webpackContextSymbol]` + const { webpackContextSymbol } = options; - get() { - return webpackContextDeprecated(loaderContext); - }, - }); - } + // eslint-disable-next-line no-param-reassign + lessProcessor[webpackContextSymbol] = loaderContext; + + Object.defineProperty(lessProcessor, "webpackLoaderContext", { + configurable: true, + + get() { + util.deprecate(() => {}, + "less.webpackLoaderContext is deprecated and will be removed in next major release. Instead use pluginManager.webpackLoaderContext (https://webpack.js.org/loaders/less-loader/#plugins)")(); + + return lessProcessor[webpackContextSymbol]; + }, + }); }, });