aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/PursLoader/Loader.purs
diff options
context:
space:
mode:
Diffstat (limited to 'src/PursLoader/Loader.purs')
-rw-r--r--src/PursLoader/Loader.purs44
1 files changed, 29 insertions, 15 deletions
diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs
index d1068b6..e1b9e0f 100644
--- a/src/PursLoader/Loader.purs
+++ b/src/PursLoader/Loader.purs
@@ -1,26 +1,31 @@
1module PursLoader.Loader 1module PursLoader.Loader
2 ( Effects() 2 ( Effects()
3 , Effects_()
3 , loader 4 , loader
4 , loaderFn 5 , loaderFn
5 ) where 6 ) where
6 7
7import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), bind, const, id, pure, unit) 8import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), bind, const, id, pure, void, unit)
8 9
9import Control.Apply ((*>)) 10import Control.Apply ((*>))
10import Control.Alt ((<|>))
11import Control.Bind (join) 11import Control.Bind (join)
12import Control.Monad.Eff (Eff(), foreachE) 12import Control.Monad.Eff (Eff(), foreachE)
13import Control.Monad.Eff.Exception (Error(), error) 13import Control.Monad.Eff.Console (CONSOLE())
14import Control.Monad.Eff.Exception (EXCEPTION(), Error(), error, message)
14 15
15import Data.Array ((!!)) 16import Data.Array ((!!))
16import Data.Bifunctor (lmap) 17import Data.Bifunctor (lmap)
17import Data.Either (Either(..), either) 18import Data.Either (Either(..), either)
18import Data.Foreign.Class (read) 19import Data.Foreign.Class (read)
19import Data.Function (Fn2(), mkFn2) 20import Data.Function (Fn2(), mkFn2)
20import Data.Maybe (Maybe(..), maybe) 21import Data.Maybe (Maybe(), maybe)
21import Data.Nullable (toMaybe) 22import Data.Nullable (toMaybe)
22import Data.String.Regex (Regex(), match, noFlags, regex) 23import Data.String.Regex (Regex(), match, noFlags, regex)
23 24
25import Node.Encoding (Encoding(UTF8))
26import Node.Process (stderr)
27import Node.Stream (writeString)
28
24import Unsafe.Coerce (unsafeCoerce) 29import Unsafe.Coerce (unsafeCoerce)
25 30
26import PursLoader.LoaderRef 31import PursLoader.LoaderRef
@@ -41,9 +46,11 @@ import PursLoader.Options (Options(..))
41import PursLoader.Path (dirname, relative) 46import PursLoader.Path (dirname, relative)
42import PursLoader.Plugin as Plugin 47import PursLoader.Plugin as Plugin
43 48
44type Effects eff = (loader :: Loader | eff) 49type Effects eff = (console :: CONSOLE, err :: EXCEPTION | eff)
50
51type Effects_ eff = Effects (loader :: Loader | eff)
45 52
46loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit 53loader :: forall eff. LoaderRef -> String -> Eff (Effects_ eff) Unit
47loader ref source = do 54loader ref source = do
48 callback <- async ref 55 callback <- async ref
49 56
@@ -53,28 +60,35 @@ loader ref source = do
53 60
54 pluginContext.compile (compile callback) 61 pluginContext.compile (compile callback)
55 where 62 where
56 pluginContext :: Plugin.Context (Effects eff) 63 pluginContext :: Plugin.Context (Effects_ eff)
57 pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext 64 pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext
58 65
59 compile :: AsyncCallback eff -> Plugin.Compile (Effects eff) 66 compile :: AsyncCallback (Effects eff) -> Plugin.Compile (Effects_ eff)
60 compile callback error' { srcMap, ffiMap, graph } = do 67 compile callback error' { srcMap, ffiMap, graph, output } = do
61 clearDependencies ref 68 clearDependencies ref
62 69
63 either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name 70 either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name
64 71
65 addDependency ref (resourcePath ref) 72 addDependency ref (resourcePath ref)
66 73
67 either (\err -> callback (toMaybe error' <|> Just err) "") id 74 void $ writeString stderr UTF8 output (pure unit)
75
76 maybe (pure unit) (\a -> void $ writeString stderr UTF8 (message a) (pure unit)) (toMaybe error')
77
78 either (const $ callback (pure fixedError) "") id
68 (handle <$> name <*> dependencies <*> exports) 79 (handle <$> name <*> dependencies <*> exports)
69 where 80 where
70 handle :: String -> Array String -> String -> Eff (Effects eff) Unit 81 fixedError :: Error
82 fixedError = error "PureScript compilation has failed."
83
84 handle :: String -> Array String -> String -> Eff (Effects_ eff) Unit
71 handle name' deps res = do 85 handle name' deps res = do
72 debug ("Adding PureScript transitive dependencies for " ++ name') 86 debug ("Adding PureScript transitive dependencies for " ++ name')
73 addTransitive name' 87 addTransitive name'
74 foreachE deps addTransitive 88 foreachE deps addTransitive
75 debug "Generated loader result" 89 debug "Generated loader result"
76 debug res 90 debug res
77 callback (toMaybe error') res 91 callback (const fixedError <$> toMaybe error') res
78 92
79 exports :: Either Error String 93 exports :: Either Error String
80 exports = (\a b -> "module.exports = require('" ++ a ++ "')['" ++ b ++ "'];") <$> path <*> name 94 exports = (\a b -> "module.exports = require('" ++ a ++ "')['" ++ b ++ "'];") <$> path <*> name
@@ -82,10 +96,10 @@ loader ref source = do
82 dependencies :: Either Error (Array String) 96 dependencies :: Either Error (Array String)
83 dependencies = name >>= Plugin.dependenciesOf graph 97 dependencies = name >>= Plugin.dependenciesOf graph
84 98
85 addTransitive :: String -> Eff (Effects eff) Unit 99 addTransitive :: String -> Eff (Effects_ eff) Unit
86 addTransitive dep = addDep (Plugin.get srcMap dep) *> addDep (Plugin.get ffiMap dep) 100 addTransitive dep = addDep (Plugin.get srcMap dep) *> addDep (Plugin.get ffiMap dep)
87 where 101 where
88 addDep :: Maybe String -> Eff (Effects eff) Unit 102 addDep :: Maybe String -> Eff (Effects_ eff) Unit
89 addDep = maybe (pure unit) (addDependency ref) 103 addDep = maybe (pure unit) (addDependency ref)
90 104
91 name :: Either Error String 105 name :: Either Error String
@@ -107,5 +121,5 @@ loader ref source = do
107 resourceDir :: String 121 resourceDir :: String
108 resourceDir = dirname (resourcePath ref) 122 resourceDir = dirname (resourcePath ref)
109 123
110loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) 124loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects_ eff) Unit)
111loaderFn = mkFn2 loader 125loaderFn = mkFn2 loader