Skip to content

Commit bd5ad04

Browse files
refactor: reduce runtime code
1 parent a2463ca commit bd5ad04

8 files changed

+3169
-3833
lines changed

src/plugins/sources-plugin.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,8 @@ 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, runtime, startOffset, endOffset } =
143+
source;
151144

152145
let request = value;
153146

@@ -172,7 +165,7 @@ export default (options) =>
172165
importName = `___HTML_LOADER_IMPORT_${imports.size}___`;
173166
imports.set(request, importName);
174167

175-
options.imports.push({ format, importName, request });
168+
options.imports.push({ importName, request });
176169
}
177170

178171
const replacementKey = JSON.stringify({ request, isValueQuoted, hash });

src/utils.js

+14-19
Original file line numberDiff line numberDiff line change
@@ -1239,22 +1239,13 @@ export function getImportCode(html, loaderContext, imports, options) {
12391239
: `var ${GET_SOURCE_FROM_IMPORT_NAME} = require("${fileURLToHelper}");\n`;
12401240

12411241
for (const item of imports) {
1242-
const { format, importName, request } = item;
1242+
const { importName, request } = item;
12431243

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-
}
1244+
code += options.esModule
1245+
? `var ${importName} = new URL(${JSON.stringify(
1246+
request,
1247+
)}, import.meta.url);\n`
1248+
: `var ${importName} = require(${JSON.stringify(request)});\n`;
12581249
}
12591250

