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

Improve the experience for using modern JS a bit #16414

Draft
wants to merge 3 commits into
base: 5.x
Choose a base branch
from

Conversation

brianjhanson
Copy link
Contributor

Adds two methods to the View class.

registerScriptWithVars

Mirrors registerJsWithVars but for registerScript because the JS method doesn't allow you to add type="module" to your script tag. This just makes it a little easier to deal with module JS in PHP

$view->registerScriptWithVars(fn($refHandles) => <<<JS
	import {setLocalizedRefHandles} from '@craftcms/ckeditor';
	setLocalizedRefHandles($refHandles);
JS, [$refHandles], View::POS_END, ['type' => 'module']);

registerJsImport

Allows registering key value pairs for a "module specifier map" to be injected via a <script type="importmap"> tag. Documents are only allowed to have a single import map at the moment, so we need a way to collect all of those specified by Craft + plugins and output them into a single script tag. The import map must also be declared before any script tags, so we add it before any scripts registered with the View::POS_HEAD position.

More on import maps can be found here

Mirrors `registerJsWithVars` but outputs a script tag instead allowing the use of `type=module`
Allows registerring JS module imports for Es module import maps.
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.

1 participant