Ok, the CDNs are different as I thougt and they can't be mixed.
So that means that you would need different versions of a web component (for AMD and ESM).
In fact you probably need 3 versions:
1) AMD (imports SDK classes from AMD CDN with require function)
2) ESM (imports SDK classes from local SDK with import @ArcGIS/core/...
3) ESM (imports SDK classes from ESM CDN with import https://js.arcgis.com/4.29/@arcgis/core/...
Then the user can take the appropriate version matching his web app.
I like the idea to set the imported SDK classes to the component class from the outsite. This way the component class is more independent.
I played around with the AMD version of my simple example and got it to work this way:
index.html
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>Simple mapping app</title>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>
<link rel="stylesheet" href="https://js.arcgis.com/4.29/esri/themes/light/main.css">
<script src="https://js.arcgis.com/4.29/"></script>
<script type="module" src="./main.js"></script>
</head>
<body>
<div id="viewDiv"></div>
</body>
</html>
main.js
import MapComponent from "./MapComponent.js";
require(["esri/Map", "esri/views/MapView"], function(Map, MapView) {
const map = new Map({
basemap: "osm"
});
const view = new MapView({
map: map,
center: [-118.805, 34.027],
zoom: 13,
container: "viewDiv"
});
const mapComponent = document.createElement("map-component");
mapComponent.setView(view);
view.ui.add(mapComponent, "top-right");
});
MapComponent.js
require(["esri/layers/GraphicsLayer", "esri/Graphic"], function(GraphicsLayer, Graphic) {
MapComponent.GraphicsLayer = GraphicsLayer;
MapComponent.Graphic = Graphic;
});
export default class MapComponent extends HTMLElement{
static GraphicsLayer = null;
static Graphic = null;
constructor(){
super();
this.shadow = this.attachShadow({ mode: 'open' });
}
setView(view){
this.view = view;
}
connectedCallback() {
let goToButton = document.createElement("div");
goToButton.setAttribute("style", "background-color:#d9dde0;padding:2px 6px;cursor:pointer");
goToButton.textContent = "Add Graphic";
this.shadow.appendChild(goToButton);
goToButton.addEventListener("click", () => {
this.addGraphic();
});
}
addGraphic(){
let point = {
type: "point",
longitude: -71.2643,
latitude: 42.0909
};
let markerSymbol = {
type: "simple-marker",
color: [226, 119, 40]
};
if(this.view && MapComponent.Graphic && MapComponent.GraphicsLayer){
let graphic = new MapComponent.Graphic({
geometry: point,
symbol: markerSymbol
});
let graphicsLayer = new MapComponent.GraphicsLayer();
graphicsLayer.add(graphic);
this.view.map.add(graphicsLayer);
this.view.goTo(graphic);
}
}
}
customElements.define("map-component", MapComponent);