diff options
author | eric thul <thul.eric@gmail.com> | 2016-03-08 21:58:55 -0500 |
---|---|---|
committer | eric thul <thul.eric@gmail.com> | 2016-03-08 22:01:40 -0500 |
commit | 87145c4d29e9ab45d04c62d19e8697527728549f (patch) | |
tree | 35bc21658cebc370f2d0aecee13a2188602bfd80 | |
parent | 17f22f868b851e50081844562627a7a8a414dcaa (diff) | |
download | purs-loader-87145c4d29e9ab45d04c62d19e8697527728549f.tar.gz purs-loader-87145c4d29e9ab45d04c62d19e8697527728549f.tar.zst purs-loader-87145c4d29e9ab45d04c62d19e8697527728549f.zip |
Handle optional bundling by the compiler
Resolves ethul/purescript-webpack-plugin#9
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | docs/PursLoader/Plugin.md | 8 | ||||
-rw-r--r-- | package.json | 3 | ||||
-rw-r--r-- | src/PursLoader/Loader.purs | 45 | ||||
-rw-r--r-- | src/PursLoader/LoaderRef.js | 4 | ||||
-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.purs | 5 |
10 files changed, 37 insertions, 81 deletions
@@ -12,12 +12,6 @@ This loader works in conjunction with the [PureScript webpack plugin](https://np | |||
12 | npm install purs-loader --save-dev | 12 | npm install purs-loader --save-dev |
13 | ``` | 13 | ``` |
14 | 14 | ||
15 | ## Options | ||
16 | |||
17 | ###### `bundleOutput` (String) | ||
18 | |||
19 | Relative path to the bundled JavaScript file generated by the `PurescriptWebpackPlugin`. The default value is `output/bundle.js`. | ||
20 | |||
21 | ## Example | 15 | ## Example |
22 | 16 | ||
23 | ```js | 17 | ```js |
diff --git a/docs/PursLoader/Plugin.md b/docs/PursLoader/Plugin.md index 9abec4d..26e3f26 100644 --- a/docs/PursLoader/Plugin.md +++ b/docs/PursLoader/Plugin.md | |||
@@ -15,7 +15,13 @@ type Compile eff = Nullable Error -> Result -> Eff eff Unit | |||
15 | #### `Context` | 15 | #### `Context` |
16 | 16 | ||
17 | ``` purescript | 17 | ``` purescript |
18 | type Context eff = { compile :: Compile eff -> Eff eff Unit } | 18 | type Context eff = { compile :: Compile eff -> Eff eff Unit, options :: Options } |
19 | ``` | ||
20 | |||
21 | #### `Options` | ||
22 | |||
23 | ``` purescript | ||
24 | type Options = { bundle :: Boolean, output :: String, bundleOutput :: String } | ||
19 | ``` | 25 | ``` |
20 | 26 | ||
21 | #### `get` | 27 | #### `get` |
diff --git a/package.json b/package.json index 1491de6..b0c119d 100644 --- a/package.json +++ b/package.json | |||
@@ -26,7 +26,6 @@ | |||
26 | "dependencies": { | 26 | "dependencies": { |
27 | "async": "^1.3.0", | 27 | "async": "^1.3.0", |
28 | "chalk": "^1.1.0", | 28 | "chalk": "^1.1.0", |
29 | "debug": "^2.2.0", | 29 | "debug": "^2.2.0" |
30 | "loader-utils": "^0.2.6" | ||
31 | } | 30 | } |
32 | } | 31 | } |
diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs index d1068b6..0dfa20e 100644 --- a/src/PursLoader/Loader.purs +++ b/src/PursLoader/Loader.purs | |||
@@ -4,7 +4,7 @@ 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 ((*>)) | 9 | import Control.Apply ((*>)) |
10 | import Control.Alt ((<|>)) | 10 | import Control.Alt ((<|>)) |
@@ -13,9 +13,7 @@ import Control.Monad.Eff (Eff(), foreachE) | |||
13 | import Control.Monad.Eff.Exception (Error(), error) | 13 | import Control.Monad.Eff.Exception (Error(), error) |
14 | 14 | ||
15 | import Data.Array ((!!)) | 15 | import Data.Array ((!!)) |
16 | import Data.Bifunctor (lmap) | ||
17 | import Data.Either (Either(..), either) | 16 | import Data.Either (Either(..), either) |
18 | import Data.Foreign.Class (read) | ||
19 | import Data.Function (Fn2(), mkFn2) | 17 | import Data.Function (Fn2(), mkFn2) |
20 | import Data.Maybe (Maybe(..), maybe) | 18 | import Data.Maybe (Maybe(..), maybe) |
21 | import Data.Nullable (toMaybe) | 19 | import Data.Nullable (toMaybe) |
@@ -29,16 +27,13 @@ import PursLoader.LoaderRef | |||
29 | , Loader() | 27 | , Loader() |
30 | , async | 28 | , async |
31 | , cacheable | 29 | , cacheable |
32 | , query | ||
33 | , clearDependencies | 30 | , clearDependencies |
34 | , addDependency | 31 | , addDependency |
35 | , resourcePath | 32 | , resourcePath |
36 | ) | 33 | ) |
37 | 34 | ||
38 | import PursLoader.Debug (debug) | 35 | import PursLoader.Debug (debug) |
39 | import PursLoader.LoaderUtil (parseQuery) | 36 | import PursLoader.Path (dirname, joinPath, relative) |
40 | import PursLoader.Options (Options(..)) | ||
41 | import PursLoader.Path (dirname, relative) | ||
42 | import PursLoader.Plugin as Plugin | 37 | import PursLoader.Plugin as Plugin |
43 | 38 | ||
44 | type Effects eff = (loader :: Loader | eff) | 39 | type Effects eff = (loader :: Loader | eff) |
@@ -77,10 +72,31 @@ loader ref source = do | |||
77 | callback (toMaybe error') res | 72 | callback (toMaybe error') res |
78 | 73 | ||
79 | exports :: Either Error String | 74 | exports :: Either Error String |
80 | exports = (\a b -> "module.exports = require('" ++ a ++ "')['" ++ b ++ "'];") <$> path <*> name | 75 | exports = |
76 | if pluginContext.options.bundle | ||
77 | then bundleExport <$> name | ||
78 | else moduleExport <<< modulePath <$> name | ||
79 | where | ||
80 | bundleExport :: String -> String | ||
81 | bundleExport name' = "module.exports = require('" ++ path ++ "')['" ++ name' ++ "'];" | ||
82 | where | ||
83 | path :: String | ||
84 | path = relative resourceDir pluginContext.options.bundleOutput | ||
85 | |||
86 | moduleExport :: String -> String | ||
87 | moduleExport path = "module.exports = require('" ++ path ++ "');" | ||
88 | |||
89 | modulePath :: String -> String | ||
90 | modulePath = relative resourceDir <<< joinPath pluginContext.options.output | ||
91 | |||
92 | resourceDir :: String | ||
93 | resourceDir = dirname (resourcePath ref) | ||
81 | 94 | ||
82 | dependencies :: Either Error (Array String) | 95 | dependencies :: Either Error (Array String) |
83 | dependencies = name >>= Plugin.dependenciesOf graph | 96 | dependencies = |
97 | if pluginContext.options.bundle | ||
98 | then name >>= Plugin.dependenciesOf graph | ||
99 | else pure [] | ||
84 | 100 | ||
85 | addTransitive :: String -> Eff (Effects eff) Unit | 101 | addTransitive :: String -> Eff (Effects eff) Unit |
86 | addTransitive dep = addDep (Plugin.get srcMap dep) *> addDep (Plugin.get ffiMap dep) | 102 | addTransitive dep = addDep (Plugin.get srcMap dep) *> addDep (Plugin.get ffiMap dep) |
@@ -96,16 +112,5 @@ loader ref source = do | |||
96 | re :: Regex | 112 | re :: Regex |
97 | re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } | 113 | re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } |
98 | 114 | ||
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) | 115 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) |
111 | loaderFn = mkFn2 loader | 116 | loaderFn = mkFn2 loader |
diff --git a/src/PursLoader/LoaderRef.js b/src/PursLoader/LoaderRef.js index 3ce0970..e92c72c 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(); |
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.purs b/src/PursLoader/Plugin.purs index 23f8600..520c786 100644 --- a/src/PursLoader/Plugin.purs +++ b/src/PursLoader/Plugin.purs | |||
@@ -2,6 +2,7 @@ module PursLoader.Plugin | |||
2 | ( Result() | 2 | ( Result() |
3 | , Compile() | 3 | , Compile() |
4 | , Context() | 4 | , Context() |
5 | , Options() | ||
5 | , ImmutableMap() | 6 | , ImmutableMap() |
6 | , DependencyGraph() | 7 | , DependencyGraph() |
7 | , get | 8 | , get |
@@ -22,7 +23,9 @@ type Result = { srcMap :: ImmutableMap String String, ffiMap :: ImmutableMap Str | |||
22 | 23 | ||
23 | type Compile eff = Nullable Error -> Result -> Eff eff Unit | 24 | type Compile eff = Nullable Error -> Result -> Eff eff Unit |
24 | 25 | ||
25 | type Context eff = { compile :: Compile eff -> Eff eff Unit } | 26 | type Context eff = { compile :: Compile eff -> Eff eff Unit, options :: Options } |
27 | |||
28 | type Options = { bundle :: Boolean, output :: String, bundleOutput :: String } | ||
26 | 29 | ||
27 | get :: forall key value. ImmutableMap key value -> key -> Maybe value | 30 | get :: forall key value. ImmutableMap key value -> key -> Maybe value |
28 | get = runFn4 getFn Nothing Just | 31 | get = runFn4 getFn Nothing Just |