Skip to content

Commit

Permalink
feat: remove node-html-parser from browser bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
SettingDust committed Dec 1, 2022
1 parent f7d6a37 commit de14110
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
30 changes: 21 additions & 9 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { nodeExternalsPlugin } from 'esbuild-node-externals';
target: 'es2017',
logLevel: 'info',
color: true,
// minify: true,
minify: true,
sourcemap: true,
legalComments: 'external'
}
Expand All @@ -20,20 +20,26 @@ import { nodeExternalsPlugin } from 'esbuild-node-externals';
outfile: 'dist/node-html-markdown.js',
format: 'esm',
platform: 'node',
define: {
'__IS_BROWSER__': 'false'
},
plugins: [ nodeExternalsPlugin({
packagePath: './package.json'
}) ]
}).then()
})

await build({
...commonOptions,
outfile: 'dist/node-html-markdown.cjs',
format: 'cjs',
platform: 'node',
define: {
'__IS_BROWSER__': 'false'
},
plugins: [ nodeExternalsPlugin({
packagePath: './package.json'
}) ]
}).then()
})

/**
* Browser bundles are use for import/require directly from browser scripts.
Expand All @@ -45,17 +51,23 @@ import { nodeExternalsPlugin } from 'esbuild-node-externals';
format: 'esm',
platform: 'browser',
define: {
'process.env.LOG_PERF': process.env.LOG_PERF ?? 'false'
}
}).then()
// This will remove the perfStart/End after minified by esbuild.
'process.env.LOG_PERF': process.env.LOG_PERF ?? 'false',
// This will remove the node-html-parser usage
'__IS_BROWSER__': 'true'
},
external: ['node-html-parser']
})

await build({
...commonOptions,
outfile: 'dist/node-html-markdown.browser.cjs',
format: 'cjs',
platform: 'browser',
define: {
'process.env.LOG_PERF': process.env.LOG_PERF ?? 'false'
}
}).then()
'process.env.LOG_PERF': process.env.LOG_PERF ?? 'false',
'__IS_BROWSER__': 'true'
},
external: ['node-html-parser']
})
})()
3 changes: 3 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const config: Config = {
}
]
},
globals: {
'__IS_BROWSER__': false
},
modulePaths: [ '<rootDir>' ],
testTimeout: 10000,
roots: [ '<rootDir>' ],
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isWhiteSpaceOnly, splitSpecial, surround, tagSurround, trimNewLines } from './utilities';
import { PostProcessResult, TranslatorConfigObject } from './translator';
import { NodeHtmlMarkdownOptions } from './options';
import { Options as NodeHtmlParserOptions } from 'node-html-parser'
import type { Options as NodeHtmlParserOptions } from 'node-html-parser'


/* ****************************************************************************************************************** */
Expand Down
13 changes: 8 additions & 5 deletions src/nodes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import * as NHParser from 'node-html-parser';
import { CommentNode, NodeType } from 'node-html-parser';

import type * as NHParser from 'node-html-parser';
import type { CommentNode } from 'node-html-parser';

/* ****************************************************************************************************************** */
// region: Types
/* ****************************************************************************************************************** */

export { NodeType, CommentNode }

/* ********************************************************* *
* Merged Nodes - Unions of node-html-parser and common DOM
* ********************************************************* */
Expand All @@ -25,6 +22,12 @@ export type TextNode = (NHParser.TextNode) & NodeBase
// region: TypeGuards
/* ****************************************************************************************************************** */

const NodeType = {
ELEMENT_NODE: 1,
TEXT_NODE: 3,
COMMENT_NODE: 8
}

export const isTextNode = (node: HtmlNode): node is TextNode => node.nodeType === NodeType.TEXT_NODE;
export const isCommentNode = (node: HtmlNode): node is CommentNode => node.nodeType === NodeType.COMMENT_NODE;
export const isElementNode = (node: HtmlNode): node is ElementNode => node.nodeType === NodeType.ELEMENT_NODE;
Expand Down
13 changes: 8 additions & 5 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { NodeHtmlMarkdownOptions } from './options';
import { ElementNode, HtmlNode } from './nodes';
import { nodeHtmlParserConfig } from './config';


/* ****************************************************************************************************************** */
// region: String Utils
/* ****************************************************************************************************************** */
Expand Down Expand Up @@ -123,6 +122,9 @@ export const truthyStr = (v: any, value?: string): string => v ? ((value !== und
/* ****************************************************************************************************************** */
// region: Parser
/* ****************************************************************************************************************** */
// For esbuild removing code
declare global {const __IS_BROWSER__: boolean}


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

/* If specified, try to parse with native engine, fallback to node-html-parser */
perfStart('parse');
let el: ElementNode | undefined;
if (options.preferNativeParser) {
/* If specified, try to parse with native engine, fallback to node-html-parser */
if (__IS_BROWSER__ || options.preferNativeParser) {
try {
el = tryParseWithNativeDom(html);
}
catch (e) {
if (__IS_BROWSER__) throw e;
nodeHtmlParse = getNodeHtmlParser();
if (nodeHtmlParse) console.warn('Native DOM parser encountered an error during parse', e);
else throw e;
}
} else nodeHtmlParse = getNodeHtmlParser();

if (!el) el = nodeHtmlParse!(html, nodeHtmlParserConfig);
if (!__IS_BROWSER__ && !el) el = nodeHtmlParse!(html, nodeHtmlParserConfig);
perfStop('parse');

return el;
return el!;
}

// endregion
Expand Down

0 comments on commit de14110

Please sign in to comment.