Documentation

ShimLoader extends AutoService
in package
implements EventSubscriberInterface uses BasicLoaderTrait

The ShimLoader leverages `es-module-shims` for loading ECMAScript Modules (ESM's).

Tags
link

ShimLoader is very similar to BrowserLoader. It adds more backward compatibility (e.g. Safari 10 vs 16; Firefox 60 vs 108) and more features (e.g. "JSON Modules"), but it also incurs more runtime overhead.

ShimLoader works by:

  1. Adding the extra es-module-shims.js file. +<script async src="...../es-module-shims/dist/es-module-shims.js">
  2. Swapping HTML tags to prefer shim-loading. -<script type='importmap'> +<script type='importmap-shim'> -<script type='module'> +<script type='module-shim'>

There are a few different modes with trade-offs for performance, consistency, and compatibility. The methods createFastShim() and createSlowShim() have some notes about the trade-offs.

For a fuller description of this mechanism, see the neighboring README.

see
BrowserLoader
see

./README.md

Table of Contents

Interfaces

EventSubscriberInterface

Properties

$importMap  : ImportMap
$required  : bool
Do we need to send an import-map for the current page-view?
$scriptTypes  : array<string|int, mixed>
List of supported script types.

Methods

createFashShim()  : ShimLoader
Load the importmap with `es-module-shims`. Allow it to auto-detect browser support for ESM.
createSlowShim()  : ShimLoader
In this flavor, we use `es-module-shims`. We force it to use "shim mode".
getSubscribedEvents()  : array<string|int, mixed>
onRegionRender()  : void
Listen to 'civi.region.render[html-header]'.
onUseModule()  : void
Receive a notification that an ESM is being used.
renderModule()  : string
buildImportMap()  : array<string|int, mixed>
renderImportMap()  : string
renderModuleScript()  : string
renderModuleUrl()  : string

Properties

$required

Do we need to send an import-map for the current page-view?

protected bool $required = FALSE

For the moment, we figure this dynamically -- based on whether any "esm" scripts have been added. During the early stages (where ESMs aren't in widespread use), this seems safer. However, in the future, we might find some kind of race (e.g. where the system renders "" before it decides on a specific "<script type=module"> to load. If that edge-case happens, then it's probably fair to switch this default ($required=TRUE).

$scriptTypes

List of supported script types.

protected array<string|int, mixed> $scriptTypes

Ex: ['importmap' => 'importmap-shim', 'module' => 'module-shim'];

Methods

createFashShim()

Load the importmap with `es-module-shims`. Allow it to auto-detect browser support for ESM.

public static createFashShim() : ShimLoader

For browsers that support ESM, this should allow faster execution. However, there may be small variations or missing features in specific browser implementations.

Tags
service

esm.loader.shim-fast

Return values
ShimLoader

createSlowShim()

In this flavor, we use `es-module-shims`. We force it to use "shim mode".

public static createSlowShim() : ShimLoader

This should provide the most consistent functionality across browser implementations, but there may be a performance penalty.

Tags
service

esm.loader.shim-slow

Return values
ShimLoader

getSubscribedEvents()

public static getSubscribedEvents() : array<string|int, mixed>
Return values
array<string|int, mixed>

onRegionRender()

Listen to 'civi.region.render[html-header]'.

public onRegionRender(CRM_Core_Region $region) : void

If there are any active "module"s on this page, then output the "import-map".

Parameters
$region : CRM_Core_Region

onUseModule()

Receive a notification that an ESM is being used.

public onUseModule(array<string|int, mixed> &$snippet) : void
Parameters
$snippet : array<string|int, mixed>

The module resource being rendered, as per "CollectionInterface::add()". Ex: ['type' => 'scriptUrl', 'scriptUrl' => 'https://example.com/foo.js', 'esm' => TRUE, 'region' => 'page-footer']

Tags
see
CRM_Core_Resources_CollectionTrait::add()

renderModule()

public renderModule(array<string|int, mixed> $snippet) : string
Parameters
$snippet : array<string|int, mixed>

The module resource being rendered, as per "CollectionInterface::add()". Ex: ['type' => 'scriptUrl', 'scriptUrl' => 'https://example.com/foo.js', 'esm' => TRUE]

Tags
see
CRM_Core_Resources_CollectionInterface::add()
Return values
string

HTML

renderImportMap()

protected renderImportMap(array<string|int, mixed> $importMap) : string
Parameters
$importMap : array<string|int, mixed>
Tags
inheritDoc
Return values
string

renderModuleScript()

protected renderModuleScript(array<string|int, mixed> $snippet) : string
Parameters
$snippet : array<string|int, mixed>
Tags
inheritDoc
Return values
string

renderModuleUrl()

protected renderModuleUrl(array<string|int, mixed> $snippet) : string
Parameters
$snippet : array<string|int, mixed>
Tags
inheritDoc
Return values
string

        
On this page

Search results