"version": "0.0.0",
"private": true,
"scripts": {
- "build": "mkdir -p output && ./node_modules/.bin/webpack",
+ "build": "mkdir -p output && ./node_modules/.bin/webpack --progress --colors",
+ "build:watch": "mkdir -p output && ./node_modules/.bin/webpack --progress --colors --watch",
"run": "node bundle.js",
"clean": "rm -rf bower_components && rm -rf bundle.js && rm -rf node_modules && rm -rf output"
},
"license": "MIT",
"devDependencies": {
+ "node-libs-browser": "^0.5.2",
"purs-loader": "file:../",
- "webpack": "^1.8.4"
+ "webpack": "^1.10.1"
}
}
-module Test (test) where
+module Test (test, testing) where
import Prelude
test = "a"
+
+foreign import testing :: String
--- /dev/null
+'use strict';
+
+// module Test
+
+var testing = 'abcde';
+
+exports.testing = testing;
var Prelude = require('Prelude');
-var test = require('./Test');
+var test = require('./Test.purs');
-var foo = require('./Foo');
+var foo = require('./Foo.purs');
-var baz = require('./Foo/Baz');
+var baz = require('./Foo/Baz.purs');
-var bar = require('./Foo/Bar');
+var bar = require('./Foo/Bar.purs');
console.log(Prelude, test, foo, baz, bar);
var srcs = ['src[]=bower_components/purescript-*/src/**/*.purs', 'src[]=src/**/*.purs'];
-var ffis = ['ffi[]=bower_components/purescript-*/src/**/*.js'];
+var ffis = ['ffi[]=bower_components/purescript-*/src/**/*.js', 'ffi[]=src/**/*FFI.js'];
var output = 'output';
, loader: 'purs-loader?output=' + output + '&' + srcs.concat(ffis).join('&')
} ] }
, resolve: { modulesDirectories: modulesDirectories
- , extensions: ['', '.js', '.purs']
+ , extensions: ['', '.js']
}
, resolveLoader: { root: path.join(__dirname, 'node_modules') }
}
module PursLoader.FS
( FS()
, writeFileUtf8
+ , findFileUtf8
) where
import Control.Monad.Aff (Aff(), makeAff)
import Control.Monad.Eff (Eff())
import Control.Monad.Eff.Exception (Error())
+import Data.Maybe (Maybe(..))
+import Data.String.Regex (Regex())
+
import Data.Function
foreign import data FS :: !
(Error -> Eff (fs :: FS | eff) Unit)
(Unit -> Eff (fs :: FS | eff) Unit)
(Eff (fs :: FS | eff) Unit)
+
+findFileUtf8 :: forall eff. Regex -> [String] -> Aff (fs :: FS | eff) (Maybe String)
+findFileUtf8 regexp filepaths = makeAff $ runFn6 findFileUtf8Fn Nothing Just regexp filepaths
+
+foreign import findFileUtf8Fn """
+function findFileUtf8Fn(nothing, just, regex, filepaths, errback, callback) {
+ return function(){
+ var fs = require('fs');
+
+ var async = require('async');
+
+ function findFile(filepath, callback) {
+ fs.readFile(filepath, {encoding: 'utf-8'}, function(error, result){
+ if (error) callback(false);
+ else callback(regex.test(result));
+ });
+ }
+
+ async.detect(filepaths, findFile, function(result){
+ if (!result) callback(nothing)();
+ else callback(just(result))();
+ });
+ };
+}
+""" :: forall eff. Fn6 (Maybe String)
+ (String -> Maybe String)
+ Regex
+ [String]
+ (Error -> Eff (fs :: FS | eff) Unit)
+ (Maybe String -> Eff (fs :: FS | eff) Unit)
+ (Eff (fs :: FS | eff) Unit)
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)
moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true }
+foreignRegex = regex "(?:^|\\n)\\s*foreign import\\s+" noFlags { ignoreCase = true }
+
pscCommand = "psc"
psciCommand = "psci"
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
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
, async
, cacheable
, query
+ , clearDependencies
+ , addDependency
+ , resourcePath
) where
import Control.Monad.Eff (Eff())
function query(ref){
return ref.query;
}""" :: LoaderRef -> String
+
+foreign import clearDependencies """
+function clearDependencies(ref){
+ return function(){
+ return ref.clearDependencies();
+ };
+}""" :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit
+
+foreign import resourcePath """
+function resourcePath(ref){
+ return ref.resourcePath;
+}""" :: LoaderRef -> String
+
+foreign import addDependency """
+function addDependency(ref){
+ return function(dep){
+ return function(){
+ return ref.addDependency(dep);
+ };
+ };
+}""" :: forall eff. LoaderRef -> String -> Eff (loader :: Loader | eff) Unit