, 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.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)
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
+
+ moduleExport :: String -> String
+ moduleExport path = "module.exports = require('" ++ path ++ "');"
+
+ modulePath :: String -> String
+ modulePath = relative resourceDir <<< joinPath pluginContext.options.output
+
+ resourceDir :: String
+ resourceDir = dirname (resourcePath ref)
dependencies :: Either Error (Array String)
- dependencies = name >>= Plugin.dependenciesOf graph
+ dependencies =
+ if pluginContext.options.bundle
+ then name >>= Plugin.dependenciesOf graph
+ else pure []
addTransitive :: String -> Eff (Effects eff) Unit
addTransitive dep = addDep (Plugin.get srcMap dep) *> addDep (Plugin.get ffiMap dep)
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"