aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/PursLoader
diff options
context:
space:
mode:
authoreric thul <thul.eric@gmail.com>2016-03-12 14:55:05 -0500
committereric thul <thul.eric@gmail.com>2016-03-12 14:55:05 -0500
commitcb2f4c886fd1057fbf0511b46cab3489123ced23 (patch)
tree5079dcf661e96520e9b37562b47ab4d83bc7496a /src/PursLoader
parent1c49c88ed60341341ab2b82aba738e8fc1179941 (diff)
parent55120f4502cb76e768f60654f3937db809df8ade (diff)
downloadpurs-loader-cb2f4c886fd1057fbf0511b46cab3489123ced23.tar.gz
purs-loader-cb2f4c886fd1057fbf0511b46cab3489123ced23.tar.zst
purs-loader-cb2f4c886fd1057fbf0511b46cab3489123ced23.zip
Merge branch 'master' into topic/stderr
Diffstat (limited to 'src/PursLoader')
-rw-r--r--src/PursLoader/Loader.purs61
-rw-r--r--src/PursLoader/LoaderRef.js6
-rw-r--r--src/PursLoader/LoaderRef.purs3
-rw-r--r--src/PursLoader/LoaderUtil.js7
-rw-r--r--src/PursLoader/LoaderUtil.purs7
-rw-r--r--src/PursLoader/Options.purs30
-rw-r--r--src/PursLoader/Plugin.js6
-rw-r--r--src/PursLoader/Plugin.purs25
8 files changed, 31 insertions, 114 deletions
diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs
index e1b9e0f..c50c63c 100644
--- a/src/PursLoader/Loader.purs
+++ b/src/PursLoader/Loader.purs
@@ -5,20 +5,17 @@ module PursLoader.Loader
5 , loaderFn 5 , loaderFn
6 ) where 6 ) where
7 7
8import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), bind, const, id, pure, void, unit) 8import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), (<<<), bind, const, id, pure, unit, void)
9 9
10import Control.Apply ((*>))
11import Control.Bind (join) 10import Control.Bind (join)
12import Control.Monad.Eff (Eff(), foreachE) 11import Control.Monad.Eff (Eff(), foreachE)
13import Control.Monad.Eff.Console (CONSOLE()) 12import Control.Monad.Eff.Console (CONSOLE())
14import Control.Monad.Eff.Exception (EXCEPTION(), Error(), error, message) 13import Control.Monad.Eff.Exception (EXCEPTION(), Error(), error, message)
15 14
16import Data.Array ((!!)) 15import Data.Array ((!!))
17import Data.Bifunctor (lmap)
18import Data.Either (Either(..), either) 16import Data.Either (Either(..), either)
19import Data.Foreign.Class (read)
20import Data.Function (Fn2(), mkFn2) 17import Data.Function (Fn2(), mkFn2)
21import Data.Maybe (Maybe(), maybe) 18import Data.Maybe (maybe)
22import Data.Nullable (toMaybe) 19import Data.Nullable (toMaybe)
23import Data.String.Regex (Regex(), match, noFlags, regex) 20import Data.String.Regex (Regex(), match, noFlags, regex)
24 21
@@ -34,16 +31,12 @@ import PursLoader.LoaderRef
34 , Loader() 31 , Loader()
35 , async 32 , async
36 , cacheable 33 , cacheable
37 , query
38 , clearDependencies
39 , addDependency 34 , addDependency
40 , resourcePath 35 , resourcePath
41 ) 36 )
42 37
43import PursLoader.Debug (debug) 38import PursLoader.Debug (debug)
44import PursLoader.LoaderUtil (parseQuery) 39import PursLoader.Path (dirname, joinPath, relative)
45import PursLoader.Options (Options(..))
46import PursLoader.Path (dirname, relative)
47import PursLoader.Plugin as Plugin 40import PursLoader.Plugin as Plugin
48 41
49type Effects eff = (console :: CONSOLE, err :: EXCEPTION | eff) 42type Effects eff = (console :: CONSOLE, err :: EXCEPTION | eff)
@@ -64,9 +57,7 @@ loader ref source = do
64 pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext 57 pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext
65 58
66 compile :: AsyncCallback (Effects eff) -> Plugin.Compile (Effects_ eff) 59 compile :: AsyncCallback (Effects eff) -> Plugin.Compile (Effects_ eff)
67 compile callback error' { srcMap, ffiMap, graph, output } = do 60 compile callback error' graph output = do
68 clearDependencies ref
69
70 either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name 61 either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name
71 62
72 addDependency ref (resourcePath ref) 63 addDependency ref (resourcePath ref)
@@ -83,24 +74,35 @@ loader ref source = do
83 74
84 handle :: String -> Array String -> String -> Eff (Effects_ eff) Unit 75 handle :: String -> Array String -> String -> Eff (Effects_ eff) Unit
85 handle name' deps res = do 76 handle name' deps res = do
86 debug ("Adding PureScript transitive dependencies for " ++ name') 77 debug ("Adding PureScript dependencies for " ++ name')
87 addTransitive name' 78 foreachE deps (addDependency ref)
88 foreachE deps addTransitive
89 debug "Generated loader result" 79 debug "Generated loader result"
90 debug res 80 debug res
91 callback (const fixedError <$> toMaybe error') res 81 callback (const fixedError <$> toMaybe error') res
92 82
93 exports :: Either Error String 83 exports :: Either Error String
94 exports = (\a b -> "module.exports = require('" ++ a ++ "')['" ++ b ++ "'];") <$> path <*> name 84 exports =
85 if pluginContext.options.bundle
86 then bundleExport <$> name
87 else moduleExport <<< modulePath <$> name
88 where
89 bundleExport :: String -> String
90 bundleExport name' = "module.exports = require('" ++ path ++ "')['" ++ name' ++ "'];"
91 where
92 path :: String
93 path = relative resourceDir pluginContext.options.bundleOutput
95 94
96 dependencies :: Either Error (Array String) 95 moduleExport :: String -> String
97 dependencies = name >>= Plugin.dependenciesOf graph 96 moduleExport path = "module.exports = require('" ++ path ++ "');"
98 97
99 addTransitive :: String -> Eff (Effects_ eff) Unit 98 modulePath :: String -> String
100 addTransitive dep = addDep (Plugin.get srcMap dep) *> addDep (Plugin.get ffiMap dep) 99 modulePath = relative resourceDir <<< joinPath pluginContext.options.output
101 where 100
102 addDep :: Maybe String -> Eff (Effects_ eff) Unit 101 resourceDir :: String
103 addDep = maybe (pure unit) (addDependency ref) 102 resourceDir = dirname (resourcePath ref)
103
104 dependencies :: Either Error (Array String)
105 dependencies = Plugin.dependenciesOf graph (resourcePath ref)
104 106
105 name :: Either Error String 107 name :: Either Error String
106 name = 108 name =
@@ -110,16 +112,5 @@ loader ref source = do
110 re :: Regex 112 re :: Regex
111 re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } 113 re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true }
112 114
113 path :: Either Error String
114 path = (\(Options opts) -> relative resourceDir opts.bundleOutput) <$> options
115 where
116 options :: Either Error Options
117 options =
118 lmap (const $ error "Failed to parse loader query")
119 (read $ parseQuery (query ref))
120
121 resourceDir :: String
122 resourceDir = dirname (resourcePath ref)
123
124loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects_ eff) Unit) 115loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects_ eff) Unit)
125loaderFn = mkFn2 loader 116loaderFn = mkFn2 loader
diff --git a/src/PursLoader/LoaderRef.js b/src/PursLoader/LoaderRef.js
index 3ce0970..a5d8e1f 100644
--- a/src/PursLoader/LoaderRef.js
+++ b/src/PursLoader/LoaderRef.js
@@ -21,10 +21,6 @@ function cacheable(ref){
21 }; 21 };
22} 22}
23 23
24function query(ref){
25 return ref.query;
26}
27
28function clearDependencies(ref){ 24function clearDependencies(ref){
29 return function(){ 25 return function(){
30 return ref.clearDependencies(); 26 return ref.clearDependencies();
@@ -47,8 +43,6 @@ exports.asyncFn = asyncFn;
47 43
48exports.cacheable = cacheable; 44exports.cacheable = cacheable;
49 45
50exports.query = query;
51
52exports.clearDependencies = clearDependencies; 46exports.clearDependencies = clearDependencies;
53 47
54exports.resourcePath = resourcePath; 48exports.resourcePath = resourcePath;
diff --git a/src/PursLoader/LoaderRef.purs b/src/PursLoader/LoaderRef.purs
index 87d6006..140d94a 100644
--- a/src/PursLoader/LoaderRef.purs
+++ b/src/PursLoader/LoaderRef.purs
@@ -4,7 +4,6 @@ module PursLoader.LoaderRef
4 , AsyncCallback() 4 , AsyncCallback()
5 , async 5 , async
6 , cacheable 6 , cacheable
7 , query
8 , clearDependencies 7 , clearDependencies
9 , addDependency 8 , addDependency
10 , resourcePath 9 , resourcePath
@@ -34,8 +33,6 @@ async ref = runFn3 asyncFn isJust fromMaybe ref
34 33
35foreign import cacheable :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit 34foreign import cacheable :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit
36 35
37foreign import query :: LoaderRef -> String
38
39foreign import clearDependencies :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit 36foreign import clearDependencies :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit
40 37
41foreign import resourcePath :: LoaderRef -> String 38foreign import resourcePath :: LoaderRef -> String
diff --git a/src/PursLoader/LoaderUtil.js b/src/PursLoader/LoaderUtil.js
deleted file mode 100644
index 45f2703..0000000
--- a/src/PursLoader/LoaderUtil.js
+++ /dev/null
@@ -1,7 +0,0 @@
1'use strict';
2
3// module PursLoader.LoaderUtil
4
5var loaderUtils = require('loader-utils');
6
7exports.parseQuery = loaderUtils.parseQuery;
diff --git a/src/PursLoader/LoaderUtil.purs b/src/PursLoader/LoaderUtil.purs
deleted file mode 100644
index 45495c8..0000000
--- a/src/PursLoader/LoaderUtil.purs
+++ /dev/null
@@ -1,7 +0,0 @@
1module PursLoader.LoaderUtil
2 ( parseQuery
3 ) where
4
5import Data.Foreign (Foreign())
6
7foreign import parseQuery :: String -> Foreign
diff --git a/src/PursLoader/Options.purs b/src/PursLoader/Options.purs
deleted file mode 100644
index 0c1453e..0000000
--- a/src/PursLoader/Options.purs
+++ /dev/null
@@ -1,30 +0,0 @@
1module PursLoader.Options
2 ( Options(..)
3 , runOptions
4 ) where
5
6import Prelude ((<$>), (<<<), id)
7
8import Data.Foreign.Class (IsForeign, readProp)
9import Data.Foreign.NullOrUndefined (runNullOrUndefined)
10import Data.Maybe (maybe)
11
12import PursLoader.Path (joinPath)
13
14newtype Options = Options { bundleOutput :: String }
15
16type Options_ = { bundleOutput :: String }
17
18runOptions :: Options -> Options_
19runOptions (Options options) = options
20
21instance isForeignOptions :: IsForeign Options where
22 read obj =
23 Options <$> ({ bundleOutput: _ }
24 <$> (maybe bundleOutputDefault id <<< runNullOrUndefined <$> readProp bundleOutput obj))
25 where
26 bundleOutput :: String
27 bundleOutput = "bundleOutput"
28
29 bundleOutputDefault :: String
30 bundleOutputDefault = joinPath "output" "bundle.js"
diff --git a/src/PursLoader/Plugin.js b/src/PursLoader/Plugin.js
index 90feb33..ded6df5 100644
--- a/src/PursLoader/Plugin.js
+++ b/src/PursLoader/Plugin.js
@@ -2,12 +2,6 @@
2 2
3// module PursLoader.Plugin 3// module PursLoader.Plugin
4 4
5function getFn(nothing, just, map, key) {
6 var value = map.get(key);
7 return value === undefined ? nothing : just(value);
8}
9exports.getFn = getFn;
10
11function dependenciesOfFn(left, right, graph, node) { 5function dependenciesOfFn(left, right, graph, node) {
12 try { 6 try {
13 var dependencies = graph.dependenciesOf(node); 7 var dependencies = graph.dependenciesOf(node);
diff --git a/src/PursLoader/Plugin.purs b/src/PursLoader/Plugin.purs
index c9f0133..8bb53be 100644
--- a/src/PursLoader/Plugin.purs
+++ b/src/PursLoader/Plugin.purs
@@ -1,10 +1,8 @@
1module PursLoader.Plugin 1module PursLoader.Plugin
2 ( Result() 2 ( Compile()
3 , Compile()
4 , Context() 3 , Context()
5 , ImmutableMap() 4 , Options()
6 , DependencyGraph() 5 , DependencyGraph()
7 , get
8 , dependenciesOf 6 , dependenciesOf
9 ) where 7 ) where
10 8
@@ -15,32 +13,19 @@ import Control.Monad.Eff.Exception (Error())
15 13
16import Data.Either (Either(..)) 14import Data.Either (Either(..))
17import Data.Function (Fn4(), runFn4) 15import Data.Function (Fn4(), runFn4)
18import Data.Maybe (Maybe(..))
19import Data.Nullable (Nullable()) 16import Data.Nullable (Nullable())
20 17
21type Result = { srcMap :: ImmutableMap String String, ffiMap :: ImmutableMap String String, graph :: DependencyGraph, output :: String } 18type Compile eff = Nullable Error -> DependencyGraph -> String -> Eff eff Unit
22 19
23type Compile eff = Nullable Error -> Result -> Eff eff Unit 20type Context eff = { compile :: Compile eff -> Eff eff Unit, options :: Options }
24 21
25type Context eff = { compile :: Compile eff -> Eff eff Unit } 22type Options = { bundle :: Boolean, output :: String, bundleOutput :: String }
26
27get :: forall key value. ImmutableMap key value -> key -> Maybe value
28get = runFn4 getFn Nothing Just
29 23
30dependenciesOf :: DependencyGraph -> String -> Either Error (Array String) 24dependenciesOf :: DependencyGraph -> String -> Either Error (Array String)
31dependenciesOf = runFn4 dependenciesOfFn Left Right 25dependenciesOf = runFn4 dependenciesOfFn Left Right
32 26
33foreign import data ImmutableMap :: * -> * -> *
34
35foreign import data DependencyGraph :: * 27foreign import data DependencyGraph :: *
36 28
37foreign import getFn
38 :: forall key value. Fn4 (Maybe value)
39 (value -> Maybe value)
40 (ImmutableMap key value)
41 key
42 (Maybe value)
43
44foreign import dependenciesOfFn 29foreign import dependenciesOfFn
45 :: Fn4 (Error -> Either Error (Array String)) 30 :: Fn4 (Error -> Either Error (Array String))
46 (Array String -> Either Error (Array String)) 31 (Array String -> Either Error (Array String))