diff options
Diffstat (limited to 'src/PursLoader/Loader.purs')
-rw-r--r-- | src/PursLoader/Loader.purs | 45 |
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 | ||
7 | import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), bind, const, id, pure, unit) | 7 | import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), (<<<), bind, const, id, pure, unit) |
8 | 8 | ||
9 | import Control.Apply ((*>)) | 9 | import Control.Apply ((*>)) |
10 | import Control.Alt ((<|>)) | 10 | import Control.Alt ((<|>)) |
@@ -13,9 +13,7 @@ import Control.Monad.Eff (Eff(), foreachE) | |||
13 | import Control.Monad.Eff.Exception (Error(), error) | 13 | import Control.Monad.Eff.Exception (Error(), error) |
14 | 14 | ||
15 | import Data.Array ((!!)) | 15 | import Data.Array ((!!)) |
16 | import Data.Bifunctor (lmap) | ||
17 | import Data.Either (Either(..), either) | 16 | import Data.Either (Either(..), either) |
18 | import Data.Foreign.Class (read) | ||
19 | import Data.Function (Fn2(), mkFn2) | 17 | import Data.Function (Fn2(), mkFn2) |
20 | import Data.Maybe (Maybe(..), maybe) | 18 | import Data.Maybe (Maybe(..), maybe) |
21 | import Data.Nullable (toMaybe) | 19 | import 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 | ||
38 | import PursLoader.Debug (debug) | 35 | import PursLoader.Debug (debug) |
39 | import PursLoader.LoaderUtil (parseQuery) | 36 | import PursLoader.Path (dirname, joinPath, relative) |
40 | import PursLoader.Options (Options(..)) | ||
41 | import PursLoader.Path (dirname, relative) | ||
42 | import PursLoader.Plugin as Plugin | 37 | import PursLoader.Plugin as Plugin |
43 | 38 | ||
44 | type Effects eff = (loader :: Loader | eff) | 39 | type 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 | |||
110 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) | 115 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) |
111 | loaderFn = mkFn2 loader | 116 | loaderFn = mkFn2 loader |