@@ -6,16 +6,21 @@ import Ace (Annotation)
66import Control.Monad.Except (runExceptT )
77import Data.Array (fold )
88import Data.Array as Array
9- import Data.Either (Either (..))
9+ import Data.Either (Either (..), either )
1010import Data.Foldable (for_ , oneOf )
1111import Data.FoldableWithIndex (foldMapWithIndex )
1212import Data.Maybe (Maybe (..), fromMaybe , isNothing )
1313import Data.Symbol (SProxy (..))
14+ import Data.String as String
15+ import Data.String (Pattern (..))
16+ import Data.String.Regex as Regex
17+ import Data.String.Regex.Flags as RegexFlags
1418import Effect (Effect )
1519import Effect.Aff (Aff , makeAff )
1620import Effect.Aff as Aff
1721import Effect.Class.Console (error )
1822import Effect.Uncurried (EffectFn3 , runEffectFn3 )
23+ import Partial.Unsafe (unsafeCrashWith )
1924import Halogen as H
2025import Halogen.HTML as HH
2126import Halogen.HTML.Events as HE
@@ -187,7 +192,17 @@ component = H.mkComponent
187192 _ <- H .query _editor unit $ H .tell $ Editor.SetAnnotations anns
188193 pure unit
189194 let
190- eventData = { code: js }
195+ importRegex :: Regex.Regex
196+ importRegex = either (\_ -> unsafeCrashWith " Invalid regex" ) identity
197+ $ Regex .regex """ ^import (.+) from "../([^"]+)";$""" RegexFlags .noFlags
198+ replacement = " import $1 from \" " <> Config .loaderUrl <> " /$2\" ;"
199+ codeWithRemappedImports = js
200+ # String .split (Pattern " \n " )
201+ # map (Regex .replace importRegex replacement)
202+ # String .joinWith " \n "
203+
204+ -- Actually call the `main` function
205+ eventData = { code: codeWithRemappedImports <> " \n\n " <> " main();" }
191206 H .liftEffect teardownIFrame
192207 H .liftAff $ makeAff \f -> do
193208 runEffectFn3 setupIFrame eventData (f (Right unit)) (f (Left $ Aff .error " Could not load iframe" ))
0 commit comments