From e006f66ebf1bed92f643c01af8e542e7519c4611 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Fri, 12 Apr 2024 17:00:11 +0300 Subject: [PATCH] refactor: use `environment` to get `templateLiteral` value (#1591) --- src/index.js | 19 ++----------------- src/utils.js | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/index.js b/src/index.js index c1137b12..3c89c371 100644 --- a/src/index.js +++ b/src/index.js @@ -27,6 +27,7 @@ import { stringifyRequest, warningFactory, syntaxErrorFactory, + supportTemplateLiteral, } from "./utils"; export default async function loader(content, map, meta) { @@ -229,23 +230,7 @@ export default async function loader(content, map, meta) { } } - let isTemplateLiteralSupported = false; - - if ( - // eslint-disable-next-line no-underscore-dangle - this._compilation && - // eslint-disable-next-line no-underscore-dangle - this._compilation.options && - // eslint-disable-next-line no-underscore-dangle - this._compilation.options.output && - // eslint-disable-next-line no-underscore-dangle - this._compilation.options.output.environment && - // eslint-disable-next-line no-underscore-dangle - this._compilation.options.output.environment.templateLiteral - ) { - isTemplateLiteralSupported = true; - } - + const isTemplateLiteralSupported = supportTemplateLiteral(this); const importCode = getImportCode(imports, options); let moduleCode; diff --git a/src/utils.js b/src/utils.js index 0ccc6dbd..5d3ae360 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1427,6 +1427,30 @@ function syntaxErrorFactory(error) { return obj; } +function supportTemplateLiteral(loaderContext) { + if (loaderContext.environment && loaderContext.environment.templateLiteral) { + return true; + } + + // TODO remove in the next major release + if ( + // eslint-disable-next-line no-underscore-dangle + loaderContext._compilation && + // eslint-disable-next-line no-underscore-dangle + loaderContext._compilation.options && + // eslint-disable-next-line no-underscore-dangle + loaderContext._compilation.options.output && + // eslint-disable-next-line no-underscore-dangle + loaderContext._compilation.options.output.environment && + // eslint-disable-next-line no-underscore-dangle + loaderContext._compilation.options.output.environment.templateLiteral + ) { + return true; + } + + return false; +} + export { normalizeOptions, shouldUseModulesPlugins, @@ -1454,4 +1478,5 @@ export { defaultGetLocalIdent, warningFactory, syntaxErrorFactory, + supportTemplateLiteral, };