Skip to content

Commit 1addbb5

Browse files
committed
feat: remove node-html-parser from browser bundle
1 parent f7d6a37 commit 1addbb5

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

build.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { nodeExternalsPlugin } from 'esbuild-node-externals';
99
target: 'es2017',
1010
logLevel: 'info',
1111
color: true,
12-
// minify: true,
12+
minify: true,
1313
sourcemap: true,
1414
legalComments: 'external'
1515
}
@@ -20,6 +20,9 @@ import { nodeExternalsPlugin } from 'esbuild-node-externals';
2020
outfile: 'dist/node-html-markdown.js',
2121
format: 'esm',
2222
platform: 'node',
23+
define: {
24+
'IS_BROWSER': 'false'
25+
},
2326
plugins: [ nodeExternalsPlugin({
2427
packagePath: './package.json'
2528
}) ]
@@ -30,6 +33,9 @@ import { nodeExternalsPlugin } from 'esbuild-node-externals';
3033
outfile: 'dist/node-html-markdown.cjs',
3134
format: 'cjs',
3235
platform: 'node',
36+
define: {
37+
'IS_BROWSER': 'false'
38+
},
3339
plugins: [ nodeExternalsPlugin({
3440
packagePath: './package.json'
3541
}) ]
@@ -45,8 +51,12 @@ import { nodeExternalsPlugin } from 'esbuild-node-externals';
4551
format: 'esm',
4652
platform: 'browser',
4753
define: {
48-
'process.env.LOG_PERF': process.env.LOG_PERF ?? 'false'
49-
}
54+
// This will remove the perfStart/End after minified by esbuild.
55+
'process.env.LOG_PERF': process.env.LOG_PERF ?? 'false',
56+
// This will remove the node-html-parser usage
57+
'IS_BROWSER': 'true'
58+
},
59+
external: ['node-html-parser']
5060
}).then()
5161

5262
await build({
@@ -55,7 +65,9 @@ import { nodeExternalsPlugin } from 'esbuild-node-externals';
5565
format: 'cjs',
5666
platform: 'browser',
5767
define: {
58-
'process.env.LOG_PERF': process.env.LOG_PERF ?? 'false'
59-
}
68+
'process.env.LOG_PERF': process.env.LOG_PERF ?? 'false',
69+
'IS_BROWSER': 'true'
70+
},
71+
external: ['node-html-parser']
6072
}).then()
6173
})()

src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { isWhiteSpaceOnly, splitSpecial, surround, tagSurround, trimNewLines } from './utilities';
22
import { PostProcessResult, TranslatorConfigObject } from './translator';
33
import { NodeHtmlMarkdownOptions } from './options';
4-
import { Options as NodeHtmlParserOptions } from 'node-html-parser'
4+
import type { Options as NodeHtmlParserOptions } from 'node-html-parser'
55

66

77
/* ****************************************************************************************************************** */

src/nodes.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import * as NHParser from 'node-html-parser';
2-
import { CommentNode, NodeType } from 'node-html-parser';
3-
1+
import type * as NHParser from 'node-html-parser';
2+
import type { CommentNode } from 'node-html-parser';
43

54
/* ****************************************************************************************************************** */
65
// region: Types
76
/* ****************************************************************************************************************** */
87

9-
export { NodeType, CommentNode }
10-
118
/* ********************************************************* *
129
* Merged Nodes - Unions of node-html-parser and common DOM
1310
* ********************************************************* */
@@ -25,8 +22,8 @@ export type TextNode = (NHParser.TextNode) & NodeBase
2522
// region: TypeGuards
2623
/* ****************************************************************************************************************** */
2724

28-
export const isTextNode = (node: HtmlNode): node is TextNode => node.nodeType === NodeType.TEXT_NODE;
29-
export const isCommentNode = (node: HtmlNode): node is CommentNode => node.nodeType === NodeType.COMMENT_NODE;
30-
export const isElementNode = (node: HtmlNode): node is ElementNode => node.nodeType === NodeType.ELEMENT_NODE;
25+
export const isTextNode = (node: HtmlNode): node is TextNode => node.nodeType === 3;
26+
export const isCommentNode = (node: HtmlNode): node is CommentNode => node.nodeType === 8;
27+
export const isElementNode = (node: HtmlNode): node is ElementNode => node.nodeType === 1;
3128

3229
// endregion

src/utilities.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { NodeHtmlMarkdownOptions } from './options';
22
import { ElementNode, HtmlNode } from './nodes';
33
import { nodeHtmlParserConfig } from './config';
44

5-
65
/* ****************************************************************************************************************** */
76
// region: String Utils
87
/* ****************************************************************************************************************** */
@@ -123,6 +122,9 @@ export const truthyStr = (v: any, value?: string): string => v ? ((value !== und
123122
/* ****************************************************************************************************************** */
124123
// region: Parser
125124
/* ****************************************************************************************************************** */
125+
// For esbuild removing code
126+
declare global {const IS_BROWSER: boolean}
127+
126128

127129
function tryParseWithNativeDom(html: string): ElementNode | undefined {
128130
try {
@@ -169,24 +171,25 @@ const getNodeHtmlParser = () => {
169171
export function parseHTML(html: string, options: NodeHtmlMarkdownOptions): ElementNode {
170172
let nodeHtmlParse: ReturnType<typeof getNodeHtmlParser>;
171173

172-
/* If specified, try to parse with native engine, fallback to node-html-parser */
173174
perfStart('parse');
174175
let el: ElementNode | undefined;
175-
if (options.preferNativeParser) {
176+
/* If specified, try to parse with native engine, fallback to node-html-parser */
177+
if (IS_BROWSER || options.preferNativeParser) {
176178
try {
177179
el = tryParseWithNativeDom(html);
178180
}
179181
catch (e) {
182+
if (IS_BROWSER) throw e;
180183
nodeHtmlParse = getNodeHtmlParser();
181184
if (nodeHtmlParse) console.warn('Native DOM parser encountered an error during parse', e);
182185
else throw e;
183186
}
184187
} else nodeHtmlParse = getNodeHtmlParser();
185188

186-
if (!el) el = nodeHtmlParse!(html, nodeHtmlParserConfig);
189+
if (!IS_BROWSER && !el) el = nodeHtmlParse!(html, nodeHtmlParserConfig);
187190
perfStop('parse');
188191

189-
return el;
192+
return el!;
190193
}
191194

192195
// endregion

0 commit comments

Comments
 (0)