diff options
author | eric <thul.eric@gmail.com> | 2016-03-12 14:56:34 -0500 |
---|---|---|
committer | eric <thul.eric@gmail.com> | 2016-03-12 14:56:34 -0500 |
commit | 7c6f8623b2448e9a371d6c7964493e365843004c (patch) | |
tree | 5079dcf661e96520e9b37562b47ab4d83bc7496a | |
parent | 55120f4502cb76e768f60654f3937db809df8ade (diff) | |
parent | cb2f4c886fd1057fbf0511b46cab3489123ced23 (diff) | |
download | purs-loader-7c6f8623b2448e9a371d6c7964493e365843004c.tar.gz purs-loader-7c6f8623b2448e9a371d6c7964493e365843004c.tar.zst purs-loader-7c6f8623b2448e9a371d6c7964493e365843004c.zip |
Merge pull request #42 from ethul/topic/stderr
Writes PureScript output to stderr
-rw-r--r-- | bower.json | 3 | ||||
-rw-r--r-- | docs/PursLoader/Loader.md | 12 | ||||
-rw-r--r-- | docs/PursLoader/Plugin.md | 2 | ||||
-rw-r--r-- | src/PursLoader/Loader.purs | 40 | ||||
-rw-r--r-- | src/PursLoader/Plugin.purs | 2 |
5 files changed, 40 insertions, 19 deletions
@@ -5,6 +5,7 @@ | |||
5 | "purescript-aff": "^0.13.0", | 5 | "purescript-aff": "^0.13.0", |
6 | "purescript-foreign": "^0.7.0", | 6 | "purescript-foreign": "^0.7.0", |
7 | "purescript-unsafe-coerce": "~0.1.0", | 7 | "purescript-unsafe-coerce": "~0.1.0", |
8 | "purescript-nullable": "~0.2.1" | 8 | "purescript-nullable": "~0.2.1", |
9 | "purescript-node-process": "~0.4.1" | ||
9 | } | 10 | } |
10 | } | 11 | } |
diff --git a/docs/PursLoader/Loader.md b/docs/PursLoader/Loader.md index bb02470..d05e3b7 100644 --- a/docs/PursLoader/Loader.md +++ b/docs/PursLoader/Loader.md | |||
@@ -3,19 +3,25 @@ | |||
3 | #### `Effects` | 3 | #### `Effects` |
4 | 4 | ||
5 | ``` purescript | 5 | ``` purescript |
6 | type Effects eff = (loader :: Loader | eff) | 6 | type Effects eff = (console :: CONSOLE, err :: EXCEPTION | eff) |
7 | ``` | ||
8 | |||
9 | #### `Effects_` | ||
10 | |||
11 | ``` purescript | ||
12 | type Effects_ eff = Effects (loader :: Loader | eff) | ||
7 | ``` | 13 | ``` |
8 | 14 | ||
9 | #### `loader` | 15 | #### `loader` |
10 | 16 | ||
11 | ``` purescript | 17 | ``` purescript |
12 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit | 18 | loader :: forall eff. LoaderRef -> String -> Eff (Effects_ eff) Unit |
13 | ``` | 19 | ``` |
14 | 20 | ||
15 | #### `loaderFn` | 21 | #### `loaderFn` |
16 | 22 | ||
17 | ``` purescript | 23 | ``` purescript |
18 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) | 24 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects_ eff) Unit) |
19 | ``` | 25 | ``` |
20 | 26 | ||
21 | 27 | ||
diff --git a/docs/PursLoader/Plugin.md b/docs/PursLoader/Plugin.md index 7a524da..9f9f852 100644 --- a/docs/PursLoader/Plugin.md +++ b/docs/PursLoader/Plugin.md | |||
@@ -3,7 +3,7 @@ | |||
3 | #### `Compile` | 3 | #### `Compile` |
4 | 4 | ||
5 | ``` purescript | 5 | ``` purescript |
6 | type Compile eff = Nullable Error -> DependencyGraph -> Eff eff Unit | 6 | type Compile eff = Nullable Error -> DependencyGraph -> String -> Eff eff Unit |
7 | ``` | 7 | ``` |
8 | 8 | ||
9 | #### `Context` | 9 | #### `Context` |
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 @@ | |||
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, unit, void) |
8 | 9 | ||
9 | import Control.Alt ((<|>)) | ||
10 | import Control.Bind (join) | 10 | import Control.Bind (join) |
11 | import Control.Monad.Eff (Eff(), foreachE) | 11 | import Control.Monad.Eff (Eff(), foreachE) |
12 | import Control.Monad.Eff.Exception (Error(), error) | 12 | import Control.Monad.Eff.Console (CONSOLE()) |
13 | import Control.Monad.Eff.Exception (EXCEPTION(), Error(), error, message) | ||
13 | 14 | ||
14 | import Data.Array ((!!)) | 15 | import Data.Array ((!!)) |
15 | import Data.Either (Either(..), either) | 16 | import Data.Either (Either(..), either) |
16 | import Data.Function (Fn2(), mkFn2) | 17 | import Data.Function (Fn2(), mkFn2) |
17 | import Data.Maybe (Maybe(..), maybe) | 18 | import Data.Maybe (maybe) |
18 | import Data.Nullable (toMaybe) | 19 | import Data.Nullable (toMaybe) |
19 | import Data.String.Regex (Regex(), match, noFlags, regex) | 20 | import Data.String.Regex (Regex(), match, noFlags, regex) |
20 | 21 | ||
22 | import Node.Encoding (Encoding(UTF8)) | ||
23 | import Node.Process (stderr) | ||
24 | import Node.Stream (writeString) | ||
25 | |||
21 | import Unsafe.Coerce (unsafeCoerce) | 26 | import Unsafe.Coerce (unsafeCoerce) |
22 | 27 | ||
23 | import PursLoader.LoaderRef | 28 | import PursLoader.LoaderRef |
@@ -34,9 +39,11 @@ import PursLoader.Debug (debug) | |||
34 | import PursLoader.Path (dirname, joinPath, relative) | 39 | import PursLoader.Path (dirname, joinPath, relative) |
35 | import PursLoader.Plugin as Plugin | 40 | import PursLoader.Plugin as Plugin |
36 | 41 | ||
37 | type Effects eff = (loader :: Loader | eff) | 42 | type Effects eff = (console :: CONSOLE, err :: EXCEPTION | eff) |
43 | |||
44 | type Effects_ eff = Effects (loader :: Loader | eff) | ||
38 | 45 | ||
39 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit | 46 | loader :: forall eff. LoaderRef -> String -> Eff (Effects_ eff) Unit |
40 | loader ref source = do | 47 | loader 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 | ||
101 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) | 115 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects_ eff) Unit) |
102 | loaderFn = mkFn2 loader | 116 | loaderFn = 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(..)) | |||
15 | import Data.Function (Fn4(), runFn4) | 15 | import Data.Function (Fn4(), runFn4) |
16 | import Data.Nullable (Nullable()) | 16 | import Data.Nullable (Nullable()) |
17 | 17 | ||
18 | type Compile eff = Nullable Error -> DependencyGraph -> Eff eff Unit | 18 | type Compile eff = Nullable Error -> DependencyGraph -> String -> Eff eff Unit |
19 | 19 | ||
20 | type Context eff = { compile :: Compile eff -> Eff eff Unit, options :: Options } | 20 | type Context eff = { compile :: Compile eff -> Eff eff Unit, options :: Options } |
21 | 21 | ||