aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/PursLoader/Loader.purs
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/Loader.purs
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/Loader.purs')
-rw-r--r--src/PursLoader/Loader.purs45
1 files changed, 25 insertions, 20 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