Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ESM Import Compatibility #660

Open
evanplaice opened this issue Feb 20, 2019 · 19 comments
Open

Add ESM Import Compatibility #660

evanplaice opened this issue Feb 20, 2019 · 19 comments
Assignees

Comments

@evanplaice
Copy link

evanplaice commented Feb 20, 2019

ESM is coming. It is already supported in most browsers and is coming to Node 1.

With that said, there are few libraries that have native support for it yet. As far as Markdown parsers go:

  • markdown-it is very heavily invested in CommonJS, ESM support will likely never happen there
  • marked - had a contributor implement ESM support and the maintainers rejected the PR
  • remark - also heavily invested in CommonJS
  • markdown-js - looks like it's no longer being maintained

Long-story-short, unless somebody starts a 'clean room' implementation of a Markdown parser in ES this is going to be a pain point for FE devs working in native ESM.

I've been experimenting a bit and managed to create a PR that adds support with very minimal impact on the existing codebase.

1 I'm a participant on the Node/Modules work group. If all goes well, we may see unflagged support before the end of the year.

@tivie tivie self-assigned this Mar 16, 2019
@tivie
Copy link
Member

tivie commented Mar 16, 2019

Thanks for your contribution. I will have a look as soon as possible, when I have the time.

@Justsnoopy30
Copy link

It still seems there is not ESM compatibility for Showdown in 2020.

@Gillardo
Copy link

Gillardo commented Nov 3, 2020

Is this coming?

@tirithen
Copy link

Any updates on ESM compatability?

@Marshal27
Copy link

Marshal27 commented Jul 19, 2021

+1

the dist in closed PR 661 so far works well.

@LeaVerou
Copy link

Another vote for ESM here. I love Showdown, but had to switch away from it for my modules because I can't distribute libraries that leak into the global scope. 😞

@wojteko22
Copy link

I'm also waiting for this and starting to think about switching library.

@Gillardo
Copy link

yeah i think i may have to switch if this doesnt happen soon, which is a shame as i like this library, no idea what to change to either

@jessejohnson
Copy link

Does anyone have an alternative that has ESM Import compat? It's 2023 and I can't seem to find any. :(

@LeaVerou what did you switch to?

@taorepoara
Copy link

Here is the way to import this lib in an ESM:

import Showdown from 'showdown';

@TheDogHusky
Copy link

TheDogHusky commented Apr 5, 2024

Hello!
If I downloaded showdown.min.js, on an Expressjs server, how can I do the import?
Currently I have a script /static/script/ticketManager.js that needs showdown.
Showdown is located in the same directory as ticketManager.
When I try to import import showdown from "/static/script/showdown.min.js", it says that it doesn't have default export. (the same happens if I do import showdown from "./showdown.min.js".

@TheDogHusky
Copy link

Is there no esm support for browser?

@suhli
Copy link

suhli commented May 20, 2024

not working with import() function

demo

TypeError: Cannot set properties of undefined (setting 'showdown')
    at https://cdn.jsdelivr.net/npm/showdown@latest/dist/showdown.min.js:2:72031
    at https://cdn.jsdelivr.net/npm/showdown@latest/dist/showdown.min.js:2:72035

just using jsDeliver /+esm path or esm.run

(async () => {
  try {
    const Showdown = await import("https://cdn.jsdelivr.net/npm/showdown/+esm");
    const converter = new Showdown.default.Converter();
    document.querySelector("#d").innerHTML = converter.makeHtml(
      `# showdown.js`
    );
  } catch (e) {
    console.error(e.stack);
  }
})();

@bgoosmanviz
Copy link

For the TypeScript fans:

Install showdown

yarn add showdown

showdown.d.ts

declare module 'showdown' {
  class Converter {
    constructor();
    makeHtml(text: string): string;
  }

  export { Converter };
}

MyComponent.tsx

import showdown from 'showdown';

@KeithHenry
Copy link

This is a 5 year old problem but it now is a complete block - I can't even consider using this library without ESM support.

Another vote for ESM here. I love Showdown, but had to switch away from it for my modules because I can't distribute libraries that leak into the global scope. 😞

@LeaVerou I realise this from 4 years ago, but please let us know you used instead?

Everyone else: has anyone forked this to make a modern ESM compatible version?

@kurmasz
Copy link

kurmasz commented Aug 20, 2024

The error below can be fixed by adding a failover to globalThis. I'm working on a PR now.

TypeError: Cannot set properties of undefined (setting 'showdown')
at https://cdn.jsdelivr.net/npm/showdown@latest/dist/showdown.min.js:2:72031
at https://cdn.jsdelivr.net/npm/showdown@latest/dist/showdown.min.js:2:72035

@kurmasz
Copy link

kurmasz commented Aug 20, 2024

This PR should fix the main problem: #1017

@KeithHenry
Copy link

This PR should fix the main problem: #1017

That's a patch for being able to run in ESM, but it still loads as a side effect attached to the globalThis.

Where @LeaVerou said:

I can't distribute libraries that leak into the global scope. 😞

For this to be consumed by ESM libraries it needs to load as a module:

import { showdown } from './showdown.esm.js';

// Scoped to module
const converter = new showdown.Converter();

// Must not leak into window or self
if(globalThis.showdown)
    throw new ('Global scope polluted',);

This allows my library to require import { showdown } from './v1/showdown.esm.js' and someone else's to load import { showdown } from './v2/showdown.esm.js' (or even import { showdown } from './unrelatedLibraryWithSimilarName.js') and not get nasty bugs depending on loading order.

@csisy
Copy link

csisy commented Nov 5, 2024

@KeithHenry I was just looking for an alternative. Not sure if it fits your needs, but markedjs/marked seems to be a good one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests