taoqf / node-html-parser

A very fast HTML parser, generating a simplified DOM, with basic element query support.
MIT License
1.11k stars 107 forks source link

fix: fix vite build error #208

Closed Pat1enceLos closed 2 years ago

Pat1enceLos commented 2 years ago

fix build error when use vite build:

import { parse } from 'node-html-parser'

// parse is not a function
parse(text);
taoqf commented 2 years ago

Thank you so much for your work. I tried create a vite project and used with your code but I could not see any build error. Could you please show me more clue?

Pat1enceLos commented 2 years ago

Steps to reproduce:

taoqf commented 2 years ago

I tried in my vite project:

import { parse } from 'node-html-parser';

export default function MyComponent() {
    useEffect(() => {
        const a = parse('<div></div>');
        let b = a;
        console.log(b);
    }, []);
}

and it seems works well.

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"],
      "@pages/*": ["./pages/*"],
      "@static/*": ["./static/*"],
    },
    "target": "ESNext",
    "useDefineForClassFields": true,
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"

  },
  "include": ["**/*.ts", "**/*.tsx","src"],
  // "include": ["src"],
  "references": [{ "path": "./tsconfig.node.json" }]
}

image

Pat1enceLos commented 2 years ago

I reproduced in Vite3 + Vue3 before. So I tried a new project Vite3 + React18 and use your config file, but still got the error Did you got the error in http://localhost:4173/ ?

image

taoqf commented 2 years ago

I'm really so sorry for that. My mistake, I didn't browse the page.

You can try this:

import parse from 'node-html-parser';
Pat1enceLos commented 2 years ago
import parse from 'node-html-parser;

It will not work in dev runtime by:

yarn dev

image

taoqf commented 2 years ago

I tried to fix this, but I am not sure this commit will cause trouble with https://github.com/taoqf/node-html-parser/pull/166 , so I published this in prepatch version.

Pat1enceLos commented 2 years ago

I thought the reason maybe was:

// src/index.ts
export { default as parse, default } from './parse';

// esm/index.js
import nhp from '../dist/index.js' // nhp just stands for parse function
export const CommentNode = nhp.CommentNode;
export const HTMLElement = nhp.HTMLElement;
...

and I was a little bit confused, why should we bind attributes to parse function in v5.4.2-0:

parse.parse = baseParse;
parse.HTMLElement = HTMLElement;
parse.CommentNode = CommentNode;
parse.valid = valid;
parse.Node = Node;
parse.TextNode = TextNode;
parse.NodeType = NodeType;

and also why should there export default parse function hoping for your replay :)

taoqf commented 2 years ago

why should we bind attributes to parse function in v5.4.2-0: why should there export default parse function

I think some people would import this lib like this

import parse from ''node-html-parser;
// or
import { parse } from ''node-html-parser;

I would like both way would work.

Would v5.4.2-0 work for you?

Pat1enceLos commented 2 years ago

Would v5.4.2-0 work for you?

Yes, it works well :)