]> git.immae.eu Git - github/fretlink/purs-loader.git/commitdiff
Merge pull request #43 from ethul/topic/optional-bundle
authoreric <thul.eric@gmail.com>
Sat, 12 Mar 2016 19:46:01 +0000 (14:46 -0500)
committereric <thul.eric@gmail.com>
Sat, 12 Mar 2016 19:46:01 +0000 (14:46 -0500)
Handle optional bundling by the compiler

12 files changed:
README.md
docs/PursLoader/LoaderRef.md
docs/PursLoader/Plugin.md
package.json
src/PursLoader/Loader.purs
src/PursLoader/LoaderRef.js
src/PursLoader/LoaderRef.purs
src/PursLoader/LoaderUtil.js [deleted file]
src/PursLoader/LoaderUtil.purs [deleted file]
src/PursLoader/Options.purs [deleted file]
src/PursLoader/Plugin.js
src/PursLoader/Plugin.purs

index 0e1ce2fc1b1b5529409ca588a80d73b9da890cc8..e93e96987d0a6334ac83c388168003ca28455647 100644 (file)
--- a/README.md
+++ b/README.md
@@ -12,12 +12,6 @@ This loader works in conjunction with the [PureScript webpack plugin](https://np
 npm install purs-loader --save-dev
 ```
 
-## Options
-
-###### `bundleOutput` (String)
-
-Relative path to the bundled JavaScript file generated by the `PurescriptWebpackPlugin`. The default value is `output/bundle.js`.
-
 ## Example
 
 ```js
index 60341f7e28fdbcbe5293e7ba55058a9b690efbe0..917db3abb21d721980c61a8866fa3bcb5c821981 100644 (file)
@@ -30,12 +30,6 @@ async :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) (Maybe Error -> S
 cacheable :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit
 ```
 
-#### `query`
-
-``` purescript
-query :: LoaderRef -> String
-```
-
 #### `clearDependencies`
 
 ``` purescript
index 9abec4df29eee5739123baf406c0ff36cd9215c8..7a524da94a35cb48427d623cbb504b72f05af176 100644 (file)
@@ -1,27 +1,21 @@
 ## Module PursLoader.Plugin
 
-#### `Result`
-
-``` purescript
-type Result = { srcMap :: ImmutableMap String String, ffiMap :: ImmutableMap String String, graph :: DependencyGraph }
-```
-
 #### `Compile`
 
 ``` purescript
-type Compile eff = Nullable Error -> Result -> Eff eff Unit
+type Compile eff = Nullable Error -> DependencyGraph -> Eff eff Unit
 ```
 
 #### `Context`
 
 ``` purescript
-type Context eff = { compile :: Compile eff -> Eff eff Unit }
+type Context eff = { compile :: Compile eff -> Eff eff Unit, options :: Options }
 ```
 
-#### `get`
+#### `Options`
 
 ``` purescript
-get :: forall key value. ImmutableMap key value -> key -> Maybe value
+type Options = { bundle :: Boolean, output :: String, bundleOutput :: String }
 ```
 
 #### `dependenciesOf`
@@ -30,12 +24,6 @@ get :: forall key value. ImmutableMap key value -> key -> Maybe value
 dependenciesOf :: DependencyGraph -> String -> Either Error (Array String)
 ```
 
-#### `ImmutableMap`
-
-``` purescript
-data ImmutableMap :: * -> * -> *
-```
-
 #### `DependencyGraph`
 
 ``` purescript
index 1491de6941e6a64f16929dc814449f5cfe69b160..b0c119dd46c6ca698f9e7d2f4181b07a1f093734 100644 (file)
@@ -26,7 +26,6 @@
   "dependencies": {
     "async": "^1.3.0",
     "chalk": "^1.1.0",
-    "debug": "^2.2.0",
-    "loader-utils": "^0.2.6"
+    "debug": "^2.2.0"
   }
 }
index d1068b6b91b896939b9cf491cfd694ab168fe91e..402e805b2afb835b35485eca02f3e750410f8ccf 100644 (file)
@@ -4,18 +4,15 @@ module PursLoader.Loader
   , loaderFn
   ) where
 
-import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), bind, const, id, pure, unit)
+import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), (<<<), bind, const, id, pure, unit)
 
-import Control.Apply ((*>))
 import Control.Alt ((<|>))
 import Control.Bind (join)
 import Control.Monad.Eff (Eff(), foreachE)
 import Control.Monad.Eff.Exception (Error(), error)
 
 import Data.Array ((!!))
-import Data.Bifunctor (lmap)
 import Data.Either (Either(..), either)
