walkermatt / ol-layerswitcher

Layer control for OpenLayers
MIT License
384 stars 176 forks source link

Problem with Rails Stimulus controller use #491

Open MtnBiker opened 1 year ago

MtnBiker commented 1 year ago

Rails Stimulus doesn't deal with ol/control/Control, therefore going back to old style which seems to work.

Using node_modules;

import ol from 'openlayers/dist/ol-debug.js'

and then have to use for example var baseLayers = new ol.layer.Group

But import LayerSwitcher from 'ol-layerswitcher/dist/ol-layerswitcher.js'

has this error

 ✘ [ERROR] Could not resolve "ol/control/Control"

     node_modules/ol-layerswitcher/dist/ol-layerswitcher.js:2:97:
       2 │ ...rts = factory(require('ol/control/Control'), require('ol/Observa...
         ╵                          ~~~~~~~~~~~~~~~~~~~~

   You can mark the path "ol/control/Control" as external to exclude it from the bundle, which will remove this error. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.

 ✘ [ERROR] Could not resolve "ol/Observable"

     node_modules/ol-layerswitcher/dist/ol-layerswitcher.js:2:128:
       2 │ ...ntrol/Control'), require('ol/Observable'), require('ol/layer/Gro...
         ╵                             ~~~~~~~~~~~~~~~

   You can mark the path "ol/Observable" as external to exclude it from the bundle, which will remove this error. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.

 ✘ [ERROR] Could not resolve "ol/layer/Group"

     node_modules/ol-layerswitcher/dist/ol-layerswitcher.js:2:154:
       2 │ ...l/Control'), require('ol/Observable'), require('ol/layer/Group')) :
         ╵                                                   ~~~~~~~~~~~~~~~~

   You can mark the path "ol/layer/Group" as external to exclude it from the bundle, which will remove this error. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.

Any work arounds or? LayerSwitcher is what I need for my app. Thank you

walkermatt commented 1 year ago

It looks as though you're using ES Modules to import OpenLayers and the LayerSwitcher but when imported ol-layerswitcher thinks you're using CommonJS hence the attempt to use require.

Do you know why Rails Stimulus doesn't support import Map from 'ol/Map'; etc?