@@ -115,13 +115,18 @@ compileJS ms foreigns prefix = do
115115
116116 where
117117
118− --codegenModule :: CF.Module CF.Ann -> m [J.JS]
119118 codegenModule m =
120− J.moduleToJs m $ (\js -> J.JSApp (J.JSFunction Nothing [] (J.JSBlock
121− [ J.JSVariableIntroduction "exports" (Just $ J.JSObjectLiteral [])
122− , J.JSRaw js
123− , J.JSReturn (J.JSVar "exports")
124− ])) []) <$> CF.moduleName m `M.lookup` foreigns
119+ let requiresForeign = not $ null (CF.moduleForeign m)
120+ in case CF.moduleName m `M.lookup` foreigns of
121+ Just js | not requiresForeign -> error "Found unnecessary foreign module"
122+ | otherwise -> J.moduleToJs m $ Just $
123+ J.JSApp (J.JSFunction Nothing [] $
124+ J.JSBlock [ J.JSVariableIntroduction "exports" (Just $ J.JSObjectLiteral [])
125+ , J.JSRaw js
126+ , J.JSReturn (J.JSVar "exports")
127+ ]) []
128+ Nothing | requiresForeign -> error "Foreign module missing"
129+ | otherwise -> J.moduleToJs m Nothing
125130
126131 generateMain :: P.Environment -> [J.JS] -> m [J.JS]
127132 generateMain env js = do