diff options
author | eric thul <thul.eric@gmail.com> | 2015-07-18 17:07:38 -0400 |
---|---|---|
committer | eric thul <thul.eric@gmail.com> | 2015-07-18 17:07:38 -0400 |
commit | 3610dff1b8308a810d827f0595832b326deff37b (patch) | |
tree | a4858f5e16e3df01aee249ad53b5b76f90fb97de /src/Loader.purs | |
parent | 418c9429609c6d80ddaf7df41e0ee64192e2ec7b (diff) | |
download | purs-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.purs | 23 |
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) | |||
13 | import Data.Function (Fn2(), mkFn2) | 13 | import Data.Function (Fn2(), mkFn2) |
14 | import Data.Maybe (Maybe(..), fromMaybe, maybe) | 14 | import Data.Maybe (Maybe(..), fromMaybe, maybe) |
15 | import Data.String (joinWith) | 15 | import Data.String (joinWith) |
16 | import Data.String.Regex (match, noFlags, regex) | 16 | import Data.String.Regex (match, noFlags, regex, test) |
17 | 17 | ||
18 | import PursLoader.ChildProcess (ChildProcess(), spawn) | 18 | import PursLoader.ChildProcess (ChildProcess(), spawn) |
19 | import PursLoader.FS (FS(), writeFileUtf8) | 19 | import PursLoader.FS (FS(), writeFileUtf8, findFileUtf8) |
20 | import PursLoader.Glob (Glob(), globAll) | 20 | import PursLoader.Glob (Glob(), globAll) |
21 | import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query) | 21 | import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query, clearDependencies, addDependency, resourcePath) |
22 | import PursLoader.LoaderUtil (parseQuery) | 22 | import PursLoader.LoaderUtil (parseQuery) |
23 | import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions) | 23 | import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions) |
24 | 24 | ||
@@ -26,6 +26,8 @@ type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader | |||
26 | 26 | ||
27 | moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } | 27 | moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } |
28 | 28 | ||
29 | foreignRegex = regex "(?:^|\\n)\\s*foreign import\\s+" noFlags { ignoreCase = true } | ||
30 | |||
29 | pscCommand = "psc" | 31 | pscCommand = "psc" |
30 | 32 | ||
31 | psciCommand = "psci" | 33 | psciCommand = "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 | ||
59 | findFFI :: forall eff. [[String]] -> String -> Aff (fs :: FS | eff) (Maybe String) | ||
60 | findFFI ffiss name = findFileUtf8 re (concat ffiss) | ||
61 | where | ||
62 | re = regex ("(?:^|\\n)//\\s*module\\s*" ++ name ++ "\\s*\\n") noFlags | ||
63 | |||
57 | loader' :: forall eff. LoaderRef -> String -> Aff (Effects eff) (Maybe String) | 64 | loader' :: forall eff. LoaderRef -> String -> Aff (Effects eff) (Maybe String) |
58 | loader' ref source = do | 65 | loader' 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 | ||
80 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit | 97 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit |