Skip to content

Commit 0758b1a

Browse files
refactor: removed parse5-sax-parser package in favor parse5 (#365)
1 parent 6654c9d commit 0758b1a

File tree

9 files changed

+204
-74
lines changed

9 files changed

+204
-74
lines changed

package-lock.json

Lines changed: 2 additions & 18 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
},
4848
"dependencies": {
4949
"html-minifier-terser": "^5.1.1",
50-
"parse5-sax-parser": "^6.0.1"
50+
"parse5": "^6.0.1"
5151
},
5252
"devDependencies": {
5353
"@babel/cli": "^7.12.10",

src/parse5-traverse.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const traverse = (root, callback) => {
2+
const visit = (node, parent) => {
3+
let res;
4+
5+
if (callback) {
6+
res = callback(node, parent);
7+
}
8+
9+
let { childNodes } = node;
10+
11+
// in case a <template> tag is in the middle of the HTML: https://github.com/JPeer264/node-rcs-core/issues/58
12+
if (node.content && Array.isArray(node.content.childNodes)) {
13+
({ childNodes } = node.content);
14+
}
15+
16+
if (res !== false && Array.isArray(childNodes) && childNodes.length >= 0) {
17+
childNodes.forEach((child) => {
18+
visit(child, node);
19+
});
20+
}
21+
};
22+
23+
visit(root, null);
24+
};
25+
26+
module.exports = traverse;

src/plugins/sources-plugin.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import SAXParser from 'parse5-sax-parser';
1+
import parse5 from 'parse5';
2+
3+
import traverse from '../parse5-traverse';
24

35
import {
46
getFilter,
@@ -9,12 +11,16 @@ import {
911

1012
export default (options) =>
1113
function process(html) {
12-
const parser5 = new SAXParser({ sourceCodeLocationInfo: true });
1314
const sources = [];
15+
const document = parse5.parse(html, { sourceCodeLocationInfo: true });
1416

15-
parser5.on('startTag', (node) => {
17+
traverse(document, (node) => {
1618
const { tagName, attrs: attributes, sourceCodeLocation } = node;
1719

20+
if (!tagName) {
21+
return;
22+
}
23+
1824
attributes.forEach((attribute) => {
1925
let { name } = attribute;
2026

@@ -92,8 +98,6 @@ export default (options) =>
9298
});
9399
});
94100

95-
parser5.end(html);
96-
97101
const urlFilter = getFilter(options.sources.urlFilter);
98102
const imports = new Map();
99103
const replacements = new Map();

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

Lines changed: 21 additions & 6 deletions
Large diffs are not rendered by default.

test/__snapshots__/loader.test.js.snap

Lines changed: 7 additions & 2 deletions
Large diffs are not rendered by default.

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

Lines changed: 27 additions & 12 deletions
Large diffs are not rendered by default.

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

Lines changed: 105 additions & 30 deletions
Large diffs are not rendered by default.

test/fixtures/simple.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,9 @@ <h2>An Ordered HTML List</h2>
442442
<img src='\nested\image3.png' />
443443
<img src='/nested\image3.png' />
444444
<img src='nested\image3.png' />
445+
446+
<div class="will-be-parsed">
447+
<template type="some_type">
448+
<div class="will-not-be-parse"></div>
449+
</template>
450+
</div>

0 commit comments

Comments
 (0)