]>
Commit | Line | Data |
---|---|---|
c194f84c | 1 | module PursLoader.Loader |
1983893b | 2 | ( Effects() |
c194f84c | 3 | , loader |
4 | , loaderFn | |
5 | ) where | |
6 | ||
63d6a244 | 7 | import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (<<<), (++), bind, const) |
03b840cb | 8 | |
63d6a244 | 9 | import Control.Bind (join) |
c194f84c | 10 | import Control.Monad.Eff (Eff()) |
63d6a244 | 11 | import Control.Monad.Eff.Exception (Error(), error) |
c194f84c | 12 | |
63d6a244 | 13 | import Data.Array ((!!)) |
c194f84c | 14 | import Data.Function (Fn2(), mkFn2) |
63d6a244 | 15 | import Data.Maybe (Maybe(..), maybe) |
16 | import Data.Either (either) | |
17 | import Data.Foreign (Foreign()) | |
2b620717 | 18 | import Data.Foreign.Class (read) |
63d6a244 | 19 | import Data.Foreign.Null (runNull) |
20 | import Data.String.Regex (Regex(), match, noFlags, regex) | |
21 | ||
22 | import Unsafe.Coerce (unsafeCoerce) | |
23 | ||
24 | import PursLoader.LoaderRef | |
25 | ( LoaderRef() | |
26 | , Loader() | |
27 | , async | |
28 | , cacheable | |
29 | , query | |
30 | , clearDependencies | |
31 | , addDependency | |
32 | , resourcePath | |
33 | ) | |
c194f84c | 34 | |
0e1221d7 | 35 | import PursLoader.LoaderUtil (parseQuery) |
63d6a244 | 36 | import PursLoader.Options (runOptions) |
37 | import PursLoader.Path (dirname, relative) | |
c194f84c | 38 | |
63d6a244 | 39 | type Effects eff = (loader :: Loader | eff) |
c194f84c | 40 | |
63d6a244 | 41 | type PurescriptWebpackPluginContext eff = { compile :: (Foreign -> Eff (Effects eff) Unit) -> Eff (Effects eff) Unit } |
c194f84c | 42 | |
63d6a244 | 43 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit |
44 | loader ref source = do | |
45 | callback <- async ref | |
3610dff1 | 46 | |
63d6a244 | 47 | cacheable ref |
c194f84c | 48 | |
0e1221d7 | 49 | let parsed = parseQuery $ query ref |
c194f84c | 50 | |
63d6a244 | 51 | options = either (const Nothing) (Just <<< runOptions) (read parsed) |
2b620717 | 52 | |
63d6a244 | 53 | moduleName = join $ match moduleRegex source >>= \as -> as !! 1 |
0e1221d7 | 54 | |
63d6a244 | 55 | resourceDir = dirname (resourcePath ref) |
0e1221d7 | 56 | |
63d6a244 | 57 | modulePath = (\opts -> relative resourceDir opts.pscBundle) <$> options |
0e1221d7 | 58 | |
63d6a244 | 59 | result = (\path name -> "module.exports = require('" ++ path ++ "')['" ++ name ++ "'];") <$> modulePath <*> moduleName |
0e1221d7 | 60 | |
63d6a244 | 61 | clearDependencies ref |
3610dff1 | 62 | |
63d6a244 | 63 | addDependency ref (resourcePath ref) |
3610dff1 | 64 | |
63d6a244 | 65 | pluginContext.compile (\err -> maybe (callback (Just $ error "Failed to run loader") "") |
66 | (callback (compileError err)) result) | |
67 | where | |
68 | moduleRegex :: Regex | |
69 | moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } | |
bbd2a7af | 70 | |
63d6a244 | 71 | pluginContext :: PurescriptWebpackPluginContext eff |
72 | pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext | |
c194f84c | 73 | |
63d6a244 | 74 | compileError :: Foreign -> Maybe Error |
75 | compileError value = either (const $ Just (error "Failed to compile")) ((<$>) error) (runNull <$> read value) | |
c194f84c | 76 | |
1983893b | 77 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) |
c194f84c | 78 | loaderFn = mkFn2 loader |