Skip to content

Commit 61f9a69

Browse files
feat: reduce runtime code
1 parent a2463ca commit 61f9a69

12 files changed

+3540
-4606
lines changed

src/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ export default async function loader(content) {
8080
html = await options.postprocessor(html, this);
8181
}
8282

83-
const importCode = getImportCode(html, this, imports, options);
84-
const moduleCode = getModuleCode(html, replacements, {
83+
const importCode = getImportCode(html, imports, options);
84+
const moduleCode = getModuleCode(html, replacements, this, {
85+
esModule: options.esModule,
8586
isTemplateLiteralSupported,
8687
});
8788
const exportCode = getExportCode(html, options);

src/plugins/sources-plugin.js

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

141141
for (const source of sources) {
142-
const {
143-
name,
144-
value,
145-
isValueQuoted,
146-
format,
147-
runtime,
148-
startOffset,
149-
endOffset,
150-
} = source;
142+
const { name, value, isValueQuoted, startOffset, endOffset } = source;
151143

152144
let request = value;
153145

@@ -172,7 +164,7 @@ export default (options) =>
172164
importName = `___HTML_LOADER_IMPORT_${imports.size}___`;
173165
imports.set(request, importName);
174166

175-
options.imports.push({ format, importName, request });
167+
options.imports.push({ importName, request });
176168
}
177169

178170
const replacementKey = JSON.stringify({ request, isValueQuoted, hash });
@@ -187,7 +179,6 @@ export default (options) =>
187179
importName,
188180
hash,
189181
isValueQuoted,
190-
runtime,
191182
});
192183
}
193184

