aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/PursLoader/Loader.purs
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/Loader.purs
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/Loader.purs')
-rw-r--r--src/PursLoader/Loader.purs56
1 files changed, 35 insertions, 21 deletions
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