Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use environment to get templateLiteral value #1591

Merged
merged 1 commit into from Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 2 additions & 17 deletions src/index.js
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
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,
};