aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--example/package.json6
-rw-r--r--example/src/Test.purs4
-rw-r--r--example/src/TestFFI.js7
-rw-r--r--example/src/entry.js8
-rw-r--r--example/webpack.config.js4
-rw-r--r--src/FS.purs35
-rw-r--r--src/Loader.purs23
-rw-r--r--src/LoaderRef.purs24
8 files changed, 99 insertions, 12 deletions
diff --git a/example/package.json b/example/package.json
index df3e74f..d1319b1 100644
--- a/example/package.json
+++ b/example/package.json
@@ -3,13 +3,15 @@
3 "version": "0.0.0", 3 "version": "0.0.0",
4 "private": true, 4 "private": true,
5 "scripts": { 5 "scripts": {
6 "build": "mkdir -p output && ./node_modules/.bin/webpack", 6 "build": "mkdir -p output && ./node_modules/.bin/webpack --progress --colors",
7 "build:watch": "mkdir -p output && ./node_modules/.bin/webpack --progress --colors --watch",
7 "run": "node bundle.js", 8 "run": "node bundle.js",
8 "clean": "rm -rf bower_components && rm -rf bundle.js && rm -rf node_modules && rm -rf output" 9 "clean": "rm -rf bower_components && rm -rf bundle.js && rm -rf node_modules && rm -rf output"
9 }, 10 },
10 "license": "MIT", 11 "license": "MIT",
11 "devDependencies": { 12 "devDependencies": {
13 "node-libs-browser": "^0.5.2",
12 "purs-loader": "file:../", 14 "purs-loader": "file:../",
13 "webpack": "^1.8.4" 15 "webpack": "^1.10.1"
14 } 16 }
15} 17}
diff --git a/example/src/Test.purs b/example/src/Test.purs
index d9f1b96..133a192 100644
--- a/example/src/Test.purs
+++ b/example/src/Test.purs
@@ -1,5 +1,7 @@
1module Test (test) where 1module Test (test, testing) where
2 2
3import Prelude 3import Prelude
4 4
5test = "a" 5test = "a"
6
7foreign import testing :: String
diff --git a/example/src/TestFFI.js b/example/src/TestFFI.js
new file mode 100644
index 0000000..dc2f04f
--- /dev/null
+++ b/example/src/TestFFI.js
@@ -0,0 +1,7 @@
1'use strict';
2
3// module Test
4
5var testing = 'abcde';
6
7exports.testing = testing;
diff --git a/example/src/entry.js b/example/src/entry.js
index 344d4c6..a41536c 100644
--- a/example/src/entry.js
+++ b/example/src/entry.js
@@ -1,11 +1,11 @@
1var Prelude = require('Prelude'); 1var Prelude = require('Prelude');
2 2
3var test = require('./Test'); 3var test = require('./Test.purs');
4 4
5var foo = require('./Foo'); 5var foo = require('./Foo.purs');
6 6
7var baz = require('./Foo/Baz'); 7var baz = require('./Foo/Baz.purs');
8 8
9var bar = require('./Foo/Bar'); 9var bar = require('./Foo/Bar.purs');
10 10
11console.log(Prelude, test, foo, baz, bar); 11console.log(Prelude, test, foo, baz, bar);
diff --git a/example/webpack.config.js b/example/webpack.config.js
index 9294904..1a0eef6 100644
--- a/example/webpack.config.js
+++ b/example/webpack.config.js
@@ -2,7 +2,7 @@ var path = require('path');
2 2
3var srcs = ['src[]=bower_components/purescript-*/src/**/*.purs', 'src[]=src/**/*.purs']; 3var srcs = ['src[]=bower_components/purescript-*/src/**/*.purs', 'src[]=src/**/*.purs'];
4 4
5var ffis = ['ffi[]=bower_components/purescript-*/src/**/*.js']; 5var ffis = ['ffi[]=bower_components/purescript-*/src/**/*.js', 'ffi[]=src/**/*FFI.js'];
6 6
7var output = 'output'; 7var output = 'output';
8 8
@@ -22,7 +22,7 @@ var config
22 , loader: 'purs-loader?output=' + output + '&' + srcs.concat(ffis).join('&') 22 , loader: 'purs-loader?output=' + output + '&' + srcs.concat(ffis).join('&')
23 } ] } 23 } ] }
24 , resolve: { modulesDirectories: modulesDirectories 24 , resolve: { modulesDirectories: modulesDirectories
25 , extensions: ['', '.js', '.purs'] 25 , extensions: ['', '.js']
26 } 26 }
27 , resolveLoader: { root: path.join(__dirname, 'node_modules') } 27 , resolveLoader: { root: path.join(__dirname, 'node_modules') }
28 } 28 }
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