aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Loader.purs
diff options
context:
space:
mode:
authoreric thul <thul.eric@gmail.com>2015-07-18 17:07:38 -0400
committereric thul <thul.eric@gmail.com>2015-07-18 17:07:38 -0400
commit3610dff1b8308a810d827f0595832b326deff37b (patch)
treea4858f5e16e3df01aee249ad53b5b76f90fb97de /src/Loader.purs
parent418c9429609c6d80ddaf7df41e0ee64192e2ec7b (diff)
downloadpurs-loader-3610dff1b8308a810d827f0595832b326deff37b.tar.gz
purs-loader-3610dff1b8308a810d827f0595832b326deff37b.tar.zst
purs-loader-3610dff1b8308a810d827f0595832b326deff37b.zip
Add FFI JavaScript as a webpack dependency
Resolves #18
Diffstat (limited to 'src/Loader.purs')
-rw-r--r--src/Loader.purs23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/Loader.purs b/src/Loader.purs
index 872a51c..e9e03c4 100644
--- a/src/Loader.purs
+++ b/src/Loader.purs
@@ -13,12 +13,12 @@ import Data.Array ((!!), concat)
13import Data.Function (Fn2(), mkFn2) 13import Data.Function (Fn2(), mkFn2)
14import Data.Maybe (Maybe(..), fromMaybe, maybe) 14import Data.Maybe (Maybe(..), fromMaybe, maybe)
15import Data.String (joinWith) 15import Data.String (joinWith)
16import Data.String.Regex (match, noFlags, regex) 16import Data.String.Regex (match, noFlags, regex, test)
17 17
18import PursLoader.ChildProcess (ChildProcess(), spawn) 18import PursLoader.ChildProcess (ChildProcess(), spawn)
19import PursLoader.FS (FS(), writeFileUtf8) 19import PursLoader.FS (FS(), writeFileUtf8, findFileUtf8)
20import PursLoader.Glob (Glob(), globAll) 20import PursLoader.Glob (Glob(), globAll)
21import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query) 21import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query, clearDependencies, addDependency, resourcePath)
22import PursLoader.LoaderUtil (parseQuery) 22import PursLoader.LoaderUtil (parseQuery)
23import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions) 23import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions)
24 24
@@ -26,6 +26,8 @@ type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader
26 26
27moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } 27moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true }
28 28
29foreignRegex = regex "(?:^|\\n)\\s*foreign import\\s+" noFlags { ignoreCase = true }
30
29pscCommand = "psc" 31pscCommand = "psc"
30 32
31psciCommand = "psci" 33psciCommand = "psci"
@@ -54,6 +56,11 @@ mkPsci srcs ffis = joinWith "\n" ((loadModule <$> concat srcs) <> (loadForeign <
54 loadForeign :: String -> String 56 loadForeign :: String -> String
55 loadForeign a = ":f " ++ relative cwd a 57 loadForeign a = ":f " ++ relative cwd a
56 58
59findFFI :: forall eff. [[String]] -> String -> Aff (fs :: FS | eff) (Maybe String)
60findFFI ffiss name = findFileUtf8 re (concat ffiss)
61 where
62 re = regex ("(?:^|\\n)//\\s*module\\s*" ++ name ++ "\\s*\\n") noFlags
63
57loader' :: forall eff. LoaderRef -> String -> Aff (Effects eff) (Maybe String) 64loader' :: forall eff. LoaderRef -> String -> Aff (Effects eff) (Maybe String)
58loader' ref source = do 65loader' ref source = do
59 liftEff $ cacheable ref 66 liftEff $ cacheable ref
@@ -73,8 +80,18 @@ loader' ref source = do
73 writeFileUtf8 psciFilename psciFile 80 writeFileUtf8 psciFilename psciFile
74 81
75 let moduleName = match moduleRegex source >>= (!!!) 1 82 let moduleName = match moduleRegex source >>= (!!!) 1
83 hasForeign = test foreignRegex source
76 result = (\a -> "module.exports = require('" ++ a ++ "');") <$> moduleName 84 result = (\a -> "module.exports = require('" ++ a ++ "');") <$> moduleName
77 85
86 liftEff (clearDependencies ref)
87 liftEff (addDependency ref (resourcePath ref))
88
89 foreignPath <- if hasForeign
90 then fromMaybe (pure Nothing) (findFFI ffiss <$> moduleName)
91 else pure Nothing
92
93 fromMaybe (pure unit) ((\path -> liftEff (addDependency ref path)) <$> foreignPath)
94
78 return result 95 return result
79 96
80loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit 97loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit