Skip to content

Commit

Permalink
refactor: use environment to get templateLiteral value (#1591)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Apr 12, 2024
1 parent 5c717c9 commit e006f66
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
19 changes: 2 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
stringifyRequest,
warningFactory,
syntaxErrorFactory,
supportTemplateLiteral,
} from "./utils";

export default async function loader(content, map, meta) {
Expand Down Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1454,4 +1478,5 @@ export {
defaultGetLocalIdent,
warningFactory,
syntaxErrorFactory,
supportTemplateLiteral,
};

0 comments on commit e006f66

Please sign in to comment.