How to get the module URL for custom-element?
Answer: by defining the following attributes in cem-module-url:
srcwith the required URL or mapped module/resource specifiersliceto read the resolved absolute URL- optional
referrerto set the URL used for relative resolution and import-mapscopesmatching
referrer-selector only when resolution must enter one rendered
descendant DCE's complete local map context; a URL-valued referrer
changes the base and import-map scope matching without changing context.
Where to use cem-module-url?
Answer: to inject the library resources URL into generated HTML.
Import maps are used for defining the dependencies location on the web page level. This way dependencies could reside on different CDN path or even domains while in the code would be referenced by symbolic package name. See the mapped resource examples below.
The CEM runtime captures the page import map and resolves it through the
scope-aware CEM resolver. It does not use the JavaScript module containing
the runtime as the implicit import.meta.resolve() referrer.
Where NOT to use cem-module-url?
Answer:
- for relative or absolute
srcvalues oncem-element, which already resolve from the declaration base URL; resolve a mapped bare specifier first and pass its URL to a dynamic declaration - for getting/setting the page URL,
- see How to set page URL incustom-element?
See also
Scoped CSS imports and resource overrides
How to use external templates?
{ "imports": {
"lib-root": "./lib-dir/",
"lib-root/": "./lib-dir/",
"embed-lib": "./lib-dir/embed-lib.html",
"@epa-wg/cem-elements/": "../",
"@epa-wg/cem-elements/demo/lib-dir/Smiley.svg": "./lib-dir/Smiley.svg",
"@epa-wg/material": "../../custom-element/material/",
"demo-src-image": "./lib-dir/Smiley.svg?src=module",
"demo-referrer-image": "./lib-dir/Smiley.svg?referrer=default",
"demo-module-referrer": "./module-referrer/component.js"
},
"scopes": {
"./relative-referrer/": {
"demo-referrer-image": "./lib-dir/Smiley.svg?referrer=relative"
},
"./module-referrer/": {
"demo-referrer-image": "./confused.svg?referrer=module"
},
"https://referrer.example.test/absolute/": {
"demo-referrer-image": "./wc-square.svg?referrer=absolute"
} } }