-
-
Notifications
You must be signed in to change notification settings - Fork 825
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) Define import-maps for ECMAScript Modules (OOP) #26309
Conversation
…efault civicrm-core mappings".
(a) I don't really care about customizing `esm_loader` via web UI on a day-to-day basis. It's not terrible that the web UI has the setting, but that's not the main point. So why have a setting? (b) When/if some CMS or CMS-addon causes a break in ESM loading, the site-builder should be able to swap loaders and work-around the problem. Neither loader is invicible, but they have some complementary failure-modes. Compare: - If the conflict is two browser-level "importmap"s, then switching Civi to "importmap-shim" (`esm.loader.shim`) should work-around that. - If the conflict is two copies of "es-module-shims.js", then switching to basic "importmap" (`esm.loader.browser`) should work-around that. (c) I went back/forth on whether the configuration should be a constant (`CIVICRM_ESM_LOADER`), setting (`esm_loader`), or something even more esoteric. In the event of a failure/conflict, settings provide the most media through which the site-builder can enable a work-around, eg - Edit `civicrm.settings.php`, or - Edit `/etc/civicrm.settings.d`, or - Use CLI like `cv vset` or `drush cvapi` - Use browser console like `CRM.api3` or `CRM.api4` - Use REST API (d) When/if there's a conflict, the long-term fix will (probably) be implementation of another loader (`esm.loader.drupal` or `esm.loader.wp`) based on some future API. At that point, we would need to change the default for that environment (*without changing defaults for anyone else*). Hence the support for dynamic defaults (a la `prevNextBackend` or `assetCache`).
These flags were drafted when there was single "loader" class -- the idea was that a UF might toggle these flags. However: * The branch now supports swapping the "loader" class entirely. * It's easier to read the class if you don't have so many flags. * We don't have a real UF contract to compare against, so the flags are a bit speculative.
(Standard links)
|
Cool @totten - I assume it's on your radar to add a |
We were previously discussing the edge-cases for what happens if a future CMS or CMS-plugin inserts an
I tested with Firefox v113.0.2, Chromium 113.0.5672, and es-module-shims v1.7.2. As predicted, you can provoke conflicts - but they can be resolved with opposite settings, e.g.
Yeah, I think a mixin is a good idea. We can do that as a follow-up. |
@totten good sleuthing and conflict-provocation. At this stage since all conflicts are theoretical, I'm satisfied simply to know that they all have a theoretical resolution. |
I think this PR is in a good state to merge. It's documented, has tests, and Tim has given it a good bit of kicking including some light CMS warfare. The code looks good to me. Giving |
I'd like to recap an issue that was hinted in the comments -- and we've discussed a few times.
In discussions with Coleman/Eileen/Seamus, the "Smart Loading (Implicit)" DX seems to be agreed. import _ from 'lodash/lodash.js'; We can see the implementation evolving in three periods;
|
Overview
With ECMAScript Modules (ESMs), one
*.js
file can import another*.js
file. Theimport
statement may use a physical-path or a logical-path. Logical-paths are easier to read, more portable, and more maintainable. This branch adds support for logical-paths.To try it out, install the esmdemo extension and open page
civicrm/esmdemo
.(This is the second part of https://lab.civicrm.org/dev/core/-/issues/4279. It builds on #26195. It is an alternative to #26197 which has a different hook signature.)
(Analogy: ESM import-maps serve roughly the same purpose as PHP auto-loaders. They allow you to load other modules without knowing the exact file-path.)
Before
The basic ESM support (#26195) allows you to use
import
- but only with a physical path.After
You may
import
a*.js
file with a logical-path like:To do so, you must declare the logical-path as part of the "import map":
The
$importMap
is an object. It currently accepts registrations as ext-relative-paths. This allows the contract to be the same while leaving some flexibility in how we implement it; e.g.Technical Details
In the attached
*.md
file, you will find discussion about how import-maps should work from a few different perspectives:Comments
This is currently marked draft for a few reasons:I need to write+test an example consumer as part of https://github.com/totten/esmdemo/It needs some phpunit test-coverage.I'd also like to take a stab using the shim for older browsers.After we have some working examples, we may want to talk more about the namespace conventions. (What makes for a good or bad import-map?)