Skip to content

Commit e006f66

Browse files
refactor: use environment to get templateLiteral value (#1591)
1 parent 5c717c9 commit e006f66

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/index.js

+2-17
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
stringifyRequest,
2828
warningFactory,
2929
syntaxErrorFactory,
30+
supportTemplateLiteral,
3031
} from "./utils";
3132

3233
export default async function loader(content, map, meta) {
@@ -229,23 +230,7 @@ export default async function loader(content, map, meta) {
229230
}
230231
}
231232

232-
let isTemplateLiteralSupported = false;
233-
234-
if (
235-
// eslint-disable-next-line no-underscore-dangle
236-
this._compilation &&
237-
// eslint-disable-next-line no-underscore-dangle
238-
this._compilation.options &&
239-
// eslint-disable-next-line no-underscore-dangle
240-
this._compilation.options.output &&
241-
// eslint-disable-next-line no-underscore-dangle
242-
this._compilation.options.output.environment &&
243-
// eslint-disable-next-line no-underscore-dangle
244-
this._compilation.options.output.environment.templateLiteral
245-
) {
246-
isTemplateLiteralSupported = true;
247-
}
248-
233+
const isTemplateLiteralSupported = supportTemplateLiteral(this);
249234
const importCode = getImportCode(imports, options);
250235

251236
let moduleCode;

src/utils.js

+25
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,30 @@ function syntaxErrorFactory(error) {
14271427
return obj;
14281428
}
14291429

1430+
function supportTemplateLiteral(loaderContext) {
1431+
if (loaderContext.environment && loaderContext.environment.templateLiteral) {
1432+
return true;
1433+
}
1434+
1435+
// TODO remove in the next major release
1436+
if (
1437+
// eslint-disable-next-line no-underscore-dangle
1438+
loaderContext._compilation &&
1439+
// eslint-disable-next-line no-underscore-dangle
1440+
loaderContext._compilation.options &&
1441+
// eslint-disable-next-line no-underscore-dangle
1442+
loaderContext._compilation.options.output &&
1443+
// eslint-disable-next-line no-underscore-dangle
1444+
loaderContext._compilation.options.output.environment &&
1445+
// eslint-disable-next-line no-underscore-dangle
1446+
loaderContext._compilation.options.output.environment.templateLiteral
1447+
) {
1448+
return true;
1449+
}
1450+
1451+
return false;
1452+
}
1453+
14301454
export {
14311455
normalizeOptions,
14321456
shouldUseModulesPlugins,
@@ -1454,4 +1478,5 @@ export {
14541478
defaultGetLocalIdent,
14551479
warningFactory,
14561480
syntaxErrorFactory,
1481+
supportTemplateLiteral,
14571482
};

0 commit comments

Comments
 (0)