Skip to content

Commit de14110

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

File tree

5 files changed

+41
-20
lines changed

5 files changed

+41
-20
lines changed

build.ts

+21-9
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,20 +20,26 @@ 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
}) ]
26-
}).then()
29+
})
2730

2831
await build({
2932
...commonOptions,
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
}) ]
36-
}).then()
42+
})
3743

3844
/**
3945
* Browser bundles are use for import/require directly from browser scripts.
@@ -45,17 +51,23 @@ 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-
}
50-
}).then()
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']
60+
})
5161

5262
await build({
5363
...commonOptions,
5464
outfile: 'dist/node-html-markdown.browser.cjs',
5565
format: 'cjs',
5666
platform: 'browser',
5767
define: {
58-
'process.env.LOG_PERF': process.env.LOG_PERF ?? 'false'
59-
}
60-
}).then()
68+
'process.env.LOG_PERF': process.env.LOG_PERF ?? 'false',
69+
'__IS_BROWSER__': 'true'
70+
},
71+
external: ['node-html-parser']
72+
})
6173
})()

jest.config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const config: Config = {
1313
}
1414
]
1515
},
16+
globals: {
17+
'__IS_BROWSER__': false
18+
},
1619
modulePaths: [ '<rootDir>' ],
1720
testTimeout: 10000,
1821
roots: [ '<rootDir>' ],

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

+8-5
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,6 +22,12 @@ export type TextNode = (NHParser.TextNode) & NodeBase
2522
// region: TypeGuards
2623
/* ****************************************************************************************************************** */
2724

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

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)