, 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)
, 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)
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)
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 =
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
+++ /dev/null
-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"
module PursLoader.Plugin
- ( Result()
- , Compile()
+ ( Compile()
, Context()
- , ImmutableMap()
+ , Options()
, DependencyGraph()
- , get
, dependenciesOf
) where
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))