]> git.immae.eu Git - github/fretlink/purs-loader.git/commitdiff
Merge branch 'master' into topic/stderr
authoreric thul <thul.eric@gmail.com>
Sat, 12 Mar 2016 19:55:05 +0000 (14:55 -0500)
committereric thul <thul.eric@gmail.com>
Sat, 12 Mar 2016 19:55:05 +0000 (14:55 -0500)
1  2 
docs/PursLoader/Plugin.md
src/PursLoader/Loader.purs
src/PursLoader/Plugin.purs

index 645c41bcc64bfac91f73b6d86365af5b980124aa,7a524da94a35cb48427d623cbb504b72f05af176..9f9f8520a2a3153db654d567be63a6e0722ab1bf
@@@ -3,13 -3,7 +3,7 @@@
  #### `Compile`
  
  ``` purescript
- type Compile eff = Nullable Error -> Result -> Eff eff Unit
 -type Compile eff = Nullable Error -> DependencyGraph -> Eff eff Unit
++type Compile eff = Nullable Error -> DependencyGraph -> String -> Eff eff Unit
  ```
  
  #### `Context`
index e1b9e0f36fa8dede7c11121cb0a5232b1bd1501b,402e805b2afb835b35485eca02f3e750410f8ccf..c50c63c9d748c83ab31118489aaacfda499628d1
@@@ -5,20 -4,17 +5,17 @@@ module PursLoader.Loade
    , loaderFn
    ) where
  
- import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), bind, const, id, pure, void, unit)
 -import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), (<<<), bind, const, id, pure, unit)
++import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), (<<<), bind, const, id, pure, unit, void)
  
- import Control.Apply ((*>))
 -import Control.Alt ((<|>))
  import Control.Bind (join)
  import Control.Monad.Eff (Eff(), foreachE)
 -import Control.Monad.Eff.Exception (Error(), error)
 +import Control.Monad.Eff.Console (CONSOLE())
 +import Control.Monad.Eff.Exception (EXCEPTION(), Error(), error, message)
  
  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.Maybe (Maybe(..), maybe)
++import Data.Maybe (maybe)
  import Data.Nullable (toMaybe)
  import Data.String.Regex (Regex(), match, noFlags, regex)
  
@@@ -41,16 -31,12 +36,14 @@@ import PursLoader.LoaderRe
    )
  
  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)
 +type Effects eff = (console :: CONSOLE, err :: EXCEPTION | eff)
 +
 +type Effects_ eff = Effects (loader :: Loader | eff)
  
 -loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit
 +loader :: forall eff. LoaderRef -> String -> Eff (Effects_ eff) Unit
  loader ref source = do
    callback <- async ref
  
  
    pluginContext.compile (compile callback)
    where
 -  pluginContext :: Plugin.Context (Effects eff)
 +  pluginContext :: Plugin.Context (Effects_ eff)
    pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext
  
 -  compile :: AsyncCallback eff -> Plugin.Compile (Effects eff)
 -  compile callback error' graph = do
 +  compile :: AsyncCallback (Effects eff) -> Plugin.Compile (Effects_ eff)
-   compile callback error' { srcMap, ffiMap, graph, output } = do
-     clearDependencies ref
++  compile callback error' graph output = do
      either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name
  
      addDependency ref (resourcePath ref)
  
 -    either (\err -> callback (toMaybe error' <|> Just err) "") id
 +    void $ writeString stderr UTF8 output (pure unit)
 +
 +    maybe (pure unit) (\a -> void $ writeString stderr UTF8 (message a) (pure unit)) (toMaybe error')
 +
 +    either (const $ callback (pure fixedError) "") id
             (handle <$> name <*> dependencies <*> exports)
      where
 -    handle :: String -> Array String -> String -> Eff (Effects eff) Unit
 +    fixedError :: Error
 +    fixedError = error "PureScript compilation has failed."
 +
 +    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
 +      callback (const fixedError <$> 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 =
        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 :: forall eff. Fn2 LoaderRef String (Eff (Effects_ eff) Unit)
  loaderFn = mkFn2 loader
index c9f01333e2683b271a9a19dae3a8ef2079dbf011,c798c836ceef7e4b7044c838c157331e2906b7de..8bb53bec5e0b44b48c70abd21edd3ecd5a1c9263
@@@ -15,17 -13,13 +13,13 @@@ import Control.Monad.Eff.Exception (Err
  
  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, output :: String }
 -type Compile eff = Nullable Error -> DependencyGraph -> Eff eff Unit
++type Compile eff = Nullable Error -> DependencyGraph -> String -> 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