]> git.immae.eu Git - github/fretlink/purs-loader.git/blame - src/PursLoader/Loader.purs
Bumping version number to 0.6.0-beta.3
[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
46039343 7import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), bind, const, id, pure, unit)
03b840cb 8
46039343 9import Control.Apply ((*>))
17f22f86 10import Control.Alt ((<|>))
63d6a244 11import Control.Bind (join)
46039343 12import Control.Monad.Eff (Eff(), foreachE)
63d6a244 13import Control.Monad.Eff.Exception (Error(), error)
c194f84c 14
63d6a244 15import Data.Array ((!!))
46039343 16import Data.Bifunctor (lmap)
17import Data.Either (Either(..), either)
18import Data.Foreign.Class (read)
c194f84c 19import Data.Function (Fn2(), mkFn2)
63d6a244 20import Data.Maybe (Maybe(..), maybe)
46039343 21import Data.Nullable (toMaybe)
63d6a244 22import Data.String.Regex (Regex(), match, noFlags, regex)
23
24import Unsafe.Coerce (unsafeCoerce)
25
26import PursLoader.LoaderRef
46039343 27 ( AsyncCallback()
28 , LoaderRef()
63d6a244 29 , Loader()
30 , async
31 , cacheable
32 , query
33 , clearDependencies
34 , addDependency
35 , resourcePath
36 )
c194f84c 37
3afbe2fd 38import PursLoader.Debug (debug)
0e1221d7 39import PursLoader.LoaderUtil (parseQuery)
46039343 40import PursLoader.Options (Options(..))
63d6a244 41import PursLoader.Path (dirname, relative)
46039343 42import PursLoader.Plugin as Plugin
c194f84c 43
63d6a244 44type Effects eff = (loader :: Loader | eff)
c194f84c 45
63d6a244 46loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit
47loader ref source = do
48 callback <- async ref
3610dff1 49
63d6a244 50 cacheable ref
c194f84c 51
3afbe2fd 52 debug "Invoke PureScript plugin compilation"
53
46039343 54 pluginContext.compile (compile callback)
63d6a244 55 where
46039343 56 pluginContext :: Plugin.Context (Effects eff)
63d6a244 57 pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext
c194f84c 58
46039343 59 compile :: AsyncCallback eff -> Plugin.Compile (Effects eff)
60 compile callback error' { srcMap, ffiMap, graph } = do
61 clearDependencies ref
62
3afbe2fd 63 either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name
64
46039343 65 addDependency ref (resourcePath ref)
66
17f22f86 67 either (\err -> callback (toMaybe error' <|> Just err) "") id
46039343 68 (handle <$> name <*> dependencies <*> exports)
69 where
70 handle :: String -> Array String -> String -> Eff (Effects eff) Unit
71 handle name' deps res = do
3afbe2fd 72 debug ("Adding PureScript transitive dependencies for " ++ name')
46039343 73 addTransitive name'
74 foreachE deps addTransitive
b1b6e146 75 debug "Generated loader result"
76 debug res
46039343 77 callback (toMaybe error') res
78
79 exports :: Either Error String
80 exports = (\a b -> "module.exports = require('" ++ a ++ "')['" ++ b ++ "'];") <$> path <*> name
81
82 dependencies :: Either Error (Array String)
83 dependencies = name >>= Plugin.dependenciesOf graph
84
85 addTransitive :: String -> Eff (Effects eff) Unit
86 addTransitive dep = addDep (Plugin.get srcMap dep) *> addDep (Plugin.get ffiMap dep)
87 where
88 addDep :: Maybe String -> Eff (Effects eff) Unit
89 addDep = maybe (pure unit) (addDependency ref)
90
91 name :: Either Error String
92 name =
93 maybe (Left $ error "Failed to parse module name") Right
94 (join $ match re source >>= \as -> as !! 1)
95 where
96 re :: Regex
97 re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true }
98
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)
c194f84c 109
1983893b 110loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit)
c194f84c 111loaderFn = mkFn2 loader