Hello all,
Since a few versions, we have a support for importmap in XWiki (see the doc).
Currently, the only thing developers can do is to define mapping betwteen module ids, and webjar references.
But, for anything to be actually loaded in the browser, developers need to manually import the module from another piece of javascript (i.e., use import "mymoduleid" in some script).
While for some modules, we definitely want to wait for code to import it before load (e.g., not loading Vue when no code in the page needs it).
For other use cases, it is interesting to force the module to be loaded eagerly.
For instance:
- a module registering client-side components (proposal for this topic coming soon).
- a module register polyfills to support older browsers
As a reminder, the current format to declared importmaps from webjars is:
<xwiki.extension.javascript.modules.importmap>{
"vue": "org.webjars.npm:vue/dist/vue.runtime.esm-browser.prod.js"
}</xwiki.extension.javascript.modules.importmap>
And this translates to the following script element added to the head of the pages:
<script type="importmap">{
"imports":{
"vue":"\/xwiki\/webjars\/wiki%3Axwiki\/vue\/3.5.23\/dist\/vue.runtime.esm-browser.prod.js?r=1"
}
}</script>
If vue was marked as preloaded, it would be
<script type="importmap">{
"imports":{
"vue":"\/xwiki\/webjars\/wiki%3Axwiki\/vue\/3.5.23\/dist\/vue.runtime.esm-browser.prod.js?r=1"
}
}</script>
<!-- The module is loaded eagerly. -->
<script type="module" src="/xwiki/webjars/wiki%3Axwiki/vue/3.5.23/dist/vue.runtime.esm-browser.prod.js?r=1"></script>
Option 1: special prefix
An easy option is to simply introduce a special character. I have in mind !.
When an entry value in the importmap starts by !, it is loaded eagerly. I’m proposing to set it as the value because is not a valid groupId character, therefore not a breaking change.
Example:
<xwiki.extension.javascript.modules.importmap>{
"vue": "!org.webjars.npm:vue/dist/vue.runtime.esm-browser.prod.js"
}</xwiki.extension.javascript.modules.importmap>
Pros:
- Very easy to use as a developer.
Cons:
- This could become a limitation if we want to introduce more concepts to the importmap later.
Option 2: introduce more structure for special cases
Keep support for a simple string for basic cases, but also allow for an object for advanced cases
Example:
<xwiki.extension.javascript.modules.importmap>{
"vue": {
"webjarPath": "org.webjars.npm:vue/dist/vue.runtime.esm-browser.prod.js",
"eager": true
}
}</xwiki.extension.javascript.modules.importmap>
Cons:
- More verbose.
Pros:
- Easier to extends with more concepts over time (e.g., concepts related to resource load priority could become important to fine tune page rendering time).
Conclusion
WDYT? Thanks