]>
Commit | Line | Data |
---|---|---|
c194f84c | 1 | module PursLoader.Loader |
1983893b | 2 | ( Effects() |
c194f84c | 3 | , loader |
4 | , loaderFn | |
5 | ) where | |
6 | ||
7 | import Control.Monad.Aff (Aff(), runAff) | |
8 | import Control.Monad.Eff (Eff()) | |
9 | import Control.Monad.Eff.Class (liftEff) | |
10 | import Control.Monad.Eff.Exception (error) | |
11 | ||
1983893b | 12 | import Data.Array ((!!)) |
c194f84c | 13 | import Data.Function (Fn2(), mkFn2) |
14 | import Data.Maybe (Maybe(..), fromMaybe, maybe) | |
1983893b | 15 | import Data.String.Regex (match, noFlags, regex) |
c194f84c | 16 | |
17 | import PursLoader.ChildProcess (ChildProcess(), spawn) | |
1983893b | 18 | import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query) |
c194f84c | 19 | import PursLoader.LoaderUtil (getRemainingRequest, parseQuery) |
1983893b | 20 | import PursLoader.Options (loaderSrcOption, pscOptions) |
c194f84c | 21 | |
1983893b | 22 | type Effects eff = (loader :: Loader, cp :: ChildProcess | eff) |
c194f84c | 23 | |
24 | moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } | |
25 | ||
1983893b | 26 | pscCommand = "psc" |
c194f84c | 27 | |
28 | (!!!) = flip (!!) | |
29 | ||
1983893b | 30 | loader' :: forall eff. LoaderRef -> String -> Aff (Effects eff) (Maybe String) |
c194f84c | 31 | loader' ref source = do |
32 | liftEff $ cacheable ref | |
33 | ||
34 | let request = getRemainingRequest ref | |
c194f84c | 35 | parsed = parseQuery $ query ref |
1983893b | 36 | srcs = fromMaybe [] (loaderSrcOption parsed) |
37 | opts = pscOptions parsed | |
38 | moduleName = match moduleRegex source >>= (!!!) 1 | |
39 | result = (\a -> "module.exports = require('" ++ a ++ "');") <$> moduleName | |
c194f84c | 40 | |
1983893b | 41 | spawn pscCommand (srcs <> opts) |
42 | return result | |
c194f84c | 43 | |
1983893b | 44 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit |
c194f84c | 45 | loader ref source = do |
46 | callback <- async ref | |
47 | runAff (\e -> callback (Just e) "") | |
1983893b | 48 | (maybe (callback (Just (error "Loader has failed to run")) "") |
c194f84c | 49 | (callback Nothing)) |
50 | (loader' ref source) | |
51 | ||
1983893b | 52 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) |
c194f84c | 53 | loaderFn = mkFn2 loader |