]> git.immae.eu Git - github/fretlink/purs-loader.git/blame - src/PursLoader/Loader.purs
Splitting PSC functionality into a separate plugin
[github/fretlink/purs-loader.git] / src / PursLoader / Loader.purs
CommitLineData
c194f84c 1module PursLoader.Loader
1983893b 2 ( Effects()
c194f84c 3 , loader
4 , loaderFn
5 ) where
6
63d6a244 7import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (<<<), (++), bind, const)
03b840cb 8
63d6a244 9import Control.Bind (join)
c194f84c 10import Control.Monad.Eff (Eff())
63d6a244 11import Control.Monad.Eff.Exception (Error(), error)
c194f84c 12
63d6a244 13import Data.Array ((!!))
c194f84c 14import Data.Function (Fn2(), mkFn2)
63d6a244 15import Data.Maybe (Maybe(..), maybe)
16import Data.Either (either)
17import Data.Foreign (Foreign())
2b620717 18import Data.Foreign.Class (read)
63d6a244 19import Data.Foreign.Null (runNull)
20import Data.String.Regex (Regex(), match, noFlags, regex)
21
22import Unsafe.Coerce (unsafeCoerce)
23
24import PursLoader.LoaderRef
25 ( LoaderRef()
26 , Loader()
27 , async
28 , cacheable
29 , query
30 , clearDependencies
31 , addDependency
32 , resourcePath
33 )
c194f84c 34
0e1221d7 35import PursLoader.LoaderUtil (parseQuery)
63d6a244 36import PursLoader.Options (runOptions)
37import PursLoader.Path (dirname, relative)
c194f84c 38
63d6a244 39type Effects eff = (loader :: Loader | eff)
c194f84c 40
63d6a244 41type PurescriptWebpackPluginContext eff = { compile :: (Foreign -> Eff (Effects eff) Unit) -> Eff (Effects eff) Unit }
c194f84c 42
63d6a244 43loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit
44loader 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 77loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit)
c194f84c 78loaderFn = mkFn2 loader