Skip to content

Commit 6075f4b

Browse files
fix: use fast-xml-parser to parse and build XML. This substantially reduces the bundle size. (#234)
* refactor: use `fast-xml-parser` to parse and build XML * fix: update format of mock data --------- Co-authored-by: Timothy Jones <timothy.l.jones@gmail.com>
1 parent 75d69a6 commit 6075f4b

File tree

4 files changed

+15
-23
lines changed

4 files changed

+15
-23
lines changed

lib/updaters/types/maven.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
1-
const jsdom = require('jsdom');
2-
const serialize = require('w3c-xmlserializer');
31
const detectNewline = require('detect-newline');
2+
const { XMLParser, XMLBuilder } = require('fast-xml-parser');
3+
44
const CRLF = '\r\n';
55
const LF = '\n';
66

77
function pomDocument(contents) {
8-
const dom = new jsdom.JSDOM('');
9-
const parser = new dom.window.DOMParser();
10-
return parser.parseFromString(contents, 'application/xml');
8+
const parser = new XMLParser();
9+
return parser.parse(contents);
1110
}
1211

13-
function pomVersionElement(document) {
14-
const versionElement = document.querySelector('project > version');
12+
function pomVersion(document) {
13+
const version = document?.project?.version;
1514

16-
if (!versionElement) {
15+
if (!version) {
1716
throw new Error(
1817
'Failed to read the version field in your pom file - is it present?',
1918
);
2019
}
2120

22-
return versionElement;
21+
return version;
2322
}
2423

2524
module.exports.readVersion = function (contents) {
2625
const document = pomDocument(contents);
27-
return pomVersionElement(document).textContent;
26+
return pomVersion(document);
2827
};
2928

3029
module.exports.writeVersion = function (contents, version) {
3130
const newline = detectNewline(contents);
3231
const document = pomDocument(contents);
33-
const versionElement = pomVersionElement(document);
3432

35-
versionElement.textContent = version;
33+
document.project.version = version;
3634

37-
const xml = serialize(document);
35+
const builder = new XMLBuilder({ format: true });
36+
const xml = builder.build(document);
3837

3938
if (newline === CRLF) {
4039
return xml.replace(/\n/g, CRLF) + CRLF;

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,11 @@
4747
"detect-indent": "^6.1.0",
4848
"detect-newline": "^3.1.0",
4949
"dotgitignore": "^2.1.0",
50+
"fast-xml-parser": "^5.2.5",
5051
"figures": "^3.2.0",
5152
"find-up": "^5.0.0",
5253
"git-semver-tags": "^5.0.1",
53-
"jsdom": "^25.0.1",
5454
"semver": "^7.7.2",
55-
"w3c-xmlserializer": "^5.0.0",
5655
"yaml": "^2.6.0",
5756
"yargs": "^17.7.2"
5857
},

test/mocks/pom-6.4.0-crlf.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
<project>
22
<modelVersion>4.0.0</modelVersion>
3-
43
<parent>
54
<groupId>com.mycompany.app</groupId>
65
<artifactId>my-app</artifactId>
76
<version>3.0.0</version>
87
</parent>
9-
108
<groupId>com.mycompany.app</groupId>
119
<artifactId>my-module</artifactId>
1210
<version>6.4.0</version>
13-
1411
<properties>
1512
<some.version>2.0.0</some.version>
1613
</properties>
17-
1814
<dependencies>
1915
<dependency>
2016
<groupId>org.some.dependency</groupId>
@@ -28,3 +24,4 @@
2824
</dependency>
2925
</dependencies>
3026
</project>
27+

test/mocks/pom-6.4.0-lf.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
<project>
22
<modelVersion>4.0.0</modelVersion>
3-
43
<parent>
54
<groupId>com.mycompany.app</groupId>
65
<artifactId>my-app</artifactId>
76
<version>3.0.0</version>
87
</parent>
9-
108
<groupId>com.mycompany.app</groupId>
119
<artifactId>my-module</artifactId>
1210
<version>6.4.0</version>
13-
1411
<properties>
1512
<some.version>2.0.0</some.version>
1613
</properties>
17-
1814
<dependencies>
1915
<dependency>
2016
<groupId>org.some.dependency</groupId>
@@ -28,3 +24,4 @@
2824
</dependency>
2925
</dependencies>
3026
</project>
27+

0 commit comments

Comments
 (0)