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 #16414

Merged
merged 9 commits into from
Jan 14, 2025

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.
@brianjhanson brianjhanson marked this pull request as ready for review January 13, 2025 16:48
@brianjhanson brianjhanson changed the base branch from 5.x to 5.6 January 13, 2025 16:49
@brianjhanson brianjhanson changed the title Improve the experience for using modern JS a bit Improve the experience for using modern JS Jan 13, 2025
@brandonkelly brandonkelly merged commit c90a6d6 into 5.6 Jan 14, 2025
@brandonkelly brandonkelly deleted the feature/improve-modern-js-support branch January 14, 2025 17:34
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.

2 participants