Skip to content

Commit 142ac35

Browse files
refactor: reduce runtime code
1 parent c3faac4 commit 142ac35

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

Diff for: src/plugins/sources-plugin.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ export default (options) =>
139139
let offset = 0;
140140

141141
for (const source of sources) {
142-
const { name, value, isValueQuoted, runtime, startOffset, endOffset } =
143-
source;
142+
const { name, value, isValueQuoted, startOffset, endOffset } = source;
144143

145144
let request = value;
146145

@@ -180,7 +179,6 @@ export default (options) =>
180179
importName,
181180
hash,
182181
isValueQuoted,
183-
runtime,
184182
});
185183
}
186184

Diff for: src/utils.js

+19-28
Original file line numberDiff line numberDiff line change
@@ -1268,37 +1268,28 @@ export function getModuleCode(html, replacements, loaderContext, options) {
12681268
let needHelperImport = false;
12691269

12701270
for (const item of replacements) {
1271-
const { runtime, importName, replacementName, isValueQuoted, hash } = item;
1272-
1273-
if (typeof runtime === "undefined" || runtime === true) {
1274-
const getUrlOptions = []
1275-
.concat(hash ? [`hash: ${JSON.stringify(hash)}`] : [])
1276-
.concat(isValueQuoted ? [] : "maybeNeedQuotes: true");
1277-
const preparedOptions =
1278-
getUrlOptions.length > 0 ? `, { ${getUrlOptions.join(", ")} }` : "";
1279-
1280-
const needHelperFn = getUrlOptions.length > 0;
1281-
1282-
if (needHelperFn) {
1283-
if (!needHelperImport) {
1284-
needHelperImport = true;
1285-
}
1286-
1287-
replacersCode += `var ${replacementName} = ${GET_SOURCE_FROM_IMPORT_NAME}(${importName}${preparedOptions});\n`;
1271+
const { importName, replacementName, isValueQuoted, hash } = item;
1272+
const getUrlOptions = []
1273+
.concat(hash ? [`hash: ${JSON.stringify(hash)}`] : [])
1274+
.concat(isValueQuoted ? [] : "maybeNeedQuotes: true");
1275+
const preparedOptions =
1276+
getUrlOptions.length > 0 ? `, { ${getUrlOptions.join(", ")} }` : "";
1277+
1278+
const needHelperFn = getUrlOptions.length > 0;
1279+
1280+
if (needHelperFn) {
1281+
if (!needHelperImport) {
1282+
needHelperImport = true;
12881283
}
12891284

1290-
const name = needHelperFn ? replacementName : importName;
1291-
1292-
code = code.replace(new RegExp(replacementName, "g"), () =>
1293-
isTemplateLiteralSupported ? `\${${name}}` : `" + ${name} + "`,
1294-
);
1295-
} else {
1296-
code = code.replace(new RegExp(replacementName, "g"), () =>
1297-
isTemplateLiteralSupported
1298-
? `\${${replacementName}}`
1299-
: `" + ${replacementName} + "`,
1300-
);
1285+
replacersCode += `var ${replacementName} = ${GET_SOURCE_FROM_IMPORT_NAME}(${importName}${preparedOptions});\n`;
13011286
}
1287+
1288+
const name = needHelperFn ? replacementName : importName;
1289+
1290+
code = code.replace(new RegExp(replacementName, "g"), () =>
1291+
isTemplateLiteralSupported ? `\${${name}}` : `" + ${name} + "`,
1292+
);
13021293
}
13031294

13041295
// Replaces "<script>" or "</script>" to "<" + "script>" or "<" + "/script>".

0 commit comments

Comments
 (0)