-import Data.Foreign.Class (read)
 import Data.Function (Fn2(), mkFn2)
 import Data.Maybe (Maybe(..), maybe)
 import Data.Nullable (toMaybe)
@@ -29,16 +26,12 @@ import PursLoader.LoaderRef
   , Loader()
   , async
   , cacheable
-  , query
-  , clearDependencies
   , addDependency
   , resourcePath
   )
 
 import PursLoader.Debug (debug)
-import PursLoader.LoaderUtil (parseQuery)
-import PursLoader.Options (Options(..))
-import PursLoader.Path (dirname, relative)
+import PursLoader.Path (dirname, joinPath, relative)
 import PursLoader.Plugin as Plugin
 
 type Effects eff = (loader :: Loader | eff)
@@ -57,9 +50,7 @@ loader ref source = do
   pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext
 
   compile :: AsyncCallback eff -> Plugin.Compile (Effects eff)
-  compile callback error' { srcMap, ffiMap, graph } = do
-    clearDependencies ref
-
+  compile callback error' graph = do
     either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name
 
     addDependency ref (resourcePath ref)
@@ -69,24 +60,35 @@ loader ref source = do
     where
     handle :: String -> Array String -> String -> Eff (Effects eff) Unit
     handle name' deps res = do
-      debug ("Adding PureScript transitive dependencies for " ++ name')
-      addTransitive name'
-      foreachE deps addTransitive
+      debug ("Adding PureScript dependencies for " ++ name')
+      foreachE deps (addDependency ref)
       debug "Generated loader result"
       debug res
       callback (toMaybe error') res
 
     exports :: Either Error String
-    exports = (\a b -> "module.exports = require('" ++ a ++ "')['" ++ b ++ "'];") <$> path <*> name
+    exports =
+      if pluginContext.options.bundle
+         then bundleExport <$> name
+         else moduleExport <<< modulePath <$> name
+      where
+      bundleExport :: String -> String
+      bundleExport name' = "module.exports = require('" ++ path ++ "')['" ++ name' ++ "'];"
+        where
+        path :: String
+        path = relative resourceDir pluginContext.options.bundleOutput
 
-    dependencies :: Either Error (Array String)
-    dependencies = name >>= Plugin.dependenciesOf graph
+      moduleExport :: String -> String
+      moduleExport path = "module.exports = require('" ++ path ++ "');"
 
-    addTransitive :: String -> Eff (Effects eff) Unit
-    addTransitive dep = addDep (Plugin.get srcMap dep) *> addDep (Plugin.get ffiMap dep)
-      where
-      addDep :: Maybe String -> Eff (Effects eff) Unit
-      addDep = maybe (pure unit) (addDependency ref)
+      modulePath :: String -> String
+      modulePath = relative resourceDir <<< joinPath pluginContext.options.output
+
+      resourceDir :: String
+      resourceDir = dirname (resourcePath ref)
+
+    dependencies :: Either Error (Array String)
+    dependencies = Plugin.dependenciesOf graph (resourcePath ref)
 
     name :: Either Error String
     name =
@@ -96,16 +98,5 @@ loader ref source = do
       re :: Regex
       re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true }
 
-    path :: Either Error String
-    path = (\(Options opts) -> relative resourceDir opts.bundleOutput) <$> options
-      where
-      options :: Either Error Options
-      options =
-        lmap (const $ error "Failed to parse loader query")
-             (read $ parseQuery (query ref))
-
-      resourceDir :: String
-      resourceDir = dirname (resourcePath ref)
-
 loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit)
 loaderFn = mkFn2 loader
index 3ce0970d0bd729a9319da392c7af36789553d183..a5d8e1fee8aa635b16061f55100189b3cbaab6e6 100644 (file)
@@ -21,10 +21,6 @@ function cacheable(ref){
   };
 }
 
