From 3610dff1b8308a810d827f0595832b326deff37b Mon Sep 17 00:00:00 2001 From: eric thul Date: Sat, 18 Jul 2015 17:07:38 -0400 Subject: Add FFI JavaScript as a webpack dependency Resolves #18 --- src/Loader.purs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'src/Loader.purs') 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) import Data.Function (Fn2(), mkFn2) import Data.Maybe (Maybe(..), fromMaybe, maybe) import Data.String (joinWith) -import Data.String.Regex (match, noFlags, regex) +import Data.String.Regex (match, noFlags, regex, test) import PursLoader.ChildProcess (ChildProcess(), spawn) -import PursLoader.FS (FS(), writeFileUtf8) +import PursLoader.FS (FS(), writeFileUtf8, findFileUtf8) import PursLoader.Glob (Glob(), globAll) -import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query) +import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query, clearDependencies, addDependency, resourcePath) import PursLoader.LoaderUtil (parseQuery) import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions) @@ -26,6 +26,8 @@ type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } +foreignRegex = regex "(?:^|\\n)\\s*foreign import\\s+" noFlags { ignoreCase = true } + pscCommand = "psc" psciCommand = "psci" @@ -54,6 +56,11 @@ mkPsci srcs ffis = joinWith "\n" ((loadModule <$> concat srcs) <> (loadForeign < loadForeign :: String -> String loadForeign a = ":f " ++ relative cwd a +findFFI :: forall eff. [[String]] -> String -> Aff (fs :: FS | eff) (Maybe String) +findFFI ffiss name = findFileUtf8 re (concat ffiss) + where + re = regex ("(?:^|\\n)//\\s*module\\s*" ++ name ++ "\\s*\\n") noFlags + loader' :: forall eff. LoaderRef -> String -> Aff (Effects eff) (Maybe String) loader' ref source = do liftEff $ cacheable ref @@ -73,8 +80,18 @@ loader' ref source = do writeFileUtf8 psciFilename psciFile let moduleName = match moduleRegex source >>= (!!!) 1 + hasForeign = test foreignRegex source result = (\a -> "module.exports = require('" ++ a ++ "');") <$> moduleName + liftEff (clearDependencies ref) + liftEff (addDependency ref (resourcePath ref)) + + foreignPath <- if hasForeign + then fromMaybe (pure Nothing) (findFFI ffiss <$> moduleName) + else pure Nothing + + fromMaybe (pure unit) ((\path -> liftEff (addDependency ref path)) <$> foreignPath) + return result loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit -- cgit v1.2.3