12601251
return `// Imports\n${code}`;
@@ -1295,12 +1286,16 @@ export function getModuleCode(html, replacements, options) {
12951286
const preparedOptions =
12961287
getUrlOptions.length > 0 ? `, { ${getUrlOptions.join(", ")} }` : "";
12971288

1298-
replacersCode += `var ${replacementName} = ${GET_SOURCE_FROM_IMPORT_NAME}(${importName}${preparedOptions});\n`;
1289+
const needHelperFn = getUrlOptions.length > 0;
1290+
1291+
if (needHelperFn) {
1292+
replacersCode += `var ${replacementName} = ${GET_SOURCE_FROM_IMPORT_NAME}(${importName}${preparedOptions});\n`;
1293+
}
1294+
1295+
const name = needHelperFn ? replacementName : importName;
12991296

13001297
code = code.replace(new RegExp(replacementName, "g"), () =>
1301-
isTemplateLiteralSupported
1302-
? `\${${replacementName}}`
1303-
: `" + ${replacementName} + "`,
1298+
isTemplateLiteralSupported ? `\${${name}}` : `" + ${name} + "`,
13041299
);
13051300
} else {
13061301
code = code.replace(new RegExp(replacementName, "g"), () =>

test/__snapshots__/esModule-option.test.js.snap

+457-536
Large diffs are not rendered by default.

test/__snapshots__/loader.test.js.snap

+320-380
Large diffs are not rendered by default.

test/__snapshots__/minimize-option.test.js.snap

+468-631
Large diffs are not rendered by default.

test/__snapshots__/postprocessor-option.test.js.snap

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ exports[`'postprocess' option should work with async "postprocessor" function op
77
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from "../../src/runtime/getUrl.js";
88
var ___HTML_LOADER_IMPORT_0___ = new URL("./image.png", import.meta.url);
99
// Module
10-
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
1110
var code = \`<div>
1211
<p>{{firstname}} {{lastname}}</p>
13-
<img src="\${___HTML_LOADER_REPLACEMENT_0___}" alt="alt" />
12+
<img src="\${___HTML_LOADER_IMPORT_0___}" alt="alt" />
1413
<div>
1514
\`;
1615
// Exports
@@ -34,8 +33,7 @@ exports[`'postprocess' option should work with the "postprocessor" option #1: mo
3433
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from "../../src/runtime/getUrl.js";
3534
var ___HTML_LOADER_IMPORT_0___ = new URL("./image.png", import.meta.url);
3635
// Module
37-
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
38-
var code = "<img src=\\"" + ___HTML_LOADER_REPLACEMENT_0___ + "\\">\\n<img src=\\"" + 'Hello ' + (1+1) + "\\">\\n<img src=\\"" + require('./image.png') + "\\">\\n<img src=\\"" + new URL('./image.png', import.meta.url) + "\\">\\n<div>" + require('./gallery.html').default + "</div>\\n<!--Works fine, but need improve testing <div>< %= (await import('./gallery.html')).default % ></div>-->\\n";
36+
var code = "<img src=\\"" + ___HTML_LOADER_IMPORT_0___ + "\\">\\n<img src=\\"" + 'Hello ' + (1+1) + "\\">\\n<img src=\\"" + require('./image.png') + "\\">\\n<img src=\\"" + new URL('./image.png', import.meta.url) + "\\">\\n<div>" + require('./gallery.html').default + "</div>\\n<!--Works fine, but need improve testing <div>< %= (await import('./gallery.html')).default % ></div>-->\\n";
3937
// Exports
4038
export default code;"
4139
`;
@@ -59,8 +57,7 @@ exports[`'postprocess' option should work with the "postprocessor" option: modul
5957
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from "../../src/runtime/getUrl.js";
6058
var ___HTML_LOADER_IMPORT_0___ = new URL("./image.png", import.meta.url);
6159
// Module
62-
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
63-
var code = \`<img src="\${___HTML_LOADER_REPLACEMENT_0___}">
60+
var code = \`<img src="\${___HTML_LOADER_IMPORT_0___}">
6461
<img src="\${ 'Hello ' + (1+1) }">
6562
<img src="\${ require('./image.png') }">
6663
<img src="\${ new URL('./image.png', import.meta.url) }">

test/__snapshots__/preprocessor-option.test.js.snap

+4-10
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ exports[`'preprocess' option should work with async "preprocessor" function opti
77
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from "../../src/runtime/getUrl.js";
88
var ___HTML_LOADER_IMPORT_0___ = new URL("./image.png", import.meta.url);
99
// Module
10-
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
1110
var code = \`<div>
1211
<p>Alexander Krasnoyarov</p>
13-
<img src="\${___HTML_LOADER_REPLACEMENT_0___}" alt="alt" />
12+
<img src="\${___HTML_LOADER_IMPORT_0___}" alt="alt" />
1413
<div>
1514
\`;
1615
// Exports
@@ -35,9 +34,7 @@ import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from "../../src/runtime/getUrl.j
3534
var ___HTML_LOADER_IMPORT_0___ = new URL("./image.png.webp", import.meta.url);
3635
var ___HTML_LOADER_IMPORT_1___ = new URL("./image.png", import.meta.url);
3736
// Module
38-
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
39-
var ___HTML_LOADER_REPLACEMENT_1___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_1___);
40-
var code = \`<picture><source type="image/webp" srcset="\${___HTML_LOADER_REPLACEMENT_0___}"><img src="\${___HTML_LOADER_REPLACEMENT_1___}"></picture>
37+
var code = \`<picture><source type="image/webp" srcset="\${___HTML_LOADER_IMPORT_0___}"><img src="\${___HTML_LOADER_IMPORT_1___}"></picture>
4138
\`;
4239
// Exports
4340
export default code;"
@@ -57,10 +54,9 @@ exports[`'preprocess' option should work with the "preprocessor" option: module
5754
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from "../../src/runtime/getUrl.js";
5855
var ___HTML_LOADER_IMPORT_0___ = new URL("./image.png", import.meta.url);
5956
// Module
60-
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
6157
var code = \`<div>
6258
<p>Alexander Krasnoyarov</p>
63-
<img src="\${___HTML_LOADER_REPLACEMENT_0___}" alt="alt" />
59+
<img src="\${___HTML_LOADER_IMPORT_0___}" alt="alt" />
6460
<div>
6561
\`;
6662
// Exports
@@ -85,9 +81,7 @@ import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from "../../src/runtime/getUrl.j
8581
var ___HTML_LOADER_IMPORT_0___ = new URL("./image.png.webp", import.meta.url);
8682
var ___HTML_LOADER_IMPORT_1___ = new URL("./image.png", import.meta.url);
8783
// Module
88-
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
89-
var ___HTML_LOADER_REPLACEMENT_1___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_1___);
90-
var code = \`<picture><source type="image/webp" srcset="\${___HTML_LOADER_REPLACEMENT_0___}"><img src="\${___HTML_LOADER_REPLACEMENT_1___}"></picture>
84+
var code = \`<picture><source type="image/webp" srcset="\${___HTML_LOADER_IMPORT_0___}"><img src="\${___HTML_LOADER_IMPORT_1___}"></picture>
9185
\`;
9286
// Exports
9387
export default code;"

0 commit comments

Comments
 (0)