From 87145c4d29e9ab45d04c62d19e8697527728549f Mon Sep 17 00:00:00 2001 From: eric thul Date: Tue, 8 Mar 2016 21:58:55 -0500 Subject: Handle optional bundling by the compiler Resolves ethul/purescript-webpack-plugin#9 --- src/PursLoader/Loader.purs | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) (limited to 'src/PursLoader/Loader.purs') 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 , loaderFn ) where -import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), bind, const, id, pure, unit) +import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), (<<<), bind, const, id, pure, unit) import Control.Apply ((*>)) import Control.Alt ((<|>)) @@ -13,9 +13,7 @@ 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 +27,13 @@ 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) @@ -77,10 +72,31 @@ loader ref source = do 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) @@ -96,16 +112,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 -- cgit v1.2.3 From 845f3ec3c5b13a47d60b9ff2be14bf41fb5c4734 Mon Sep 17 00:00:00 2001 From: eric thul Date: Sat, 12 Mar 2016 14:33:55 -0500 Subject: Handling dependencies --- src/PursLoader/Loader.purs | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) (limited to 'src/PursLoader/Loader.purs') diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs index 0dfa20e..402e805 100644 --- a/src/PursLoader/Loader.purs +++ b/src/PursLoader/Loader.purs @@ -6,7 +6,6 @@ module PursLoader.Loader import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), (<<<), bind, const, id, pure, unit) -import Control.Apply ((*>)) import Control.Alt ((<|>)) import Control.Bind (join) import Control.Monad.Eff (Eff(), foreachE) @@ -27,7 +26,6 @@ import PursLoader.LoaderRef , Loader() , async , cacheable - , clearDependencies , addDependency , resourcePath ) @@ -52,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) @@ -64,9 +60,8 @@ 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 @@ -93,16 +88,7 @@ loader ref source = do resourceDir = dirname (resourcePath ref) dependencies :: Either Error (Array String) - 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) - where - addDep :: Maybe String -> Eff (Effects eff) Unit - addDep = maybe (pure unit) (addDependency ref) + dependencies = Plugin.dependenciesOf graph (resourcePath ref) name :: Either Error String name = -- cgit v1.2.3