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

dev/core#4279 - Add support for ECMAScript Modules (ESM, part 1) #26195

Merged
merged 5 commits into from
May 11, 2023

Conversation

totten
Copy link
Member

@totten totten commented May 11, 2023

Overview

This adds support for loading ECMAScript 6 Modules (ESM's). ESMs are Javascript files with additional support for the keywords import and export. Basic ESM support has been provided by most web-browsers since ~2017.

(This is the first part of https://lab.civicrm.org/dev/core/-/issues/4279. The second part will be a subsequent PR.)

Before

You can add basic Javascript files:

Civi::resources()->addScript('doStuff();');
Civi::resources()->addScriptFile('my_extension', 'js/foo.js');
Civi::resources()->addScriptUrl('https://example.com/bar.js');

But this will not work with ESMs.

After

You can use the addModule() methods for ESMs:

Civi::resources()->addModule('import doStuff from \'./do-stuff.js\'; doStuff();');
Civi::resources()->addModuleFile('my_extension', 'js/foo.js');
Civi::resources()->addModuleUrl('https://example.com/bar.js');

Additionally, since an ESM is a special kind of Javascript file, you can also use the addScript() method. However, this requires the flag ['esm' => TRUE].

Civi::resources()->addScript('import doStuff from \'./do-stuff.js\'; doStuff();', ['esm' => TRUE]);
Civi::resources()->addScriptFile('my_extension', 'js/foo.js', ['esm' => TRUE]);
Civi::resources()->addScriptUrl('https://example.com/bar.js', ['esm' => TRUE]);

Comments

I've included some basic unit-tests for addModule(), etc.

To see them in action, you can use this example extension: https://github.com/totten/esmdemo/

@civibot
Copy link

civibot bot commented May 11, 2023

No issue was found matching the number given in the pull request title. Please check the issue number.

@civibot
Copy link

civibot bot commented May 11, 2023

(Standard links)

@civibot civibot bot added the master label May 11, 2023
@colemanw
Copy link
Member

This seems straightforward enough for @totten to merge-on-pass once he's happy with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants