aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/PursLoader
diff options
context:
space:
mode:
Diffstat (limited to 'src/PursLoader')
-rw-r--r--src/PursLoader/Loader.purs40
-rw-r--r--src/PursLoader/Plugin.purs2
2 files changed, 28 insertions, 14 deletions
diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs
index 402e805..c50c63c 100644
--- a/src/PursLoader/Loader.purs
+++ b/src/PursLoader/Loader.purs
@@ -1,23 +1,28 @@
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, unit, void)
8 9
9import Control.Alt ((<|>))
10import Control.Bind (join) 10import Control.Bind (join)
11import Control.Monad.Eff (Eff(), foreachE) 11import Control.Monad.Eff (Eff(), foreachE)
12import Control.Monad.Eff.Exception (Error(), error) 12import Control.Monad.Eff.Console (CONSOLE())
13import Control.Monad.Eff.Exception (EXCEPTION(), Error(), error, message)
13 14
14import Data.Array ((!!)) 15import Data.Array ((!!))
15import Data.Either (Either(..), either) 16import Data.Either (Either(..), either)
16import Data.Function (Fn2(), mkFn2) 17import Data.Function (Fn2(), mkFn2)
17import Data.Maybe (Maybe(..), maybe) 18import Data.Maybe (maybe)
18import Data.Nullable (toMaybe) 19import Data.Nullable (toMaybe)
19import Data.String.Regex (Regex(), match, noFlags, regex) 20import Data.String.Regex (Regex(), match, noFlags, regex)
20 21
22import Node.Encoding (Encoding(UTF8))
23import Node.Process (stderr)
24import Node.Stream (writeString)
25
21import Unsafe.Coerce (unsafeCoerce) 26import Unsafe.Coerce (unsafeCoerce)
22 27
23import PursLoader.LoaderRef 28import PursLoader.LoaderRef
@@ -34,9 +39,11 @@ import PursLoader.Debug (debug)
34import PursLoader.Path (dirname, joinPath, relative) 39import PursLoader.Path (dirname, joinPath, relative)
35import PursLoader.Plugin as Plugin 40import PursLoader.Plugin as Plugin
36 41
37type Effects eff = (loader :: Loader | eff) 42type Effects eff = (console :: CONSOLE, err :: EXCEPTION | eff)
43
44type Effects_ eff = Effects (loader :: Loader | eff)
38 45
39loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit 46loader :: forall eff. LoaderRef -> String -> Eff (Effects_ eff) Unit
40loader ref source = do 47loader ref source = do
41 callback <- async ref 48 callback <- async ref
42 49
@@ -46,25 +53,32 @@ loader ref source = do
46 53
47 pluginContext.compile (compile callback) 54 pluginContext.compile (compile callback)
48 where 55 where
49 pluginContext :: Plugin.Context (Effects eff) 56 pluginContext :: Plugin.Context (Effects_ eff)
50 pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext 57 pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext
51 58
52 compile :: AsyncCallback eff -> Plugin.Compile (Effects eff) 59 compile :: AsyncCallback (Effects eff) -> Plugin.Compile (Effects_ eff)
53 compile callback error' graph = do 60 compile callback error' graph output = do
54 either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name 61 either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name
55 62
56 addDependency ref (resourcePath ref) 63 addDependency ref (resourcePath ref)
57 64
58 either (\err -> callback (toMaybe error' <|> Just err) "") id 65 void $ writeString stderr UTF8 output (pure unit)
66
67 maybe (pure unit) (\a -> void $ writeString stderr UTF8 (message a) (pure unit)) (toMaybe error')
68
69 either (const $ callback (pure fixedError) "") id
59 (handle <$> name <*> dependencies <*> exports) 70 (handle <$> name <*> dependencies <*> exports)
60 where 71 where
61 handle :: String -> Array String -> String -> Eff (Effects eff) Unit 72 fixedError :: Error
73 fixedError = error "PureScript compilation has failed."
74
75 handle :: String -> Array String -> String -> Eff (Effects_ eff) Unit
62 handle name' deps res = do 76 handle name' deps res = do
63 debug ("Adding PureScript dependencies for " ++ name') 77 debug ("Adding PureScript dependencies for " ++ name')
64 foreachE deps (addDependency ref) 78 foreachE deps (addDependency ref)
65 debug "Generated loader result" 79 debug "Generated loader result"
66 debug res 80 debug res
67 callback (toMaybe error') res 81 callback (const fixedError <$> toMaybe error') res
68 82
69 exports :: Either Error String 83 exports :: Either Error String
70 exports = 84 exports =
@@ -98,5 +112,5 @@ loader ref source = do
98 re :: Regex 112 re :: Regex
99 re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } 113 re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true }
100 114
101loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) 115loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects_ eff) Unit)
102loaderFn = mkFn2 loader 116loaderFn = mkFn2 loader
diff --git a/src/PursLoader/Plugin.purs b/src/PursLoader/Plugin.purs
index c798c83..8bb53be 100644
--- a/src/PursLoader/Plugin.purs
+++ b/src/PursLoader/Plugin.purs
@@ -15,7 +15,7 @@ import Data.Either (Either(..))
15import Data.Function (Fn4(), runFn4) 15import Data.Function (Fn4(), runFn4)
16import Data.Nullable (Nullable()) 16import Data.Nullable (Nullable())
17 17
18type Compile eff = Nullable Error -> DependencyGraph -> Eff eff Unit 18type Compile eff = Nullable Error -> DependencyGraph -> String -> Eff eff Unit
19 19
20type Context eff = { compile :: Compile eff -> Eff eff Unit, options :: Options } 20type Context eff = { compile :: Compile eff -> Eff eff Unit, options :: Options }
21 21