-function query(ref){
-  return ref.query;
-}
-
 function clearDependencies(ref){
   return function(){
     return ref.clearDependencies();
@@ -47,8 +43,6 @@ exports.asyncFn = asyncFn;
 
 exports.cacheable = cacheable;
 
-exports.query = query;
-
 exports.clearDependencies = clearDependencies;
 
 exports.resourcePath = resourcePath;
index 87d60065007b3f7631c85c727a4bba7b9d472c03..140d94ab9f94d106f438b820d36e755a1f9f2aa5 100644 (file)
@@ -4,7 +4,6 @@ module PursLoader.LoaderRef
   , AsyncCallback()
   , async
   , cacheable
-  , query
   , clearDependencies
   , addDependency
   , resourcePath
@@ -34,8 +33,6 @@ async ref = runFn3 asyncFn isJust fromMaybe ref
 
 foreign import cacheable :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit
 
-foreign import query :: LoaderRef -> String
-
 foreign import clearDependencies :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit
 
 foreign import resourcePath :: LoaderRef -> String
diff --git a/src/PursLoader/LoaderUtil.js b/src/PursLoader/LoaderUtil.js
deleted file mode 100644 (file)
index 45f2703..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-'use strict';
-
-// module PursLoader.LoaderUtil
-
-var loaderUtils = require('loader-utils');
-
-exports.parseQuery = loaderUtils.parseQuery;
diff --git a/src/PursLoader/LoaderUtil.purs b/src/PursLoader/LoaderUtil.purs
deleted file mode 100644 (file)
index 45495c8..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-module PursLoader.LoaderUtil
-  ( parseQuery
-  ) where
-
-import Data.Foreign (Foreign())
-
-foreign import parseQuery :: String -> Foreign
diff --git a/src/PursLoader/Options.purs b/src/PursLoader/Options.purs
deleted file mode 100644 (file)
index 0c1453e..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-module PursLoader.Options
-  ( Options(..)
-  , runOptions
-  ) where
-
-import Prelude ((<$>), (<<<), id)
-
-import Data.Foreign.Class (IsForeign, readProp)
-import Data.Foreign.NullOrUndefined (runNullOrUndefined)
-import Data.Maybe (maybe)
-
-import PursLoader.Path (joinPath)
-
-newtype Options = Options { bundleOutput :: String }
-
-type Options_ = { bundleOutput :: String }
-
-runOptions :: Options -> Options_
-runOptions (Options options) = options
-
-instance isForeignOptions :: IsForeign Options where
-  read obj =
-    Options <$> ({ bundleOutput: _ }
-            <$> (maybe bundleOutputDefault id <<< runNullOrUndefined <$> readProp bundleOutput obj))
-    where
-    bundleOutput :: String
-    bundleOutput = "bundleOutput"
-
-    bundleOutputDefault :: String
-    bundleOutputDefault = joinPath "output" "bundle.js"
index 90feb337038fb9e5e136e7cace588084e91f3ccf..ded6df551f556095ef32bc6ac751fe5f4a526ce8 100644 (file)
@@ -2,12 +2,6 @@
 
 // module PursLoader.Plugin
 
-function getFn(nothing, just, map, key) {
-  var value = map.get(key);
-  return value === undefined ? nothing : just(value);
-}
-exports.getFn = getFn;
-
 function dependenciesOfFn(left, right, graph, node) {
   try {
     var dependencies = graph.dependenciesOf(node);
index 23f8600436478fdd0ed1c585d59f2b3718149046..c798c836ceef7e4b7044c838c157331e2906b7de 100644 (file)
@@ -1,10 +1,8 @@
 module PursLoader.Plugin
-  ( Result()
-  , Compile()
+  ( Compile()
   , Context()
-  , ImmutableMap()
+  , Options()
   , DependencyGraph()
-  , get
   , dependenciesOf
   ) where
 
@@ -15,32 +13,19 @@ import Control.Monad.Eff.Exception (Error())
 
 import Data.Either (Either(..))
 import Data.Function (Fn4(), runFn4)
-import Data.Maybe (Maybe(..))
 import Data.Nullable (Nullable())
 
-type Result = { srcMap :: ImmutableMap String String, ffiMap :: ImmutableMap String String, graph :: DependencyGraph }
+type Compile eff = Nullable Error -> DependencyGraph -> Eff eff Unit
 
-type Compile eff = Nullable Error -> Result -> Eff eff Unit
+type Context eff = { compile :: Compile eff -> Eff eff Unit, options :: Options }
 
-type Context eff = { compile :: Compile eff -> Eff eff Unit }
-
-get :: forall key value. ImmutableMap key value -> key -> Maybe value
-get = runFn4 getFn Nothing Just
+type Options = { bundle :: Boolean, output :: String, bundleOutput :: String }
 
 dependenciesOf :: DependencyGraph -> String -> Either Error (Array String)
 dependenciesOf = runFn4 dependenciesOfFn Left Right
 
-foreign import data ImmutableMap :: * -> * -> *
-
 foreign import data DependencyGraph :: *
 
-foreign import getFn
-  :: forall key value. Fn4 (Maybe value)
-                           (value -> Maybe value)
-                           (ImmutableMap key value)
-                           key
-                           (Maybe value)
-
 foreign import dependenciesOfFn
   :: Fn4 (Error -> Either Error (Array String))
          (Array String -> Either Error (Array String))