Skip to content

Commit 0c4c2b6

Browse files
committed
feat: add esm for client
1 parent 154d81f commit 0c4c2b6

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
*
55
* @see {@link https://github.com/substack/node-browserify#browser-field}
66
*/
7-
module.exports = require('./lib/server/html-to-dom');
7+
var HTMLDOMParser = require('./lib/server/html-to-dom');
8+
9+
module.exports = HTMLDOMParser;
10+
module.exports.default = HTMLDOMParser;

lib/client/html-to-dom.mjs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import domparser from './domparser';
2+
import { formatDOM } from './utilities';
3+
4+
var DIRECTIVE_REGEX = /<(![a-zA-Z\s]+)>/; // e.g., <!doctype html>
5+
6+
/**
7+
* Parses HTML string to DOM nodes in browser.
8+
*
9+
* @param {string} html - HTML markup.
10+
* @return {DomElement[]} - DOM elements.
11+
*/
12+
function HTMLDOMParser(html) {
13+
if (typeof html !== 'string') {
14+
throw new TypeError('First argument must be a string');
15+
}
16+
17+
if (html === '') {
18+
return [];
19+
}
20+
21+
// match directive
22+
var match = html.match(DIRECTIVE_REGEX);
23+
var directive;
24+
25+
if (match && match[1]) {
26+
directive = match[1];
27+
}
28+
29+
return formatDOM(domparser(html), null, directive);
30+
}
31+
32+
module.exports = HTMLDOMParser;
33+
module.exports.default = HTMLDOMParser;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@
8686
"/lib"
8787
],
8888
"browser": {
89-
"./index.js": "./lib/client/html-to-dom.js"
89+
"./index.js": "./lib/client/html-to-dom.js",
90+
"./index.mjs": "./lib/client/html-to-dom.mjs"
9091
},
9192
"react-native": {
9293
"./index.js": "./lib/server/html-to-dom.js"

0 commit comments

Comments
 (0)