sprae icon indicating copy to clipboard operation
sprae copied to clipboard

Will UMD builds support manual initialization without type="module"?

Open kitevi opened this issue 1 year ago • 6 comments

The v12 readme only shows ESM examples for manual init. I tried using the UMD build (sprae.umd.js) with a regular script tag for manual initialization, but sprae isn't available globally.

Will UMD builds support manual initialization (without type="module") in the future, or is ESM the only intended approach going forward?

kitevi avatar Oct 07 '25 23:10 kitevi

If you try latest v12 umd build it creates window.sprae instance if included as CDN. Please lmk more details if you're having a trouble.

dy avatar Oct 08 '25 00:10 dy

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>Sprae Example</title>
		<script src="sprae.umd.js"></script>
		<script>
			document.addEventListener("DOMContentLoaded", () => {
				const container = document.querySelector("#container");
				const state = window.sprae(container, { user: { name: "friend" } });
				state.user.name = "love";
			});
		</script>
	</head>
	<body>
		<div id="container" :if="user">
			Hello <span :text="user.name">there</span>.
		</div>
	</body>
</html>

Is this the wrong basic approach ?

kitevi avatar Oct 08 '25 22:10 kitevi

UMD automatically initializes document.body, so normally you don't need to manually init it, all spraeable nodes will be autoinitialized, even if added after.

If you need to update global app state, you can do <script>window.sprae(document.body, {...newProps})</script>.

If you need full manual control then yes, use ESM.

Do you still need UMD without autoinit? I wonder what's your specific case for that. I will have to think about configuring pattern, maybe there will not be autoinit.

dy avatar Oct 09 '25 02:10 dy

Sorry but what do you mean by full manual control ?

I don't really need anything I'm just a curious dev. Found sprae online, currently working on a jquery 1 legacy codebase and thinking about where the web is atm.

kitevi avatar Oct 09 '25 22:10 kitevi

Short summary how you can use sprae v12.

  1. UMD sync script, autostart on document.body
<script src="path/to/sprae.umd.js" defer start></script>
  1. UMD, manual start on any element with any initial state
<script src="path/to/sprae.umd.js"></script>
<script>let state = sprae.start(element, {...initialState})</script>
  1. ESM, manual init nodes
<script type="module">
import sprae from 'path/to/sprae.js'
const state = sprae(element, {...init})
</script>

Depending on your integration use-case you can pick any way. 1 is nojs alpinejs-like, 2 is oldschool like jquery, 3 is modern.

dy avatar Oct 09 '25 22:10 dy

Please check v12.

dy avatar Oct 10 '25 17:10 dy