diff options
-rw-r--r-- | example/package.json | 6 | ||||
-rw-r--r-- | example/src/Test.purs | 4 | ||||
-rw-r--r-- | example/src/TestFFI.js | 7 | ||||
-rw-r--r-- | example/src/entry.js | 8 | ||||
-rw-r--r-- | example/webpack.config.js | 4 | ||||
-rw-r--r-- | src/FS.purs | 35 | ||||
-rw-r--r-- | src/Loader.purs | 23 | ||||
-rw-r--r-- | src/LoaderRef.purs | 24 |
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 @@ | |||
1 | module Test (test) where | 1 | module Test (test, testing) where |
2 | 2 | ||
3 | import Prelude | 3 | import Prelude |
4 | 4 | ||
5 | test = "a" | 5 | test = "a" |
6 | |||
7 | foreign 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 | |||
5 | var testing = 'abcde'; | ||
6 | |||
7 | exports.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 @@ | |||
1 | var Prelude = require('Prelude'); | 1 | var Prelude = require('Prelude'); |
2 | 2 | ||
3 | var test = require('./Test'); | 3 | var test = require('./Test.purs'); |
4 | 4 | ||
5 | var foo = require('./Foo'); | 5 | var foo = require('./Foo.purs'); |
6 | 6 | ||
7 | var baz = require('./Foo/Baz'); | 7 | var baz = require('./Foo/Baz.purs'); |
8 | 8 | ||
9 | var bar = require('./Foo/Bar'); | 9 | var bar = require('./Foo/Bar.purs'); |
10 | 10 | ||
11 | console.log(Prelude, test, foo, baz, bar); | 11 | console.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 | ||
3 | var srcs = ['src[]=bower_components/purescript-*/src/**/*.purs', 'src[]=src/**/*.purs']; | 3 | var srcs = ['src[]=bower_components/purescript-*/src/**/*.purs', 'src[]=src/**/*.purs']; |
4 | 4 | ||
5 | var ffis = ['ffi[]=bower_components/purescript-*/src/**/*.js']; | 5 | var ffis = ['ffi[]=bower_components/purescript-*/src/**/*.js', 'ffi[]=src/**/*FFI.js']; |
6 | 6 | ||
7 | var output = 'output'; | 7 | var 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 @@ | |||
1 | module PursLoader.FS | 1 | module PursLoader.FS |
2 | ( FS() | 2 | ( FS() |
3 | , writeFileUtf8 | 3 | , writeFileUtf8 |
4 | , findFileUtf8 | ||
4 | ) where | 5 | ) where |
5 | 6 | ||
6 | import Control.Monad.Aff (Aff(), makeAff) | 7 | import Control.Monad.Aff (Aff(), makeAff) |
7 | import Control.Monad.Eff (Eff()) | 8 | import Control.Monad.Eff (Eff()) |
8 | import Control.Monad.Eff.Exception (Error()) | 9 | import Control.Monad.Eff.Exception (Error()) |
9 | 10 | ||
11 | import Data.Maybe (Maybe(..)) | ||
12 | import Data.String.Regex (Regex()) | ||
13 | |||
10 | import Data.Function | 14 | import Data.Function |
11 | 15 | ||
12 | foreign import data FS :: ! | 16 | foreign 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 | |||
38 | findFileUtf8 :: forall eff. Regex -> [String] -> Aff (fs :: FS | eff) (Maybe String) | ||
39 | findFileUtf8 regexp filepaths = makeAff $ runFn6 findFileUtf8Fn Nothing Just regexp filepaths | ||
40 | |||
41 | foreign import findFileUtf8Fn """ | ||
42 | function 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) | |||
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 |
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 | ||
9 | import Control.Monad.Eff (Eff()) | 12 | import Control.Monad.Eff (Eff()) |
@@ -48,3 +51,24 @@ foreign import query """ | |||
48 | function query(ref){ | 51 | function query(ref){ |
49 | return ref.query; | 52 | return ref.query; |
50 | }""" :: LoaderRef -> String | 53 | }""" :: LoaderRef -> String |
54 | |||
55 | foreign import clearDependencies """ | ||
56 | function clearDependencies(ref){ | ||
57 | return function(){ | ||
58 | return ref.clearDependencies(); | ||
59 | }; | ||
60 | }""" :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit | ||
61 | |||
62 | foreign import resourcePath """ | ||
63 | function resourcePath(ref){ | ||
64 | return ref.resourcePath; | ||
65 | }""" :: LoaderRef -> String | ||
66 | |||
67 | foreign import addDependency """ | ||
68 | function 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 | ||