esm.sh icon indicating copy to clipboard operation
esm.sh copied to clipboard

Does not work with shared instance and subdirectory

Open mizchi opened this issue 4 years ago • 11 comments

I found some patterns.

  • preact & preact/hooks
  • svelte & svelte/internal

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)

mizchi avatar Jan 16 '21 10:01 mizchi

can you please post the svelte output?

ije avatar Jan 16 '21 11:01 ije

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

mizchi avatar Jan 18 '21 05:01 mizchi

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

hhun avatar Sep 23 '23 07:09 hhun

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

ije avatar Sep 23 '23 07:09 ije

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

hhun avatar Sep 23 '23 08:09 hhun

@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

glitch-txs avatar Sep 23 '23 09:09 glitch-txs

@ije Any update on this?

1951FDG avatar Apr 22 '24 17:04 1951FDG

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

ije avatar Apr 23 '24 11:04 ije

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 avatar Apr 23 '24 12:04 ije

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

ije avatar Apr 23 '24 12:04 ije

Hi there, will v136 also help/fix #741

1951FDG avatar Apr 23 '24 14:04 1951FDG