aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
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
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')
-rw-r--r--src/FS.purs35
-rw-r--r--src/Loader.purs23
-rw-r--r--src/LoaderRef.purs24
3 files changed, 79 insertions, 3 deletions
diff --git a/src/FS.purs b/src/FS.purs
index a56fe26..6955a63 100644
--- a/src/FS.purs
+++ b/src/FS.purs
@@ -1,12 +1,16 @@
1module PursLoader.FS 1module PursLoader.FS
2 ( FS() 2 ( FS()
3 , writeFileUtf8 3 , writeFileUtf8
4 , findFileUtf8
4 ) where 5 ) where
5 6
6import Control.Monad.Aff (Aff(), makeAff) 7import Control.Monad.Aff (Aff(), makeAff)
7import Control.Monad.Eff (Eff()) 8import Control.Monad.Eff (Eff())
8import Control.Monad.Eff.Exception (Error()) 9import Control.Monad.Eff.Exception (Error())
9 10
11import Data.Maybe (Maybe(..))
12import Data.String.Regex (Regex())
13
10import Data.Function 14import Data.Function
11 15
12foreign import data FS :: ! 16foreign import data FS :: !
@@ -30,3 +34,34 @@ function writeFileUtf8Fn(filepath, contents, errback, callback) {
30 (Error -> Eff (fs :: FS | eff) Unit) 34 (Error -> Eff (fs :: FS | eff) Unit)
31 (Unit -> Eff (fs :: FS | eff) Unit) 35 (Unit -> Eff (fs :: FS | eff) Unit)
32 (Eff (fs :: FS | eff) Unit) 36 (Eff (fs :: FS | eff) Unit)
37
38findFileUtf8 :: forall eff. Regex -> [String] -> Aff (fs :: FS | eff) (Maybe String)
39findFileUtf8 regexp filepaths = makeAff $ runFn6 findFileUtf8Fn Nothing Just regexp filepaths
40
41foreign import findFileUtf8Fn """
42function findFileUtf8Fn(nothing, just, regex, filepaths, errback, callback) {
43 return function(){
44 var fs = require('fs');
45
46 var async = require('async');
47
48 function findFile(filepath, callback) {
49 fs.readFile(filepath, {encoding: 'utf-8'}, function(error, result){
50 if (error) callback(false);
51 else callback(regex.test(result));
52 });
53 }
54
55 async.detect(filepaths, findFile, function(result){
56 if (!result) callback(nothing)();
57 else callback(just(result))();
58 });
59 };
60}
61""" :: forall eff. Fn6 (Maybe String)
62 (String -> Maybe String)
63 Regex
64 [String]
65 (Error -> Eff (fs :: FS | eff) Unit)
66 (Maybe String -> Eff (fs :: FS | eff) Unit)
67 (Eff (fs :: FS | eff) Unit)
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
diff --git a/src/LoaderRef.purs b/src/LoaderRef.purs
index 2567b1e..f1efa04 100644
--- a/src/LoaderRef.purs
+++ b/src/LoaderRef.purs
@@ -4,6 +4,9 @@ module PursLoader.LoaderRef
4 , async 4 , async
5 , cacheable 5 , cacheable
6 , query 6 , query
7 , clearDependencies
8 , addDependency
9 , resourcePath
7 ) where 10 ) where
8 11
9import Control.Monad.Eff (Eff()) 12import Control.Monad.Eff (Eff())
@@ -48,3 +51,24 @@ foreign import query """
48function query(ref){ 51function query(ref){
49 return ref.query; 52 return ref.query;
50}""" :: LoaderRef -> String 53}""" :: LoaderRef -> String
54
55foreign import clearDependencies """
56function clearDependencies(ref){
57 return function(){
58 return ref.clearDependencies();
59 };
60}""" :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit
61
62foreign import resourcePath """
63function resourcePath(ref){
64 return ref.resourcePath;
65}""" :: LoaderRef -> String
66
67foreign import addDependency """
68function addDependency(ref){
69 return function(dep){
70 return function(){
71 return ref.addDependency(dep);
72 };
73 };
74}""" :: forall eff. LoaderRef -> String -> Eff (loader :: Loader | eff) Unit