diff options
author | eric <thul.eric@gmail.com> | 2016-03-12 14:46:01 -0500 |
---|---|---|
committer | eric <thul.eric@gmail.com> | 2016-03-12 14:46:01 -0500 |
commit | 55120f4502cb76e768f60654f3937db809df8ade (patch) | |
tree | f27800de3576466ea4d5502d9adb9cd57a583715 /src/PursLoader | |
parent | 17f22f868b851e50081844562627a7a8a414dcaa (diff) | |
parent | 845f3ec3c5b13a47d60b9ff2be14bf41fb5c4734 (diff) | |
download | purs-loader-55120f4502cb76e768f60654f3937db809df8ade.tar.gz purs-loader-55120f4502cb76e768f60654f3937db809df8ade.tar.zst purs-loader-55120f4502cb76e768f60654f3937db809df8ade.zip |
Merge pull request #43 from ethul/topic/optional-bundle
Handle optional bundling by the compiler
Diffstat (limited to 'src/PursLoader')
-rw-r--r-- | src/PursLoader/Loader.purs | 59 | ||||
-rw-r--r-- | src/PursLoader/LoaderRef.js | 6 | ||||
-rw-r--r-- | src/PursLoader/LoaderRef.purs | 3 | ||||
-rw-r--r-- | src/PursLoader/LoaderUtil.js | 7 | ||||
-rw-r--r-- | src/PursLoader/LoaderUtil.purs | 7 | ||||
-rw-r--r-- | src/PursLoader/Options.purs | 30 | ||||
-rw-r--r-- | src/PursLoader/Plugin.js | 6 | ||||
-rw-r--r-- | src/PursLoader/Plugin.purs | 25 |
8 files changed, 30 insertions, 113 deletions
diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs index d1068b6..402e805 100644 --- a/src/PursLoader/Loader.purs +++ b/src/PursLoader/Loader.purs | |||
@@ -4,18 +4,15 @@ module PursLoader.Loader | |||
4 | , loaderFn | 4 | , loaderFn |
5 | ) where | 5 | ) where |
6 | 6 | ||
7 | import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), bind, const, id, pure, unit) | 7 | import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), (<<<), bind, const, id, pure, unit) |
8 | 8 | ||
9 | import Control.Apply ((*>)) | ||
10 | import Control.Alt ((<|>)) | 9 | import Control.Alt ((<|>)) |
11 | import Control.Bind (join) | 10 | import Control.Bind (join) |
12 | import Control.Monad.Eff (Eff(), foreachE) | 11 | import Control.Monad.Eff (Eff(), foreachE) |
13 | import Control.Monad.Eff.Exception (Error(), error) | 12 | import Control.Monad.Eff.Exception (Error(), error) |
14 | 13 | ||
15 | import Data.Array ((!!)) | 14 | import Data.Array ((!!)) |
16 | import Data.Bifunctor (lmap) | ||
17 | import Data.Either (Either(..), either) | 15 | import Data.Either (Either(..), either) |
18 | import Data.Foreign.Class (read) | ||
19 | import Data.Function (Fn2(), mkFn2) | 16 | import Data.Function (Fn2(), mkFn2) |
20 | import Data.Maybe (Maybe(..), maybe) | 17 | import Data.Maybe (Maybe(..), maybe) |
21 | import Data.Nullable (toMaybe) | 18 | import Data.Nullable (toMaybe) |
@@ -29,16 +26,12 @@ import PursLoader.LoaderRef | |||
29 | , Loader() | 26 | , Loader() |
30 | , async | 27 | , async |
31 | , cacheable | 28 | , cacheable |
32 | , query | ||
33 | , clearDependencies | ||
34 | , addDependency | 29 | , addDependency |
35 | , resourcePath | 30 | , resourcePath |
36 | ) | 31 | ) |
37 | 32 | ||
38 | import PursLoader.Debug (debug) | 33 | import PursLoader.Debug (debug) |
39 | import PursLoader.LoaderUtil (parseQuery) | 34 | import PursLoader.Path (dirname, joinPath, relative) |
40 | import PursLoader.Options (Options(..)) | ||
41 | import PursLoader.Path (dirname, relative) | ||
42 | import PursLoader.Plugin as Plugin | 35 | import PursLoader.Plugin as Plugin |
43 | 36 | ||
44 | type Effects eff = (loader :: Loader | eff) | 37 | type Effects eff = (loader :: Loader | eff) |
@@ -57,9 +50,7 @@ loader ref source = do | |||
57 | pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext | 50 | pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext |
58 | 51 | ||
59 | compile :: AsyncCallback eff -> Plugin.Compile (Effects eff) | 52 | compile :: AsyncCallback eff -> Plugin.Compile (Effects eff) |
60 | compile callback error' { srcMap, ffiMap, graph } = do | 53 | compile callback error' graph = do |
61 | clearDependencies ref | ||
62 | |||
63 | either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name | 54 | either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name |
64 | 55 | ||
65 | addDependency ref (resourcePath ref) | 56 | addDependency ref (resourcePath ref) |
@@ -69,24 +60,35 @@ loader ref source = do | |||
69 | where | 60 | where |
70 | handle :: String -> Array String -> String -> Eff (Effects eff) Unit | 61 | handle :: String -> Array String -> String -> Eff (Effects eff) Unit |
71 | handle name' deps res = do | 62 | handle name' deps res = do |
72 | debug ("Adding PureScript transitive dependencies for " ++ name') | 63 | debug ("Adding PureScript dependencies for " ++ name') |
73 | addTransitive name' | 64 | foreachE deps (addDependency ref) |
74 | foreachE deps addTransitive | ||
75 | debug "Generated loader result" | 65 | debug "Generated loader result" |
76 | debug res | 66 | debug res |
77 | callback (toMaybe error') res | 67 | callback (toMaybe error') res |
78 | 68 | ||
79 | exports :: Either Error String | 69 | exports :: Either Error String |
80 | exports = (\a b -> "module.exports = require('" ++ a ++ "')['" ++ b ++ "'];") <$> path <*> name | 70 | exports = |
71 | if pluginContext.options.bundle | ||
72 | then bundleExport <$> name | ||
73 | else moduleExport <<< modulePath <$> name | ||
74 | where | ||
75 | bundleExport :: String -> String | ||
76 | bundleExport name' = "module.exports = require('" ++ path ++ "')['" ++ name' ++ "'];" | ||
77 | where | ||
78 | path :: String | ||
79 | path = relative resourceDir pluginContext.options.bundleOutput | ||
81 | 80 | ||
82 | dependencies :: Either Error (Array String) | 81 | moduleExport :: String -> String |
83 | dependencies = name >>= Plugin.dependenciesOf graph | 82 | moduleExport path = "module.exports = require('" ++ path ++ "');" |
84 | 83 | ||
85 | addTransitive :: String -> Eff (Effects eff) Unit | 84 | modulePath :: String -> String |
86 | addTransitive dep = addDep (Plugin.get srcMap dep) *> addDep (Plugin.get ffiMap dep) | 85 | modulePath = relative resourceDir <<< joinPath pluginContext.options.output |
87 | where | 86 | |
88 | addDep :: Maybe String -> Eff (Effects eff) Unit | 87 | resourceDir :: String |
89 | addDep = maybe (pure unit) (addDependency ref) | 88 | resourceDir = dirname (resourcePath ref) |
89 | |||
90 | dependencies :: Either Error (Array String) | ||
91 | dependencies = Plugin.dependenciesOf graph (resourcePath ref) | ||
90 | 92 | ||
91 | name :: Either Error String | 93 | name :: Either Error String |
92 | name = | 94 | name = |
@@ -96,16 +98,5 @@ loader ref source = do | |||
96 | re :: Regex | 98 | re :: Regex |
97 | re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } | 99 | re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } |
98 | 100 | ||
99 | path :: Either Error String | ||
100 | path = (\(Options opts) -> relative resourceDir opts.bundleOutput) <$> options | ||
101 | where | ||
102 | options :: Either Error Options | ||
103 | options = | ||
104 | lmap (const $ error "Failed to parse loader query") | ||
105 | (read $ parseQuery (query ref)) | ||
106 | |||
107 | resourceDir :: String | ||
108 | resourceDir = dirname (resourcePath ref) | ||
109 | |||
110 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) | 101 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) |
111 | loaderFn = mkFn2 loader | 102 | loaderFn = mkFn2 loader |
diff --git a/src/PursLoader/LoaderRef.js b/src/PursLoader/LoaderRef.js index 3ce0970..a5d8e1f 100644 --- a/src/PursLoader/LoaderRef.js +++ b/src/PursLoader/LoaderRef.js | |||
@@ -21,10 +21,6 @@ function cacheable(ref){ | |||
21 | }; | 21 | }; |
22 | } | 22 | } |
23 | 23 | ||
24 | function query(ref){ | ||
25 | return ref.query; | ||
26 | } | ||
27 | |||
28 | function clearDependencies(ref){ | 24 | function clearDependencies(ref){ |
29 | return function(){ | 25 | return function(){ |
30 | return ref.clearDependencies(); | 26 | return ref.clearDependencies(); |
@@ -47,8 +43,6 @@ exports.asyncFn = asyncFn; | |||
47 | 43 | ||
48 | exports.cacheable = cacheable; | 44 | exports.cacheable = cacheable; |
49 | 45 | ||
50 | exports.query = query; | ||
51 | |||
52 | exports.clearDependencies = clearDependencies; | 46 | exports.clearDependencies = clearDependencies; |
53 | 47 | ||
54 | exports.resourcePath = resourcePath; | 48 | exports.resourcePath = resourcePath; |
diff --git a/src/PursLoader/LoaderRef.purs b/src/PursLoader/LoaderRef.purs index 87d6006..140d94a 100644 --- a/src/PursLoader/LoaderRef.purs +++ b/src/PursLoader/LoaderRef.purs | |||
@@ -4,7 +4,6 @@ module PursLoader.LoaderRef | |||
4 | , AsyncCallback() | 4 | , AsyncCallback() |
5 | , async | 5 | , async |
6 | , cacheable | 6 | , cacheable |
7 | , query | ||
8 | , clearDependencies | 7 | , clearDependencies |
9 | , addDependency | 8 | , addDependency |
10 | , resourcePath | 9 | , resourcePath |
@@ -34,8 +33,6 @@ async ref = runFn3 asyncFn isJust fromMaybe ref | |||
34 | 33 | ||
35 | foreign import cacheable :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit | 34 | foreign import cacheable :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit |
36 | 35 | ||
37 | foreign import query :: LoaderRef -> String | ||
38 | |||
39 | foreign import clearDependencies :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit | 36 | foreign import clearDependencies :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit |
40 | 37 | ||
41 | foreign import resourcePath :: LoaderRef -> String | 38 | foreign import resourcePath :: LoaderRef -> String |
diff --git a/src/PursLoader/LoaderUtil.js b/src/PursLoader/LoaderUtil.js deleted file mode 100644 index 45f2703..0000000 --- a/src/PursLoader/LoaderUtil.js +++ /dev/null | |||
@@ -1,7 +0,0 @@ | |||
1 | 'use strict'; | ||
2 | |||
3 | // module PursLoader.LoaderUtil | ||
4 | |||
5 | var loaderUtils = require('loader-utils'); | ||
6 | |||
7 | exports.parseQuery = loaderUtils.parseQuery; | ||
diff --git a/src/PursLoader/LoaderUtil.purs b/src/PursLoader/LoaderUtil.purs deleted file mode 100644 index 45495c8..0000000 --- a/src/PursLoader/LoaderUtil.purs +++ /dev/null | |||
@@ -1,7 +0,0 @@ | |||
1 | module PursLoader.LoaderUtil | ||
2 | ( parseQuery | ||
3 | ) where | ||
4 | |||
5 | import Data.Foreign (Foreign()) | ||
6 | |||
7 | foreign import parseQuery :: String -> Foreign | ||
diff --git a/src/PursLoader/Options.purs b/src/PursLoader/Options.purs deleted file mode 100644 index 0c1453e..0000000 --- a/src/PursLoader/Options.purs +++ /dev/null | |||
@@ -1,30 +0,0 @@ | |||
1 | module PursLoader.Options | ||
2 | ( Options(..) | ||
3 | , runOptions | ||
4 | ) where | ||
5 | |||
6 | import Prelude ((<$>), (<<<), id) | ||
7 | |||
8 | import Data.Foreign.Class (IsForeign, readProp) | ||
9 | import Data.Foreign.NullOrUndefined (runNullOrUndefined) | ||
10 | import Data.Maybe (maybe) | ||
11 | |||
12 | import PursLoader.Path (joinPath) | ||
13 | |||
14 | newtype Options = Options { bundleOutput :: String } | ||
15 | |||
16 | type Options_ = { bundleOutput :: String } | ||
17 | |||
18 | runOptions :: Options -> Options_ | ||
19 | runOptions (Options options) = options | ||
20 | |||
21 | instance isForeignOptions :: IsForeign Options where | ||
22 | read obj = | ||
23 | Options <$> ({ bundleOutput: _ } | ||
24 | <$> (maybe bundleOutputDefault id <<< runNullOrUndefined <$> readProp bundleOutput obj)) | ||
25 | where | ||
26 | bundleOutput :: String | ||
27 | bundleOutput = "bundleOutput" | ||
28 | |||
29 | bundleOutputDefault :: String | ||
30 | bundleOutputDefault = joinPath "output" "bundle.js" | ||
diff --git a/src/PursLoader/Plugin.js b/src/PursLoader/Plugin.js index 90feb33..ded6df5 100644 --- a/src/PursLoader/Plugin.js +++ b/src/PursLoader/Plugin.js | |||
@@ -2,12 +2,6 @@ | |||
2 | 2 | ||
3 | // module PursLoader.Plugin | 3 | // module PursLoader.Plugin |
4 | 4 | ||
5 | function getFn(nothing, just, map, key) { | ||
6 | var value = map.get(key); | ||
7 | return value === undefined ? nothing : just(value); | ||
8 | } | ||
9 | exports.getFn = getFn; | ||
10 | |||
11 | function dependenciesOfFn(left, right, graph, node) { | 5 | function dependenciesOfFn(left, right, graph, node) { |
12 | try { | 6 | try { |
13 | var dependencies = graph.dependenciesOf(node); | 7 | var dependencies = graph.dependenciesOf(node); |
diff --git a/src/PursLoader/Plugin.purs b/src/PursLoader/Plugin.purs index 23f8600..c798c83 100644 --- a/src/PursLoader/Plugin.purs +++ b/src/PursLoader/Plugin.purs | |||
@@ -1,10 +1,8 @@ | |||
1 | module PursLoader.Plugin | 1 | module PursLoader.Plugin |
2 | ( Result() | 2 | ( Compile() |
3 | , Compile() | ||
4 | , Context() | 3 | , Context() |
5 | , ImmutableMap() | 4 | , Options() |
6 | , DependencyGraph() | 5 | , DependencyGraph() |
7 | , get | ||
8 | , dependenciesOf | 6 | , dependenciesOf |
9 | ) where | 7 | ) where |
10 | 8 | ||
@@ -15,32 +13,19 @@ import Control.Monad.Eff.Exception (Error()) | |||
15 | 13 | ||
16 | import Data.Either (Either(..)) | 14 | import Data.Either (Either(..)) |
17 | import Data.Function (Fn4(), runFn4) | 15 | import Data.Function (Fn4(), runFn4) |
18 | import Data.Maybe (Maybe(..)) | ||
19 | import Data.Nullable (Nullable()) | 16 | import Data.Nullable (Nullable()) |
20 | 17 | ||
21 | type Result = { srcMap :: ImmutableMap String String, ffiMap :: ImmutableMap String String, graph :: DependencyGraph } | 18 | type Compile eff = Nullable Error -> DependencyGraph -> Eff eff Unit |
22 | 19 | ||
23 | type Compile eff = Nullable Error -> Result -> Eff eff Unit | 20 | type Context eff = { compile :: Compile eff -> Eff eff Unit, options :: Options } |
24 | 21 | ||
25 | type Context eff = { compile :: Compile eff -> Eff eff Unit } | 22 | type Options = { bundle :: Boolean, output :: String, bundleOutput :: String } |
26 | |||
27 | get :: forall key value. ImmutableMap key value -> key -> Maybe value | ||
28 | get = runFn4 getFn Nothing Just | ||
29 | 23 | ||
30 | dependenciesOf :: DependencyGraph -> String -> Either Error (Array String) | 24 | dependenciesOf :: DependencyGraph -> String -> Either Error (Array String) |
31 | dependenciesOf = runFn4 dependenciesOfFn Left Right | 25 | dependenciesOf = runFn4 dependenciesOfFn Left Right |
32 | 26 | ||
33 | foreign import data ImmutableMap :: * -> * -> * | ||
34 | |||
35 | foreign import data DependencyGraph :: * | 27 | foreign import data DependencyGraph :: * |
36 | 28 | ||
37 | foreign import getFn | ||
38 | :: forall key value. Fn4 (Maybe value) | ||
39 | (value -> Maybe value) | ||
40 | (ImmutableMap key value) | ||
41 | key | ||
42 | (Maybe value) | ||
43 | |||
44 | foreign import dependenciesOfFn | 29 | foreign import dependenciesOfFn |
45 | :: Fn4 (Error -> Either Error (Array String)) | 30 | :: Fn4 (Error -> Either Error (Array String)) |
46 | (Array String -> Either Error (Array String)) | 31 | (Array String -> Either Error (Array String)) |