aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/PursLoader
diff options
context:
space:
mode:
authorNathan Faubion <nathan@n-son.com>2015-11-02 14:04:58 -0600
committerNathan Faubion <nathan@n-son.com>2015-11-02 14:16:21 -0600
commit2b620717603a6a2ba4d93ee1ca7334dc31500acf (patch)
tree7548d0a34052169d853ecd5959eb6f1663c129d1 /src/PursLoader
parent8a0ac07ec2c568b8e9bd06cfd0429c6d6aa836e5 (diff)
downloadpurs-loader-2b620717603a6a2ba4d93ee1ca7334dc31500acf.tar.gz
purs-loader-2b620717603a6a2ba4d93ee1ca7334dc31500acf.tar.zst
purs-loader-2b620717603a6a2ba4d93ee1ca7334dc31500acf.zip
Remove `require-path`, use relative paths for PS
Fixes #15 Removes the `require-path` option and fixes it to '../'. When generating the temporary module for Webpack, use a relative path to the output directory so it doesn't need to be in `modulesDirectories`.
Diffstat (limited to 'src/PursLoader')
-rw-r--r--src/PursLoader/Loader.js10
-rw-r--r--src/PursLoader/Loader.purs56
-rw-r--r--src/PursLoader/Options.purs40
3 files changed, 66 insertions, 40 deletions
diff --git a/src/PursLoader/Loader.js b/src/PursLoader/Loader.js
index 98459b6..45e9c2f 100644
--- a/src/PursLoader/Loader.js
+++ b/src/PursLoader/Loader.js
@@ -12,8 +12,18 @@ function relative(from) {
12 }; 12 };
13} 13}
14 14
15function joinPath(a) {
16 return function(b) {
17 return path.join(a, b);
18 };
19}
20
15exports.cwd = cwd; 21exports.cwd = cwd;
16 22
17exports.relative = relative; 23exports.relative = relative;
18 24
25exports.joinPath = joinPath;
26
19exports.resolve = path.resolve; 27exports.resolve = path.resolve;
28
29exports.dirname = path.dirname;
diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs
index b579577..205d3eb 100644
--- a/src/PursLoader/Loader.purs
+++ b/src/PursLoader/Loader.purs
@@ -4,28 +4,31 @@ module PursLoader.Loader
4 , loaderFn 4 , loaderFn
5 ) where 5 ) where
6 6
7import Prelude (Unit(), ($), (<>), (>>=), (<$>), (++), bind, flip, id, pure, return, unit) 7import Prelude (Unit(), ($), (<>), (>>=), (<$>), (++), bind, flip, id, pure, return, unit, show)
8 8
9import Control.Monad.Aff (Aff(), runAff) 9import Control.Monad.Aff (Aff(), runAff)
10import Control.Monad.Eff (Eff()) 10import Control.Monad.Eff (Eff())
11import Control.Monad.Eff.Class (liftEff) 11import Control.Monad.Eff.Class (liftEff)
12import Control.Monad.Eff.Exception (error) 12import Control.Monad.Eff.Exception (throwException, error, EXCEPTION())
13 13
14import Data.Array ((!!), concat) 14import Data.Array ((!!), concat)
15import Data.Function (Fn2(), mkFn2) 15import Data.Function (Fn2(), mkFn2)
16import Data.Maybe (Maybe(..), fromMaybe, maybe) 16import Data.Maybe (Maybe(..), fromMaybe, maybe)
17import Data.Either (Either(..))
17import Data.String (joinWith) 18import Data.String (joinWith)
18import Data.String.Regex (match, noFlags, regex, test) 19import Data.String.Regex (match, noFlags, regex, test)
19import Data.Traversable (sequence) 20import Data.Traversable (sequence)
21import Data.Foreign (F())
22import Data.Foreign.Class (read)
20 23
21import PursLoader.ChildProcess (ChildProcess(), spawn) 24import PursLoader.ChildProcess (ChildProcess(), spawn)
22import PursLoader.FS (FS(), writeFileUtf8, findFileUtf8) 25import PursLoader.FS (FS(), writeFileUtf8, findFileUtf8)
23import PursLoader.Glob (Glob(), globAll) 26import PursLoader.Glob (Glob(), globAll)
24import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query, clearDependencies, addDependency, resourcePath) 27import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query, clearDependencies, addDependency, resourcePath)
25import PursLoader.LoaderUtil (parseQuery) 28import PursLoader.LoaderUtil (parseQuery)
26import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions) 29import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions, Options(), output)
27 30
28type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader | eff) 31type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader, err :: EXCEPTION | eff)
29 32
30moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } 33moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true }
31 34
@@ -45,6 +48,10 @@ foreign import relative :: String -> String -> String
45 48
46foreign import resolve :: String -> String 49foreign import resolve :: String -> String
47 50
51foreign import dirname :: String -> String
52
53foreign import joinPath :: String -> String -> String
54
48mkPsci :: Array (Array String) -> Array (Array String) -> String 55mkPsci :: Array (Array String) -> Array (Array String) -> String
49mkPsci srcs ffis = joinWith "\n" ((loadModule <$> concat srcs) <> (loadForeign <$> concat ffis)) 56mkPsci srcs ffis = joinWith "\n" ((loadModule <$> concat srcs) <> (loadForeign <$> concat ffis))
50 where 57 where
@@ -66,32 +73,39 @@ loader' ref source = do
66 let parsed = parseQuery $ query ref 73 let parsed = parseQuery $ query ref
67 srcs = fromMaybe [] (loaderSrcOption parsed) 74 srcs = fromMaybe [] (loaderSrcOption parsed)
68 ffis = fromMaybe [] (loaderFFIOption parsed) 75 ffis = fromMaybe [] (loaderFFIOption parsed)
69 opts = pscOptions parsed
70 76
71 srcss <- globAll srcs 77 case read parsed :: F Options of
72 ffiss <- globAll ffis 78 Left e -> liftEff (throwException (error (show e)))
79 Right opts -> do
80 let pscOpts = pscOptions opts
81
82 srcss <- globAll srcs
83 ffiss <- globAll ffis
73 84
74 let psciFile = mkPsci srcss ffiss 85 let psciFile = mkPsci srcss ffiss
75 86
76 writeFileUtf8 psciFilename psciFile 87 writeFileUtf8 psciFilename psciFile
77 88
78 let moduleName = match moduleRegex source >>= (!!!) 1 >>= id 89 let moduleName = match moduleRegex source >>= (!!!) 1 >>= id
79 hasForeign = test foreignRegex source 90 hasForeign = test foreignRegex source
80 result = (\a -> "module.exports = require('" ++ a ++ "');") <$> moduleName 91 outputDir = resolve (output opts)
92 resourceDir = dirname (resourcePath ref)
93 result = (\a -> "module.exports = require('" ++ relative resourceDir (joinPath outputDir a) ++ "');") <$> moduleName
81 94
82 liftEff (clearDependencies ref) 95 liftEff do
83 liftEff (addDependency ref (resourcePath ref)) 96 clearDependencies ref
84 liftEff (sequence $ (\src -> addDependency ref (resolve src)) <$> concat srcss) 97 addDependency ref (resourcePath ref)
98 sequence $ (\src -> addDependency ref (resolve src)) <$> concat srcss
85 99
86 foreignPath <- if hasForeign 100 foreignPath <- if hasForeign
87 then fromMaybe (pure Nothing) (findFFI ffiss <$> moduleName) 101 then fromMaybe (pure Nothing) (findFFI ffiss <$> moduleName)
88 else pure Nothing 102 else pure Nothing
89 103
90 fromMaybe (pure unit) ((\path -> liftEff (addDependency ref path)) <$> foreignPath) 104 fromMaybe (pure unit) ((\path -> liftEff (addDependency ref path)) <$> foreignPath)
91 105
92 spawn pscCommand (srcs <> opts) 106 spawn pscCommand (srcs <> pscOpts)
93 107
94 return result 108 return result
95 109
96loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit 110loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit
97loader ref source = do 111loader ref source = do
diff --git a/src/PursLoader/Options.purs b/src/PursLoader/Options.purs
index e3957eb..1650652 100644
--- a/src/PursLoader/Options.purs
+++ b/src/PursLoader/Options.purs
@@ -2,9 +2,11 @@ module PursLoader.Options
2 ( pscOptions 2 ( pscOptions
3 , loaderSrcOption 3 , loaderSrcOption
4 , loaderFFIOption 4 , loaderFFIOption
5 , Options()
6 , output
5 ) where 7 ) where
6 8
7import Prelude (Unit(), (<>), (<$>), (<<<), (++), (<*>), const) 9import Prelude (Unit(), (<>), (<$>), (<<<), (++), (<*>), ($), const, id)
8 10
9import Data.Array (concat) 11import Data.Array (concat)
10import Data.Either (either) 12import Data.Either (either)
@@ -44,13 +46,16 @@ newtype Options
44 , noTco :: NullOrUndefined Boolean 46 , noTco :: NullOrUndefined Boolean
45 , verboseErrors :: NullOrUndefined Boolean 47 , verboseErrors :: NullOrUndefined Boolean
46 , comments :: NullOrUndefined Boolean 48 , comments :: NullOrUndefined Boolean
47 , output :: NullOrUndefined String 49 , output :: String
48 , noPrefix :: NullOrUndefined Boolean 50 , noPrefix :: NullOrUndefined Boolean
49 , requirePath :: NullOrUndefined String 51 , requirePath :: String
50 , src :: NullOrUndefined (Array String) 52 , src :: NullOrUndefined (Array String)
51 , ffi :: NullOrUndefined (Array String) 53 , ffi :: NullOrUndefined (Array String)
52 } 54 }
53 55
56output :: Options -> String
57output (Options o) = o.output
58
54instance isForeignOptions :: IsForeign Options where 59instance isForeignOptions :: IsForeign Options where
55 read obj = Options <$> ({ noPrelude: _ 60 read obj = Options <$> ({ noPrelude: _
56 , noOpts: _ 61 , noOpts: _
@@ -60,7 +65,7 @@ instance isForeignOptions :: IsForeign Options where
60 , comments: _ 65 , comments: _
61 , output: _ 66 , output: _
62 , noPrefix: _ 67 , noPrefix: _
63 , requirePath: _ 68 , requirePath: "../"
64 , src: _ 69 , src: _
65 , ffi: _ 70 , ffi: _
66 } <$> readProp noPreludeOpt obj 71 } <$> readProp noPreludeOpt obj
@@ -69,9 +74,8 @@ instance isForeignOptions :: IsForeign Options where
69 <*> readProp noTcoOpt obj 74 <*> readProp noTcoOpt obj
70 <*> readProp verboseErrorsOpt obj 75 <*> readProp verboseErrorsOpt obj
71 <*> readProp commentsOpt obj 76 <*> readProp commentsOpt obj
72 <*> readProp outputOpt obj 77 <*> (maybe "output" id <<< runNullOrUndefined <$> readProp outputOpt obj)
73 <*> readProp noPrefixOpt obj 78 <*> readProp noPrefixOpt obj
74 <*> readProp requirePathOpt obj
75 <*> readProp srcOpt obj 79 <*> readProp srcOpt obj
76 <*> readProp ffiOpt obj) 80 <*> readProp ffiOpt obj)
77 81
@@ -88,19 +92,17 @@ instance arrayLoaderOption :: (LoaderOption a) => LoaderOption (Array a) where
88 opt key val = concat (opt key <$> (NullOrUndefined <<< Just) 92 opt key val = concat (opt key <$> (NullOrUndefined <<< Just)
89 <$> (fromMaybe [] (runNullOrUndefined val))) 93 <$> (fromMaybe [] (runNullOrUndefined val)))
90 94
91pscOptions :: Foreign -> Array String 95pscOptions :: Options -> Array String
92pscOptions query = either (const []) fold parsed 96pscOptions (Options a) = opt noPreludeOpt a.noPrelude <>
93 where parsed = read query :: F Options 97 opt noOptsOpt a.noOpts <>
94 fold (Options a) = opt noPreludeOpt a.noPrelude <> 98 opt noMagicDoOpt a.noMagicDo <>
95 opt noOptsOpt a.noOpts <> 99 opt noTcoOpt a.noTco <>
96 opt noMagicDoOpt a.noMagicDo <> 100 opt verboseErrorsOpt a.verboseErrors <>
97 opt noTcoOpt a.noTco <> 101 opt commentsOpt a.comments <>
98 opt verboseErrorsOpt a.verboseErrors <> 102 opt outputOpt (NullOrUndefined $ Just a.output) <>
99 opt commentsOpt a.comments <> 103 opt noPrefixOpt a.noPrefix <>
100 opt outputOpt a.output <> 104 opt requirePathOpt (NullOrUndefined $ Just a.requirePath) <>
101 opt noPrefixOpt a.noPrefix <> 105 opt ffiOpt a.ffi
102 opt requirePathOpt a.requirePath <>
103 opt ffiOpt a.ffi
104 106
105loaderSrcOption :: Foreign -> Maybe (Array String) 107loaderSrcOption :: Foreign -> Maybe (Array String)
106loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query) 108loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query)