diff options
Diffstat (limited to 'src/PursLoader/Loader.purs')
-rw-r--r-- | src/PursLoader/Loader.purs | 108 |
1 files changed, 0 insertions, 108 deletions
diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs deleted file mode 100644 index acb0993..0000000 --- a/src/PursLoader/Loader.purs +++ /dev/null | |||
@@ -1,108 +0,0 @@ | |||
1 | module PursLoader.Loader | ||
2 | ( Effects() | ||
3 | , Effects_() | ||
4 | , loader | ||
5 | , loaderFn | ||
6 | ) where | ||
7 | |||
8 | import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), (<<<), bind, const, id, pure, unit) | ||
9 | |||
10 | import Control.Bind (join) | ||
11 | import Control.Monad.Eff (Eff(), foreachE) | ||
12 | import Control.Monad.Eff.Console (CONSOLE()) | ||
13 | import Control.Monad.Eff.Exception (EXCEPTION(), Error(), error) | ||
14 | |||
15 | import Data.Array ((!!)) | ||
16 | import Data.Either (Either(..), either) | ||
17 | import Data.Function (Fn2(), mkFn2) | ||
18 | import Data.Maybe (maybe) | ||
19 | import Data.Nullable (toMaybe) | ||
20 | import Data.String.Regex (Regex(), match, noFlags, regex) | ||
21 | |||
22 | import Unsafe.Coerce (unsafeCoerce) | ||
23 | |||
24 | import PursLoader.Debug (debug) | ||
25 | import PursLoader.JsStringEscape (jsStringEscape) | ||
26 | import PursLoader.LoaderRef | ||
27 | ( AsyncCallback() | ||
28 | , LoaderRef() | ||
29 | , Loader() | ||
30 | , async | ||
31 | , cacheable | ||
32 | , addDependency | ||
33 | , resourcePath | ||
34 | ) | ||
35 | import PursLoader.Path (dirname, joinPath, relative) | ||
36 | import PursLoader.Plugin as Plugin | ||
37 | |||
38 | type Effects eff = (console :: CONSOLE, err :: EXCEPTION | eff) | ||
39 | |||
40 | type Effects_ eff = Effects (loader :: Loader | eff) | ||
41 | |||
42 | loader :: forall eff. LoaderRef -> String -> Eff (Effects_ eff) Unit | ||
43 | loader ref source = do | ||
44 | callback <- async ref | ||
45 | |||
46 | cacheable ref | ||
47 | |||
48 | debug "Invoke PureScript plugin compilation" | ||
49 | |||
50 | pluginContext.compile (compile callback) | ||
51 | where | ||
52 | pluginContext :: Plugin.Context (Effects_ eff) | ||
53 | pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext | ||
54 | |||
55 | compile :: AsyncCallback (Effects eff) -> Plugin.Compile (Effects_ eff) | ||
56 | compile callback error' graph = do | ||
57 | either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name | ||
58 | |||
59 | addDependency ref (resourcePath ref) | ||
60 | |||
61 | either (const $ callback (pure fixedError) "") id | ||
62 | (handle <$> name <*> dependencies <*> exports) | ||
63 | where | ||
64 | fixedError :: Error | ||
65 | fixedError = error "PureScript compilation has failed." | ||
66 | |||
67 | handle :: String -> Array String -> String -> Eff (Effects_ eff) Unit | ||
68 | handle name' deps res = do | ||
69 | debug ("Adding PureScript dependencies for " ++ name') | ||
70 | foreachE deps (addDependency ref) | ||
71 | debug "Generated loader result" | ||
72 | debug res | ||
73 | callback (const fixedError <$> toMaybe error') res | ||
74 | |||
75 | exports :: Either Error String | ||
76 | exports = | ||
77 | if pluginContext.options.bundle | ||
78 | then bundleExport <$> name | ||
79 | else moduleExport <<< modulePath <$> name | ||
80 | where | ||
81 | bundleExport :: String -> String | ||
82 | bundleExport name' = "module.exports = require('" ++ jsStringEscape path ++ "')['" ++ name' ++ "'];" | ||
83 | where | ||
84 | path :: String | ||
85 | path = relative resourceDir pluginContext.options.bundleOutput | ||
86 | |||
87 | moduleExport :: String -> String | ||
88 | moduleExport path = "module.exports = require('" ++ jsStringEscape path ++ "');" | ||
89 | |||
90 | modulePath :: String -> String | ||
91 | modulePath = relative resourceDir <<< joinPath pluginContext.options.output | ||
92 | |||
93 | resourceDir :: String | ||
94 | resourceDir = dirname (resourcePath ref) | ||
95 | |||
96 | dependencies :: Either Error (Array String) | ||
97 | dependencies = Plugin.dependenciesOf graph (resourcePath ref) | ||
98 | |||
99 | name :: Either Error String | ||
100 | name = | ||
101 | maybe (Left $ error "Failed to parse module name") Right | ||
102 | (join $ match re source >>= \as -> as !! 1) | ||
103 | where | ||
104 | re :: Regex | ||
105 | re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } | ||
106 | |||
107 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects_ eff) Unit) | ||
108 | loaderFn = mkFn2 loader | ||