diff options
author | Nathan Faubion <nathan@n-son.com> | 2015-11-02 14:04:58 -0600 |
---|---|---|
committer | Nathan Faubion <nathan@n-son.com> | 2015-11-02 14:16:21 -0600 |
commit | 2b620717603a6a2ba4d93ee1ca7334dc31500acf (patch) | |
tree | 7548d0a34052169d853ecd5959eb6f1663c129d1 /src/PursLoader/Loader.purs | |
parent | 8a0ac07ec2c568b8e9bd06cfd0429c6d6aa836e5 (diff) | |
download | purs-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.purs | 56 |
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 | ||
7 | import Prelude (Unit(), ($), (<>), (>>=), (<$>), (++), bind, flip, id, pure, return, unit) | 7 | import Prelude (Unit(), ($), (<>), (>>=), (<$>), (++), bind, flip, id, pure, return, unit, show) |
8 | 8 | ||
9 | import Control.Monad.Aff (Aff(), runAff) | 9 | import Control.Monad.Aff (Aff(), runAff) |
10 | import Control.Monad.Eff (Eff()) | 10 | import Control.Monad.Eff (Eff()) |
11 | import Control.Monad.Eff.Class (liftEff) | 11 | import Control.Monad.Eff.Class (liftEff) |
12 | import Control.Monad.Eff.Exception (error) | 12 | import Control.Monad.Eff.Exception (throwException, error, EXCEPTION()) |
13 | 13 | ||
14 | import Data.Array ((!!), concat) | 14 | import Data.Array ((!!), concat) |
15 | import Data.Function (Fn2(), mkFn2) | 15 | import Data.Function (Fn2(), mkFn2) |
16 | import Data.Maybe (Maybe(..), fromMaybe, maybe) | 16 | import Data.Maybe (Maybe(..), fromMaybe, maybe) |
17 | import Data.Either (Either(..)) | ||
17 | import Data.String (joinWith) | 18 | import Data.String (joinWith) |
18 | import Data.String.Regex (match, noFlags, regex, test) | 19 | import Data.String.Regex (match, noFlags, regex, test) |
19 | import Data.Traversable (sequence) | 20 | import Data.Traversable (sequence) |
21 | import Data.Foreign (F()) | ||
22 | import Data.Foreign.Class (read) | ||
20 | 23 | ||
21 | import PursLoader.ChildProcess (ChildProcess(), spawn) | 24 | import PursLoader.ChildProcess (ChildProcess(), spawn) |
22 | import PursLoader.FS (FS(), writeFileUtf8, findFileUtf8) | 25 | import PursLoader.FS (FS(), writeFileUtf8, findFileUtf8) |
23 | import PursLoader.Glob (Glob(), globAll) | 26 | import PursLoader.Glob (Glob(), globAll) |
24 | import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query, clearDependencies, addDependency, resourcePath) | 27 | import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query, clearDependencies, addDependency, resourcePath) |
25 | import PursLoader.LoaderUtil (parseQuery) | 28 | import PursLoader.LoaderUtil (parseQuery) |
26 | import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions) | 29 | import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions, Options(), output) |
27 | 30 | ||
28 | type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader | eff) | 31 | type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader, err :: EXCEPTION | eff) |
29 | 32 | ||
30 | moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } | 33 | moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } |
31 | 34 | ||
@@ -45,6 +48,10 @@ foreign import relative :: String -> String -> String | |||
45 | 48 | ||
46 | foreign import resolve :: String -> String | 49 | foreign import resolve :: String -> String |
47 | 50 | ||
51 | foreign import dirname :: String -> String | ||
52 | |||
53 | foreign import joinPath :: String -> String -> String | ||
54 | |||
48 | mkPsci :: Array (Array String) -> Array (Array String) -> String | 55 | mkPsci :: Array (Array String) -> Array (Array String) -> String |
49 | mkPsci srcs ffis = joinWith "\n" ((loadModule <$> concat srcs) <> (loadForeign <$> concat ffis)) | 56 | mkPsci 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 | ||
96 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit | 110 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit |
97 | loader ref source = do | 111 | loader ref source = do |