docsifyjs / docsify

šŸƒ A magical documentation site generator.
https://docsify.js.org
MIT License
27.77k stars 5.68k forks source link

executeScript does not support ES modules #2010

Open sanand0 opened 1 year ago

sanand0 commented 1 year ago

Bug Report

Currently, executeScript: true does not run ES Modules and throws an Uncaught SyntaxError: Cannot use import statement outside a module.

Steps to reproduce

index.html:

<div id="app"></div>
<script>
  window.$docsify = {
    executeScript: true
  };
</script>

README.md:

<script type="module">
  import { rgb } from "https://cdn.skypack.dev/d3-color@3";
</script>

What is current behaviour

It throws an Uncaught SyntaxError: Cannot use import statement outside a module

What is the expected behaviour

Run the script as an ES module (or mention in the documentation that ES modules and imports are not supported.)

Other relevant information

Please create a reproducible sandbox

Edit 307qqv236

Mention the docsify version in which this bug was not present (if any)

docsify 4.13.0

trusktr commented 1 year ago

Indeed! The fix should be easy.

trusktr commented 1 year ago

Btw, for now, to emulate a type=module script like this one:

<script type="module" src="./main.js"></script>

you can write it like this:

<script>
const runMain = () => import('./main.js')
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", runMain);
else runMain();
</script>

The second is essentially what the first one is doing, and the first one is syntax sugar.

sanand0 commented 1 year ago

Thanks, @trusktr. Since I'm using ESM to simplify examples, I'll await the fix you mentioned. Very grateful! šŸ™