diff options
Diffstat (limited to 'src/PursLoader/Loader.purs')
-rw-r--r-- | src/PursLoader/Loader.purs | 44 |
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 @@ | |||
1 | module PursLoader.Loader | 1 | module PursLoader.Loader |
2 | ( Effects() | 2 | ( Effects() |
3 | , Effects_() | ||
3 | , loader | 4 | , loader |
4 | , loaderFn | 5 | , loaderFn |
5 | ) where | 6 | ) where |
6 | 7 | ||
7 | import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), bind, const, id, pure, unit) | 8 | import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), bind, const, id, pure, void, unit) |
8 | 9 | ||
9 | import Control.Apply ((*>)) | 10 | import Control.Apply ((*>)) |
10 | import Control.Alt ((<|>)) | ||
11 | import Control.Bind (join) | 11 | import Control.Bind (join) |
12 | import Control.Monad.Eff (Eff(), foreachE) | 12 | import Control.Monad.Eff (Eff(), foreachE) |
13 | import Control.Monad.Eff.Exception (Error(), error) | 13 | import Control.Monad.Eff.Console (CONSOLE()) |
14 | import Control.Monad.Eff.Exception (EXCEPTION(), Error(), error, message) | ||
14 | 15 | ||
15 | import Data.Array ((!!)) | 16 | import Data.Array ((!!)) |
16 | import Data.Bifunctor (lmap) | 17 | import Data.Bifunctor (lmap) |
17 | import Data.Either (Either(..), either) | 18 | import Data.Either (Either(..), either) |
18 | import Data.Foreign.Class (read) | 19 | import Data.Foreign.Class (read) |
19 | import Data.Function (Fn2(), mkFn2) | 20 | import Data.Function (Fn2(), mkFn2) |
20 | import Data.Maybe (Maybe(..), maybe) | 21 | import Data.Maybe (Maybe(), maybe) |
21 | import Data.Nullable (toMaybe) | 22 | import Data.Nullable (toMaybe) |
22 | import Data.String.Regex (Regex(), match, noFlags, regex) | 23 | import Data.String.Regex (Regex(), match, noFlags, regex) |
23 | 24 | ||
25 | import Node.Encoding (Encoding(UTF8)) | ||
26 | import Node.Process (stderr) | ||
27 | import Node.Stream (writeString) | ||
28 | |||
24 | import Unsafe.Coerce (unsafeCoerce) | 29 | import Unsafe.Coerce (unsafeCoerce) |
25 | 30 | ||
26 | import PursLoader.LoaderRef | 31 | import PursLoader.LoaderRef |
@@ -41,9 +46,11 @@ import PursLoader.Options (Options(..)) | |||
41 | import PursLoader.Path (dirname, relative) | 46 | import PursLoader.Path (dirname, relative) |
42 | import PursLoader.Plugin as Plugin | 47 | import PursLoader.Plugin as Plugin |
43 | 48 | ||
44 | type Effects eff = (loader :: Loader | eff) | 49 | type Effects eff = (console :: CONSOLE, err :: EXCEPTION | eff) |
50 | |||
51 | type Effects_ eff = Effects (loader :: Loader | eff) | ||
45 | 52 | ||
46 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit | 53 | loader :: forall eff. LoaderRef -> String -> Eff (Effects_ eff) Unit |
47 | loader ref source = do | 54 | loader 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 | ||
110 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) | 124 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects_ eff) Unit) |
111 | loaderFn = mkFn2 loader | 125 | loaderFn = mkFn2 loader |