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 js.Batch #12641

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Add js.Batch #12641

wants to merge 1 commit into from

Conversation

bep
Copy link
Member

@bep bep commented Jul 5, 2024

js.Batch (new template func) takes an ID and a store (e.g. site.Store) and returns aBatcher instance:

type Batcher interface {
	UseScript(id string) BatcherScript
	UseScriptMany(id string) BatcherScriptMany
	Build() (*Package, error)
}

Benefits

  • Code splitting
  • UseScriptMany allows creation of many JS modules and instances with coordinated instantiation in a user defined callback function. Typical use case is React/JSX components where the user provided callback function could be set up as:
import * as ReactDOM from 'react-dom/client';
import * as React from 'react';

export default function Callback(modules) {
	for (const module of modules) {
		for (const instance of module.instances) {
			/* This is a convention in this project. */
			let elId = `${module.id}-${instance.id}`;
			let el = document.getElementById(elId);
			if (!el) {
				console.warn(`Element with id ${elId} not found`);
				continue;
			}
			const root = ReactDOM.createRoot(el);
			const reactEl = React.createElement(module.mod, instance.params);
			root.render(reactEl);
		}
	}
}

Limitations

  • There is a import order issue with ESBuild's current implementation, see this issue

Fixes #12626

@bep bep mentioned this pull request Jul 5, 2024
@bep bep force-pushed the feat/jsbatch-12626 branch 3 times, most recently from 8b9f6b9 to cc76968 Compare July 6, 2024 10:20
@bep bep changed the title Add js.Bundle Add js.Batch Sep 13, 2024
@bep bep force-pushed the feat/jsbatch-12626 branch 2 times, most recently from 14ea27d to bb0ddae Compare September 18, 2024 09:49
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

Successfully merging this pull request may close these issues.

Add js.Builder
2 participants