aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/PursLoader
diff options
context:
space:
mode:
authoreric thul <thul.eric@gmail.com>2016-03-08 21:58:55 -0500
committereric thul <thul.eric@gmail.com>2016-03-08 22:01:40 -0500
commit87145c4d29e9ab45d04c62d19e8697527728549f (patch)
tree35bc21658cebc370f2d0aecee13a2188602bfd80 /src/PursLoader
parent17f22f868b851e50081844562627a7a8a414dcaa (diff)
downloadpurs-loader-87145c4d29e9ab45d04c62d19e8697527728549f.tar.gz
purs-loader-87145c4d29e9ab45d04c62d19e8697527728549f.tar.zst
purs-loader-87145c4d29e9ab45d04c62d19e8697527728549f.zip
Handle optional bundling by the compiler
Resolves ethul/purescript-webpack-plugin#9
Diffstat (limited to 'src/PursLoader')
-rw-r--r--src/PursLoader/Loader.purs45
-rw-r--r--src/PursLoader/LoaderRef.js4
-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.purs5
7 files changed, 29 insertions, 72 deletions
diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs
index d1068b6..0dfa20e 100644
--- a/src/PursLoader/Loader.purs
+++ b/src/PursLoader/Loader.purs
@@ -4,7 +4,7 @@ module PursLoader.Loader
4 , loaderFn 4 , loaderFn
5 ) where 5 ) where
6 6
7import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), bind, const, id, pure, unit) 7import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), (<<<), bind, const, id, pure, unit)
8 8
9import Control.Apply ((*>)) 9import Control.Apply ((*>))
10import Control.Alt ((<|>)) 10import Control.Alt ((<|>))
@@ -13,9 +13,7 @@ import Control.Monad.Eff (Eff(), foreachE)
13import Control.Monad.Eff.Exception (Error(), error) 13import Control.Monad.Eff.Exception (Error(), error)
14 14
15import Data.Array ((!!)) 15import Data.Array ((!!))
16import Data.Bifunctor (lmap)
17import Data.Either (Either(..), either) 16import Data.Either (Either(..), either)
18import Data.Foreign.Class (read)
19import Data.Function (Fn2(), mkFn2) 17import Data.Function (Fn2(), mkFn2)
20import Data.Maybe (Maybe(..), maybe) 18import Data.Maybe (Maybe(..), maybe)
21import Data.Nullable (toMaybe) 19import Data.Nullable (toMaybe)
@@ -29,16 +27,13 @@ import PursLoader.LoaderRef
29 , Loader() 27 , Loader()
30 , async 28 , async
31 , cacheable 29 , cacheable
32 , query
33 , clearDependencies 30 , clearDependencies
34 , addDependency 31 , addDependency
35 , resourcePath 32 , resourcePath
36 ) 33 )
37 34
38import PursLoader.Debug (debug) 35import PursLoader.Debug (debug)
39import PursLoader.LoaderUtil (parseQuery) 36import PursLoader.Path (dirname, joinPath, relative)
40import PursLoader.Options (Options(..))
41import PursLoader.Path (dirname, relative)
42import PursLoader.Plugin as Plugin 37import PursLoader.Plugin as Plugin
43 38
44type Effects eff = (loader :: Loader | eff) 39type Effects eff = (loader :: Loader | eff)
@@ -77,10 +72,31 @@ loader ref source = do
77 callback (toMaybe error') res 72 callback (toMaybe error') res
78 73
79 exports :: Either Error String 74 exports :: Either Error String
80 exports = (\a b -> "module.exports = require('" ++ a ++ "')['" ++ b ++ "'];") <$> path <*> name 75 exports =
76 if pluginContext.options.bundle
77 then bundleExport <$> name
78 else moduleExport <<< modulePath <$> name
79 where
80 bundleExport :: String -> String
81 bundleExport name' = "module.exports = require('" ++ path ++ "')['" ++ name' ++ "'];"
82 where
83 path :: String
84 path = relative resourceDir pluginContext.options.bundleOutput
85
86 moduleExport :: String -> String
87 moduleExport path = "module.exports = require('" ++ path ++ "');"
88
89 modulePath :: String -> String
90 modulePath = relative resourceDir <<< joinPath pluginContext.options.output
91
92 resourceDir :: String
93 resourceDir = dirname (resourcePath ref)
81 94
82 dependencies :: Either Error (Array String) 95 dependencies :: Either Error (Array String)
83 dependencies = name >>= Plugin.dependenciesOf graph 96 dependencies =
97 if pluginContext.options.bundle
98 then name >>= Plugin.dependenciesOf graph
99 else pure []
84 100
85 addTransitive :: String -> Eff (Effects eff) Unit 101 addTransitive :: String -> Eff (Effects eff) Unit
86 addTransitive dep = addDep (Plugin.get srcMap dep) *> addDep (Plugin.get ffiMap dep) 102 addTransitive dep = addDep (Plugin.get srcMap dep) *> addDep (Plugin.get ffiMap dep)
@@ -96,16 +112,5 @@ loader ref source = do
96 re :: Regex 112 re :: Regex
97 re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } 113 re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true }
98 114
99 path :: Either Error String
100 path = (\(Options opts) -> relative resourceDir opts.bundleOutput) <$> options
101 where
102 options :: Either Error Options
103 options =
104 lmap (const $ error "Failed to parse loader query")
105 (read $ parseQuery (query ref))
106
107 resourceDir :: String
108 resourceDir = dirname (resourcePath ref)
109
110loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) 115loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit)
111loaderFn = mkFn2 loader 116loaderFn = mkFn2 loader
diff --git a/src/PursLoader/LoaderRef.js b/src/PursLoader/LoaderRef.js
index 3ce0970..e92c72c 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();
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.purs b/src/PursLoader/Plugin.purs
index 23f8600..520c786 100644
--- a/src/PursLoader/Plugin.purs
+++ b/src/PursLoader/Plugin.purs
@@ -2,6 +2,7 @@ module PursLoader.Plugin
2 ( Result() 2 ( Result()
3 , Compile() 3 , Compile()
4 , Context() 4 , Context()
5 , Options()
5 , ImmutableMap() 6 , ImmutableMap()
6 , DependencyGraph() 7 , DependencyGraph()
7 , get 8 , get
@@ -22,7 +23,9 @@ type Result = { srcMap :: ImmutableMap String String, ffiMap :: ImmutableMap Str
22 23
23type Compile eff = Nullable Error -> Result -> Eff eff Unit 24type Compile eff = Nullable Error -> Result -> Eff eff Unit
24 25
25type Context eff = { compile :: Compile eff -> Eff eff Unit } 26type Context eff = { compile :: Compile eff -> Eff eff Unit, options :: Options }
27
28type Options = { bundle :: Boolean, output :: String, bundleOutput :: String }
26 29
27get :: forall key value. ImmutableMap key value -> key -> Maybe value 30get :: forall key value. ImmutableMap key value -> key -> Maybe value
28get = runFn4 getFn Nothing Just 31get = runFn4 getFn Nothing Just