diff options
Diffstat (limited to 'src/PursLoader/Loader.purs')
-rw-r--r-- | src/PursLoader/Loader.purs | 59 |
1 files changed, 25 insertions, 34 deletions
diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs index d1068b6..402e805 100644 --- a/src/PursLoader/Loader.purs +++ b/src/PursLoader/Loader.purs | |||
@@ -4,18 +4,15 @@ 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 ((*>)) | ||
10 | import Control.Alt ((<|>)) | 9 | import Control.Alt ((<|>)) |
11 | import Control.Bind (join) | 10 | import Control.Bind (join) |
12 | import Control.Monad.Eff (Eff(), foreachE) | 11 | import Control.Monad.Eff (Eff(), foreachE) |
13 | import Control.Monad.Eff.Exception (Error(), error) | 12 | import Control.Monad.Eff.Exception (Error(), error) |
14 | 13 | ||
15 | import Data.Array ((!!)) | 14 | import Data.Array ((!!)) |
16 | import Data.Bifunctor (lmap) | ||
17 | import Data.Either (Either(..), either) | 15 | import Data.Either (Either(..), either) |
18 | import Data.Foreign.Class (read) | ||
19 | import Data.Function (Fn2(), mkFn2) | 16 | import Data.Function (Fn2(), mkFn2) |
20 | import Data.Maybe (Maybe(..), maybe) | 17 | import Data.Maybe (Maybe(..), maybe) |
21 | import Data.Nullable (toMaybe) | 18 | import Data.Nullable (toMaybe) |
@@ -29,16 +26,12 @@ import PursLoader.LoaderRef | |||
29 | , Loader() | 26 | , Loader() |
30 | , async | 27 | , async |
31 | , cacheable | 28 | , cacheable |
32 | , query | ||
33 | , clearDependencies | ||
34 | , addDependency | 29 | , addDependency |
35 | , resourcePath | 30 | , resourcePath |
36 | ) | 31 | ) |
37 | 32 | ||
38 | import PursLoader.Debug (debug) | 33 | import PursLoader.Debug (debug) |
39 | import PursLoader.LoaderUtil (parseQuery) | 34 | import PursLoader.Path (dirname, joinPath, relative) |
40 | import PursLoader.Options (Options(..)) | ||
41 | import PursLoader.Path (dirname, relative) | ||
42 | import PursLoader.Plugin as Plugin | 35 | import PursLoader.Plugin as Plugin |
43 | 36 | ||
44 | type Effects eff = (loader :: Loader | eff) | 37 | type Effects eff = (loader :: Loader | eff) |
@@ -57,9 +50,7 @@ loader ref source = do | |||
57 | pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext | 50 | pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext |
58 | 51 | ||
59 | compile :: AsyncCallback eff -> Plugin.Compile (Effects eff) | 52 | compile :: AsyncCallback eff -> Plugin.Compile (Effects eff) |
60 | compile callback error' { srcMap, ffiMap, graph } = do | 53 | compile callback error' graph = do |
61 | clearDependencies ref | ||
62 | |||
63 | either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name | 54 | either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name |
64 | 55 | ||
65 | addDependency ref (resourcePath ref) | 56 | addDependency ref (resourcePath ref) |
@@ -69,24 +60,35 @@ loader ref source = do | |||
69 | where | 60 | where |
70 | handle :: String -> Array String -> String -> Eff (Effects eff) Unit | 61 | handle :: String -> Array String -> String -> Eff (Effects eff) Unit |
71 | handle name' deps res = do | 62 | handle name' deps res = do |
72 | debug ("Adding PureScript transitive dependencies for " ++ name') | 63 | debug ("Adding PureScript dependencies for " ++ name') |
73 | addTransitive name' | 64 | foreachE deps (addDependency ref) |
74 | foreachE deps addTransitive | ||
75 | debug "Generated loader result" | 65 | debug "Generated loader result" |
76 | debug res | 66 | debug res |
77 | callback (toMaybe error') res | 67 | callback (toMaybe error') res |
78 | 68 | ||
79 | exports :: Either Error String | 69 | exports :: Either Error String |
80 | exports = (\a b -> "module.exports = require('" ++ a ++ "')['" ++ b ++ "'];") <$> path <*> name | 70 | exports = |
71 | if pluginContext.options.bundle | ||
72 | then bundleExport <$> name | ||
73 | else moduleExport <<< modulePath <$> name | ||
74 | where | ||
75 | bundleExport :: String -> String | ||
76 | bundleExport name' = "module.exports = require('" ++ path ++ "')['" ++ name' ++ "'];" | ||
77 | where | ||
78 | path :: String | ||
79 | path = relative resourceDir pluginContext.options.bundleOutput | ||
81 | 80 | ||
82 | dependencies :: Either Error (Array String) | 81 | moduleExport :: String -> String |
83 | dependencies = name >>= Plugin.dependenciesOf graph | 82 | moduleExport path = "module.exports = require('" ++ path ++ "');" |
84 | 83 | ||
85 | addTransitive :: String -> Eff (Effects eff) Unit | 84 | modulePath :: String -> String |
86 | addTransitive dep = addDep (Plugin.get srcMap dep) *> addDep (Plugin.get ffiMap dep) | 85 | modulePath = relative resourceDir <<< joinPath pluginContext.options.output |
87 | where | 86 | |
88 | addDep :: Maybe String -> Eff (Effects eff) Unit | 87 | resourceDir :: String |
89 | addDep = maybe (pure unit) (addDependency ref) | 88 | resourceDir = dirname (resourcePath ref) |
89 | |||
90 | dependencies :: Either Error (Array String) | ||
91 | dependencies = Plugin.dependenciesOf graph (resourcePath ref) | ||
90 | 92 | ||
91 | name :: Either Error String | 93 | name :: Either Error String |
92 | name = | 94 | name = |
@@ -96,16 +98,5 @@ loader ref source = do | |||
96 | re :: Regex | 98 | re :: Regex |
97 | re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } | 99 | re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } |
98 | 100 | ||
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) | 101 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) |
111 | loaderFn = mkFn2 loader | 102 | loaderFn = mkFn2 loader |