aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authoreric thul <thul.eric@gmail.com>2016-03-06 11:47:39 -0500
committereric thul <thul.eric@gmail.com>2016-03-06 11:47:39 -0500
commit1c49c88ed60341341ab2b82aba738e8fc1179941 (patch)
tree7e9aa26412c4b907e6432a879dd4b5fd5634b687
parent17f22f868b851e50081844562627a7a8a414dcaa (diff)
downloadpurs-loader-1c49c88ed60341341ab2b82aba738e8fc1179941.tar.gz
purs-loader-1c49c88ed60341341ab2b82aba738e8fc1179941.tar.zst
purs-loader-1c49c88ed60341341ab2b82aba738e8fc1179941.zip
Writes PureScript output to stderr
Resolves #40
-rw-r--r--bower.json3
-rw-r--r--docs/PursLoader/Loader.md12
-rw-r--r--docs/PursLoader/Plugin.md2
-rw-r--r--src/PursLoader/Loader.purs44
-rw-r--r--src/PursLoader/Plugin.purs2
5 files changed, 42 insertions, 21 deletions
diff --git a/bower.json b/bower.json
index 761c24c..681612d 100644
--- a/bower.json
+++ b/bower.json
@@ -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
6type Effects eff = (loader :: Loader | eff) 6type Effects eff = (console :: CONSOLE, err :: EXCEPTION | eff)
7```
8
9#### `Effects_`
10
11``` purescript
12type Effects_ eff = Effects (loader :: Loader | eff)
7``` 13```
8 14
9#### `loader` 15#### `loader`
10 16
11``` purescript 17``` purescript
12loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit 18loader :: forall eff. LoaderRef -> String -> Eff (Effects_ eff) Unit
13``` 19```
14 20
15#### `loaderFn` 21#### `loaderFn`
16 22
17``` purescript 23``` purescript
18loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) 24loaderFn :: 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 9abec4d..645c41b 100644
--- a/docs/PursLoader/Plugin.md
+++ b/docs/PursLoader/Plugin.md
@@ -3,7 +3,7 @@
3#### `Result` 3#### `Result`
4 4
5``` purescript 5``` purescript
6type Result = { srcMap :: ImmutableMap String String, ffiMap :: ImmutableMap String String, graph :: DependencyGraph } 6type Result = { srcMap :: ImmutableMap String String, ffiMap :: ImmutableMap String String, graph :: DependencyGraph, output :: String }
7``` 7```
8 8
9#### `Compile` 9#### `Compile`
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
diff --git a/src/PursLoader/Plugin.purs b/src/PursLoader/Plugin.purs
index 23f8600..c9f0133 100644
--- a/src/PursLoader/Plugin.purs
+++ b/src/PursLoader/Plugin.purs
@@ -18,7 +18,7 @@ import Data.Function (Fn4(), runFn4)
18import Data.Maybe (Maybe(..)) 18import Data.Maybe (Maybe(..))
19import Data.Nullable (Nullable()) 19import Data.Nullable (Nullable())
20 20
21type Result = { srcMap :: ImmutableMap String String, ffiMap :: ImmutableMap String String, graph :: DependencyGraph } 21type Result = { srcMap :: ImmutableMap String String, ffiMap :: ImmutableMap String String, graph :: DependencyGraph, output :: String }
22 22
23type Compile eff = Nullable Error -> Result -> Eff eff Unit 23type Compile eff = Nullable Error -> Result -> Eff eff Unit
24 24