src/runtime/getUrl.js

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
1-
module.exports = (url, options) => {
2-
if (!options) {
3-
// eslint-disable-next-line no-param-reassign
4-
options = {};
5-
}
6-
1+
module.exports = (url, maybeNeedQuotes) => {
72
if (!url) {
83
return url;
94
}
105

116
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
12-
url = String(url.__esModule ? url.default : url);
13-
14-
if (options.hash) {
15-
// eslint-disable-next-line no-param-reassign
16-
url += options.hash;
17-
}
7+
url = String(url);
188

19-
if (options.maybeNeedQuotes && /[\t\n\f\r "'=<>`]/.test(url)) {
9+
if (maybeNeedQuotes && /[\t\n\f\r "'=<>`]/.test(url)) {
2010
return `"${url}"`;
2111
}
2212

src/utils.js

+42-53
Original file line numberDiff line numberDiff line change
@@ -1218,43 +1218,21 @@ export function getFilter(filter) {
12181218
};
12191219
}
12201220

1221-
const GET_SOURCE_FROM_IMPORT_NAME = "___HTML_LOADER_GET_SOURCE_FROM_IMPORT___";
1222-
1223-
export function getImportCode(html, loaderContext, imports, options) {
1221+
export function getImportCode(html, imports, options) {
12241222
if (imports.length === 0) {
12251223
return "";
12261224
}
12271225

1228-
// TODO simplify in the next major release
1229-
const getURLRuntime = require.resolve("./runtime/getUrl.js");
1230-
const context = loaderContext.context || loaderContext.rootContext;
1231-
const fileURLToHelper =
1232-
typeof loaderContext.utils !== "undefined" &&
1233-
typeof loaderContext.utils.contextify === "function"
1234-
? loaderContext.utils.contextify(context, getURLRuntime)
1235-
: contextify(context, getURLRuntime);
1236-
1237-
let code = options.esModule
1238-
? `import ${GET_SOURCE_FROM_IMPORT_NAME} from "${fileURLToHelper}";\n`
1239-
: `var ${GET_SOURCE_FROM_IMPORT_NAME} = require("${fileURLToHelper}");\n`;
1226+
let code = "";
12401227

12411228
for (const item of imports) {
1242-
const { format, importName, request } = item;
1229+
const { importName, request } = item;
12431230

1244-
switch (format) {
1245-
case "import":
1246-
code += options.esModule
1247-
? `import ${importName} from ${JSON.stringify(request)};\n`
1248-
: `var ${importName} = require(${JSON.stringify(request)});\n`;
1249-
break;
1250-
case "url":
1251-
default:
1252-
code += options.esModule
1253-
? `var ${importName} = new URL(${JSON.stringify(
1254-
request,
1255-
)}, import.meta.url);\n`
1256-
: `var ${importName} = require(${JSON.stringify(request)});\n`;
1257-
}
1231+
code += options.esModule
1232+
? `var ${importName} = new URL(${JSON.stringify(
1233+
request,
1234+
)}, import.meta.url);\n`
1235+
: `var ${importName} = require(${JSON.stringify(request)});\n`;
12581236
}
12591237

12601238
return `// Imports\n${code}`;
@@ -1279,44 +1257,55 @@ export function convertToTemplateLiteral(str) {
12791257
return `\`${escapedString}\``;
12801258
}
12811259

1282-
export function getModuleCode(html, replacements, options) {
1260+
const GET_SOURCE_FROM_IMPORT_NAME = "___HTML_LOADER_GET_SOURCE_FROM_IMPORT___";
1261+
1262+
export function getModuleCode(html, replacements, loaderContext, options) {
12831263
let code = html;
1284-
let replacersCode = "";
12851264

12861265
const { isTemplateLiteralSupported } = options;
12871266

1267+
let needHelperImport = false;
1268+
12881269
for (const item of replacements) {
1289-
const { runtime, importName, replacementName, isValueQuoted, hash } = item;
1270+
const { importName, replacementName, isValueQuoted, hash } = item;
12901271

1291-
if (typeof runtime === "undefined" || runtime === true) {
1292-
const getUrlOptions = []
1293-
.concat(hash ? [`hash: ${JSON.stringify(hash)}`] : [])
1294-
.concat(isValueQuoted ? [] : "maybeNeedQuotes: true");
1295-
const preparedOptions =
1296-
getUrlOptions.length > 0 ? `, { ${getUrlOptions.join(", ")} }` : "";
1272+
if (!isValueQuoted && !needHelperImport) {
1273+
needHelperImport = true;
1274+
}
12971275

1298-
replacersCode += `var ${replacementName} = ${GET_SOURCE_FROM_IMPORT_NAME}(${importName}${preparedOptions});\n`;
1276+
const name = !isValueQuoted
1277+
? `${GET_SOURCE_FROM_IMPORT_NAME}(${importName}${!isValueQuoted ? ", true" : ""})`
1278+
: importName;
12991279

1300-
code = code.replace(new RegExp(replacementName, "g"), () =>
1301-
isTemplateLiteralSupported
1302-
? `\${${replacementName}}`
1303-
: `" + ${replacementName} + "`,
1304-
);
1305-
} else {
1306-
code = code.replace(new RegExp(replacementName, "g"), () =>
1307-
isTemplateLiteralSupported
1308-
? `\${${replacementName}}`
1309-
: `" + ${replacementName} + "`,
1310-
);
1311-
}
1280+
code = code.replace(new RegExp(replacementName, "g"), () =>
1281+
isTemplateLiteralSupported
1282+
? `\${${name}}${typeof hash !== "undefined" ? hash : ""}`
1283+
: `" + ${name}${typeof hash !== "undefined" ? ` + ${JSON.stringify(hash)}` : ""} + "`,
1284+
);
13121285
}
13131286

13141287
// Replaces "<script>" or "</script>" to "<" + "script>" or "<" + "/script>".
13151288
code = code.replace(/<(\/?script)/g, (_, s) =>
13161289
isTemplateLiteralSupported ? `\${"<" + "${s}"}` : `<" + "${s}`,
13171290
);
13181291

1319-
return `// Module\n${replacersCode}var code = ${code};\n`;
1292+
code = `// Module\nvar code = ${code};\n`;
1293+
1294+
if (needHelperImport) {
1295+
// TODO simplify in the next major release
1296+
const getURLRuntime = require.resolve("./runtime/getUrl.js");
1297+
const context = loaderContext.context || loaderContext.rootContext;
1298+
const fileURLToHelper =
1299+
typeof loaderContext.utils !== "undefined" &&
1300+
typeof loaderContext.utils.contextify === "function"
1301+
? loaderContext.utils.contextify(context, getURLRuntime)
1302+
: contextify(context, getURLRuntime);
1303+
code = options.esModule
1304+
? `import ${GET_SOURCE_FROM_IMPORT_NAME} from "${fileURLToHelper}";\n${code}`
1305+
: `var ${GET_SOURCE_FROM_IMPORT_NAME} = require("${fileURLToHelper}");\n${code}`;
1306+
}
1307+
1308+
return code;
13201309
}
13211310

13221311
export function getExportCode(html, options) {

0 commit comments

Comments
 (0)