aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/PursLoader/Options.purs
diff options
context:
space:
mode:
authoreric thul <thul.eric@gmail.com>2015-12-25 18:41:33 -0500
committereric thul <thul.eric@gmail.com>2015-12-25 18:41:33 -0500
commit63d6a244462d050e119bde54a7063bae8a17e987 (patch)
treecce47ed541fa9ee8b2950945a89608b1c06fb8c9 /src/PursLoader/Options.purs
parent2e2da2be94720a739c595ec179a7ed49480ce753 (diff)
downloadpurs-loader-63d6a244462d050e119bde54a7063bae8a17e987.tar.gz
purs-loader-63d6a244462d050e119bde54a7063bae8a17e987.tar.zst
purs-loader-63d6a244462d050e119bde54a7063bae8a17e987.zip
Splitting PSC functionality into a separate plugin
The loader creates shim modules that reference their corresponding PureScript module that is bundled by the PureScript webpack plugin, which invokes `psc` and `psc-bundle`. Resolves #31 and resolves #32
Diffstat (limited to 'src/PursLoader/Options.purs')
-rw-r--r--src/PursLoader/Options.purs132
1 files changed, 20 insertions, 112 deletions
diff --git a/src/PursLoader/Options.purs b/src/PursLoader/Options.purs
index 3657d1a..132096c 100644
--- a/src/PursLoader/Options.purs
+++ b/src/PursLoader/Options.purs
@@ -1,122 +1,30 @@
1module PursLoader.Options 1module PursLoader.Options
2 ( pscOptions 2 ( Options()
3 , loaderSrcOption 3 , runOptions
4 , loaderFFIOption
5 , Options()
6 , output
7 ) where 4 ) where
8 5
9import Prelude ((<>), (<$>), (<<<), (++), (<*>), ($), const, id) 6import Prelude ((<$>), (<<<), id)
10 7
11import Data.Array (concat) 8import Data.Foreign.Class (IsForeign, readProp)
12import Data.Either (either) 9import Data.Foreign.NullOrUndefined (runNullOrUndefined)
10import Data.Maybe (maybe)
13 11
14import Data.Foreign (Foreign()) 12import PursLoader.Path (joinPath)
15import Data.Foreign.Class (IsForeign, read, readProp)
16import Data.Foreign.NullOrUndefined (NullOrUndefined(..), runNullOrUndefined)
17 13
18import Data.Maybe (Maybe(..), maybe, fromMaybe) 14newtype Options = Options { pscBundle :: String }
19 15
20noPreludeOpt :: String 16type Options_ = { pscBundle :: String }
21noPreludeOpt = "no-prelude"
22 17
23noOptsOpt :: String 18runOptions :: Options -> Options_
24noOptsOpt = "no-opts" 19runOptions (Options options) = options
25
26noMagicDoOpt :: String
27noMagicDoOpt = "no-magic-do"
28
29noTcoOpt :: String
30noTcoOpt = "no-tco"
31
32verboseErrorsOpt :: String
33verboseErrorsOpt = "verbose-errors"
34
35outputOpt :: String
36outputOpt = "output"
37
38commentsOpt :: String
39commentsOpt = "comments"
40
41noPrefixOpt :: String
42noPrefixOpt = "no-prefix"
43
44requirePathOpt :: String
45requirePathOpt = "require-path"
46
47srcOpt :: String
48srcOpt = "src"
49
50ffiOpt :: String
51ffiOpt = "ffi"
52
53newtype Options
54 = Options { noPrelude :: NullOrUndefined Boolean
55 , noOpts :: NullOrUndefined Boolean
56 , noMagicDo :: NullOrUndefined Boolean
57 , noTco :: NullOrUndefined Boolean
58 , verboseErrors :: NullOrUndefined Boolean
59 , comments :: NullOrUndefined Boolean
60 , output :: String
61 , noPrefix :: NullOrUndefined Boolean
62 , requirePath :: String
63 , src :: NullOrUndefined (Array String)
64 , ffi :: NullOrUndefined (Array String)
65 }
66
67output :: Options -> String
68output (Options o) = o.output
69 20
70instance isForeignOptions :: IsForeign Options where 21instance isForeignOptions :: IsForeign Options where
71 read obj = Options <$> ({ noPrelude: _ 22 read obj =
72 , noOpts: _ 23 Options <$> ({ pscBundle: _ }
73 , noMagicDo: _ 24 <$> (maybe pscBundleDefault id <<< runNullOrUndefined <$> readProp pscBundle obj))
74 , noTco: _ 25 where
75 , verboseErrors: _ 26 pscBundle :: String
76 , comments: _ 27 pscBundle = "pscBundle"
77 , output: _ 28
78 , noPrefix: _ 29 pscBundleDefault :: String
79 , requirePath: "../" 30 pscBundleDefault = joinPath "output" "bundle.js"
80 , src: _
81 , ffi: _
82 } <$> readProp noPreludeOpt obj
83 <*> readProp noOptsOpt obj
84 <*> readProp noMagicDoOpt obj
85 <*> readProp noTcoOpt obj
86 <*> readProp verboseErrorsOpt obj
87 <*> readProp commentsOpt obj
88 <*> (maybe "output" id <<< runNullOrUndefined <$> readProp outputOpt obj)
89 <*> readProp noPrefixOpt obj
90 <*> readProp srcOpt obj
91 <*> readProp ffiOpt obj)
92
93class LoaderOption a where
94 opt :: String -> NullOrUndefined a -> Array String
95
96instance booleanLoaderOption :: LoaderOption Boolean where
97 opt key val = maybe [] (\a -> if a then ["--" ++ key] else []) (runNullOrUndefined val)
98
99instance stringLoaderOption :: LoaderOption String where
100 opt key val = maybe [] (\a -> ["--" ++ key ++ "=" ++ a]) (runNullOrUndefined val)
101
102instance arrayLoaderOption :: (LoaderOption a) => LoaderOption (Array a) where
103 opt key val = concat (opt key <$> (NullOrUndefined <<< Just)
104 <$> (fromMaybe [] (runNullOrUndefined val)))
105
106pscOptions :: Options -> Array String
107pscOptions (Options a) = opt noPreludeOpt a.noPrelude <>
108 opt noOptsOpt a.noOpts <>
109 opt noMagicDoOpt a.noMagicDo <>
110 opt noTcoOpt a.noTco <>
111 opt verboseErrorsOpt a.verboseErrors <>
112 opt commentsOpt a.comments <>
113 opt outputOpt (NullOrUndefined $ Just a.output) <>
114 opt noPrefixOpt a.noPrefix <>
115 opt requirePathOpt (NullOrUndefined $ Just a.requirePath) <>
116 opt ffiOpt a.ffi
117
118loaderSrcOption :: Foreign -> Maybe (Array String)
119loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query)
120
121loaderFFIOption :: Foreign -> Maybe (Array String)
122loaderFFIOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.ffi) (read query)