Skip to content

Commit 02b671a

Browse files
refactor: code
1 parent c354b99 commit 02b671a

37 files changed

+2245
-338
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ insert_final_newline = true
99
trim_trailing_whitespace = true
1010

1111
[*.md]
12-
trim_trailing_whitespace = false
12+
trim_trailing_whitespace = false

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"dependencies": {
4444
"es6-templates": "^0.2.3",
4545
"fastparse": "^1.1.2",
46+
"file-loader": "^5.0.2",
4647
"html-minifier-terser": "^5.0.2",
4748
"loader-utils": "^1.2.3",
4849
"schema-utils": "^2.6.4"

src/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const GET_URL_CODE =
22
'function __url__(url) { return url.__esModule ? url.default : url; }';
33

4-
export const IDENT_REGEX = /xxxHTMLLINKxxx[0-9.]+xxx/g;
4+
export const IDENT_REGEX = /___HTML_LOADER_IDENT_[0-9.]+___/g;
55

66
export const REQUIRE_REGEX = /\${require\([^)]*\)}/g;

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default function htmlLoader(source) {
3434
const replacers = new Map();
3535

3636
let offset = 0;
37+
3738
for (const link of links) {
3839
if (
3940
link.value &&
@@ -118,7 +119,7 @@ export default function htmlLoader(source) {
118119
content = JSON.stringify(content);
119120
}
120121

121-
const importCode = getImportCode(replacers);
122+
const importCode = getImportCode(this, content, replacers, options);
122123
const exportCode = getExportCode(content, replacers, options);
123124

124125
return `${importCode}${exportCode};`;

src/parseAttributes.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable no-useless-escape,line-comment-position */
21
import Parser from 'fastparse';
32

43
function processMatch(match, strUntilValue, name, value, index) {
@@ -26,8 +25,10 @@ const parser = new Parser({
2625
},
2726
},
2827
inside: {
29-
'\\s+': true, // eat up whitespace
30-
'>': 'outside', // end of attributes
28+
// eat up whitespace
29+
'\\s+': true,
30+
// end of attributes
31+
'>': 'outside',
3132
'(([0-9a-zA-Z\\-:]+)\\s*=\\s*")([^"]*)"': processMatch,
3233
"(([0-9a-zA-Z\\-:]+)\\s*=\\s*')([^']*)'": processMatch,
3334
'(([0-9a-zA-Z\\-:]+)\\s*=\\s*)([^\\s>]+)': processMatch,

src/utils.js

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import { urlToRequest } from 'loader-utils';
1+
import { urlToRequest, stringifyRequest } from 'loader-utils';
22

33
import parseAttributes from './parseAttributes';
44
import { GET_URL_CODE, IDENT_REGEX } from './constants';
55

6-
function randomIdent() {
7-
return `xxxHTMLLINKxxx${Math.random()}${Math.random()}xxx`;
8-
}
9-
106
export function getTagsAndAttributes(attributes) {
117
const defaultAttributes = ['img:src'];
128

@@ -50,13 +46,7 @@ export function getLinks(content, attributes) {
5046
}
5147

5248
export function getUniqueIdent(data) {
53-
const ident = randomIdent();
54-
55-
if (data.has(ident)) {
56-
return getUniqueIdent(data);
57-
}
58-
59-
return ident;
49+
return `___HTML_LOADER_IDENT_${data.size}___`;
6050
}
6151

6252
export function replaceLinkWithIdent(source, link, ident, offset = 0) {
@@ -71,34 +61,46 @@ export function isProductionMode(loaderContext) {
7161
return loaderContext.mode === 'production' || !loaderContext.mode;
7262
}
7363

74-
export function getImportCode(replacers) {
64+
export function getImportCode(loaderContext, content, replacers, options) {
7565
if (replacers.size === 0) {
7666
return '';
7767
}
7868

79-
return GET_URL_CODE;
80-
}
69+
const importItems = [];
8170

82-
export function getExportCode(content, replacers, options) {
83-
let newContent = content;
71+
importItems.push(GET_URL_CODE);
8472

85-
newContent = content.replace(IDENT_REGEX, (match) => {
86-
if (!replacers.has(match)) {
87-
return match;
73+
const idents = replacers.keys();
74+
75+
for (const ident of idents) {
76+
const url = replacers.get(ident);
77+
const request = urlToRequest(url, options.root);
78+
const stringifiedRequest = stringifyRequest(loaderContext, request);
79+
80+
if (options.esModule) {
81+
importItems.push(`import ${ident} from ${stringifiedRequest};`);
82+
} else {
83+
importItems.push(`var ${ident} = require(${stringifiedRequest});`);
8884
}
85+
}
86+
87+
const importCode = importItems.join('\n');
8988

90-
let request = urlToRequest(replacers.get(match), options.root);
89+
return `// Imports\n${importCode}\n`;
90+
}
9191

92-
if (options.interpolate === 'require') {
93-
request = replacers.get(match);
92+
export function getExportCode(content, replacers, options) {
93+
const exportCode = content.replace(IDENT_REGEX, (match) => {
94+
if (!replacers.has(match)) {
95+
return match;
9496
}
9597

96-
return `" + __url__(require(${JSON.stringify(request)})) + "`;
98+
return `" + __url__(${match}) + "`;
9799
});
98100

99101
if (options.esModule) {
100-
return `export default ${newContent}`;
102+
return `// Exports\nexport default ${exportCode}`;
101103
}
102104

103-
return `module.exports = ${newContent}`;
105+
return `// Exports\nmodule.exports = ${exportCode}`;
104106
}

0 commit comments

Comments
 (0)