1- /* globals __CLIENT_VERSION__, __REGISTERED_TEMPLATES_PLACEHOLDER__, __DEFAULT_RETRY_INTERVAL__, __DEFAULT_RETRY_LIMIT__, __DEFAULT_DISABLE_LOADER__, __DISABLE_LEGACY_TEMPLATES__, __EXTERNALS__ */
1+ /* globals __CLIENT_VERSION__, __REGISTERED_TEMPLATES_PLACEHOLDER__, __DEFAULT_RETRY_INTERVAL__, __DEFAULT_RETRY_LIMIT__, __DEFAULT_DISABLE_LOADER__, __DISABLE_LEGACY_TEMPLATES__, __EXTERNALS__, __IMPORTS__ */
22import { decode } from "@rdevis/turbo-stream" ;
33
44export function createOc ( oc ) {
@@ -71,6 +71,7 @@ export function createOc(oc) {
7171
7272 let registeredTemplates = __REGISTERED_TEMPLATES_PLACEHOLDER__ ;
7373 let externals = __EXTERNALS__ ;
74+ let imports = __IMPORTS__ ;
7475
7576 let registerTemplates = ( templates , overwrite ) => {
7677 templates = Array . isArray ( templates ) ? templates : [ templates ] ;
@@ -211,7 +212,9 @@ export function createOc(oc) {
211212 setAttribute ( dataRenderingAttribute , false ) ;
212213 setAttribute ( "data-version" , dataVersion ) ;
213214 setAttribute ( "data-id" , data . ocId ) ;
214- component . innerHTML = data . html ;
215+ if ( typeof data . html === "string" ) {
216+ component . innerHTML = data . html ;
217+ }
215218 // If the html contains <scripts> tags, innerHTML will not execute them.
216219 // So we need to do it manually.
217220 reanimateScripts ( component ) ;
@@ -242,7 +245,9 @@ export function createOc(oc) {
242245 isRequired ( "baseUrl" , baseUrl ) ;
243246 isRequired ( "name" , name ) ;
244247 if ( options . action ) {
245- baseUrl = `${ baseUrl } ~actions/${ options . action } /${ options . name } /${ options . version || "" } ` ;
248+ baseUrl = `${ baseUrl } ~actions/${ options . action } /${ options . name } /${
249+ options . version || ""
250+ } `;
246251 }
247252 let parameters = { ...ocConf . globalParameters , ...options . parameters } ;
248253 let data = options . action
@@ -392,6 +397,14 @@ export function createOc(oc) {
392397 } ,
393398 } ;
394399 } ) ( ) ;
400+ if ( Object . keys ( imports ) . length > 0 ) {
401+ $document . head . appendChild (
402+ Object . assign ( $document . createElement ( "script" ) , {
403+ type : "importmap" ,
404+ textContent : JSON . stringify ( { imports } ) ,
405+ } ) ,
406+ ) ;
407+ }
395408
396409 callback ( ) ;
397410
@@ -411,6 +424,34 @@ export function createOc(oc) {
411424 }
412425 } ;
413426
427+ const renderOc = ( apiResponse , callback ) => {
428+ const isEsm = ! ! apiResponse . data ?. component ?. esm ;
429+
430+ if ( isEsm ) {
431+ renderEsm ( apiResponse . data , callback ) ;
432+ } else {
433+ oc . render ( apiResponse . data , callback ) ;
434+ }
435+ } ;
436+
437+ const renderEsm = async ( data , callback ) => {
438+ try {
439+ const { _staticPath, _componentName, _componentVersion } =
440+ data . component . props ;
441+ window . oc . _esm = window . oc . _esm || { } ;
442+ window . oc . _esm [ `${ _componentName } @${ _componentVersion } ` ] = ( args ) => {
443+ return _staticPath + "public/" + args ;
444+ } ;
445+
446+ const { mount } = await import ( data . component . src ) ;
447+ mount ( data . element , data . component . props ) ;
448+ callback ( null ) ;
449+ } catch ( error ) {
450+ console . error ( "Error rendering ESM component" , error ) ;
451+ callback ( error ) ;
452+ }
453+ } ;
454+
414455 oc . render = ( compiledViewInfo , model , callback ) => {
415456 oc . ready ( ( ) => {
416457 // TODO: integrate with oc-empty-response-handler module
@@ -541,7 +582,8 @@ export function createOc(oc) {
541582 let template = apiResponse . template ;
542583 apiResponse . data . id = ocId ;
543584 apiResponse . data . element = element ;
544- oc . render ( template , apiResponse . data , ( err , html ) => {
585+
586+ renderOc ( apiResponse , ( err , html ) => {
545587 if ( err ) {
546588 callback (
547589 interpolate ( MESSAGES_ERRORS_RENDERING , apiResponse . href ) +
0 commit comments