pablo-abc / svelte-markdown

Markdown parser to svelte components
MIT License
360 stars 50 forks source link

feat: add typings #15

Closed Luke-zhang-04 closed 3 years ago

Luke-zhang-04 commented 3 years ago

Adds typescript definitions

As for 9a7cef2, the library currently imports the entirety of marked because the marked esmodule library doesn't support tree shaking. This small change brought the bundle size from 66kb to 57kb.

pablo-abc commented 3 years ago

This is beautiful work! While I'm versed in TypeScript I am actually not that familiar with TypeScript + Svelte. Is there any documentation you may point to that would help me maintain this in the future? That'd be my only worry if I merge this.

Luke-zhang-04 commented 3 years ago

There are a couple of resources

There isn't too much to worry about if you've already worked with Typescript. I sorta just looked at someone else's typings and did something similar.

Going back to 9a7cef2, I'm not too sure about this one. If someone tries to import marked/lib/marked.esm.js, that would create 2 instances of marked in the bundle, because marked/src/Lexer is separate code, right? marked/lib/marked.esm.js isn't tree-shakable, which is the problem, but I don't know if it's a good idea to try and get around it. What do you think?

TheOnlyTails commented 3 years ago

Hi, is this PR going to be merged soon? I want to use this package in my website and it would be great to have TS support.

Luke-zhang-04 commented 3 years ago

Hi, is this PR going to be merged soon? I want to use this package in my website and it would be great to have TS support.

In the meantime, you can make a file called something.d.ts and put this in it:

declare module "svelte-markdown" {
    // Everything from https://github.com/Luke-zhang-04/svelte-markdown/blob/9a7cef2b657c80fe59aebcc4d76f2f979fc777ae/types/index.d.ts
}

You should install @types/marked too

TheOnlyTails commented 3 years ago

Great, thank you!

pablo-abc commented 3 years ago

Sorry about the late responses. I need to get up to speed but have been dealing with a broken arm.

@Luke-zhang-04 Regarding the comment about where to pull the Lexer from, this is something to be tested. But there is the risk of duplicate code anyway if the user needs Marked by itself with a slightly different version. In this case I do believe the benefits of pulling from marked/src/Lexer overweight the possible downsides. It makes sense for svelte-markdown to only bundle the code it specifically uses!

Luke-zhang-04 commented 3 years ago

Alright, great! Hopefully your arm gets better soon.