esm-dev / esm.sh

A fast, smart, & global CDN for modern(es2015+) web development.
https://esm.sh
MIT License
3.14k stars 151 forks source link

Does not work with shared instance and subdirectory #16

Open mizchi opened 3 years ago

mizchi commented 3 years ago

I found some patterns.

Example

preact case

import { h, render } from "https://esm.sh/preact";
import { useEffect } from "https://esm.sh/preact/hooks";

function App (){
  useEffect(() => {
     console.log("mounted");
  });
  return <div></div>
}

svelte case.

<script>
  import { onMount } from "svelte";
  onMount()
</script>

compiled svelte has import { ... } from "svelte/internal".

To be common to both, they have to share the same instance in its inside. esm.sh failed.

(skypack works in these cases)

ije commented 3 years ago

can you please post the svelte output?

mizchi commented 3 years ago

Source

<script>
    import { onMount } from "svelte"
    let name = 'world';
    onMount(() => {
        console.log('mounted');
    })
</script>
<h1>Hello {name}!</h1>

Output

/* App.svelte generated by Svelte v3.31.2 */
import {
    SvelteComponent,
    detach,
    element,
    init,
    insert,
    noop,
    safe_not_equal
} from "svelte/internal";

import { onMount } from "svelte";

function create_fragment(ctx) {
    let h1;

    return {
        c() {
            h1 = element("h1");
            h1.textContent = `Hello ${name}!`;
        },
        m(target, anchor) {
            insert(target, h1, anchor);
        },
        p: noop,
        i: noop,
        o: noop,
        d(detaching) {
            if (detaching) detach(h1);
        }
    };
}

let name = "world";

function instance($$self) {
    onMount(() => {
        console.log("mounted");
    });

    return [];
}

class App extends SvelteComponent {
    constructor(options) {
        super();
        init(this, options, instance, create_fragment, safe_not_equal, {});
    }
}

export default App;

I added rollup plugin to rewrite specifiers like svelte => https://esm.sh/svelte and svelte/internal https://esm.sh/svelte/internal and download it https://github.com/mizchi/uniroll/tree/master/packages/rollup-plugin-http-resolve

you can try here https://svelte.dev/repl/hello-world?version=3.31.2

hhun commented 1 year ago

@ije the main store of the library isn't shared between both dependencies. Is there a way to fix this?

ije commented 1 year ago

currenlty needs to declares the exports in package.json then esm.sh can split the code, for example:

{
  "name": "svelte",
  "exports": {
    "./internal": { "import": "...", "require": "...." }
  }
}

but i think i can add a workaround just for svelte

hhun commented 1 year ago

@ije Can you help me write a more specific example?

glitch-txs commented 1 year ago

@ije we have a library that's modularized into different packages. All packages share the same store, but this communication seems to get lost when importing the different packages with esm.sh

1951FDG commented 6 months ago

@ije Any update on this?

ije commented 6 months ago

does means "svelte" equals to epxort * from "svelte/internal"

ije commented 6 months ago

i think i have fixed it, but didn't deploy the patch yet

Screenshot 2024-04-23 at 20 32 55

the "svelte" will import "./internal" instaed of bundling it

ije commented 6 months ago

i'm still working on v136, will deploy the change soon

1951FDG commented 6 months ago

Hi there, will v136 also help/fix #741