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 +++++++++++++++++++++++------------------- src/PursLoader/LoaderRef.js | 4 ---- src/PursLoader/LoaderRef.purs | 3 --- src/PursLoader/LoaderUtil.js | 7 ------- src/PursLoader/LoaderUtil.purs | 7 ------- src/PursLoader/Options.purs | 30 ---------------------------- src/PursLoader/Plugin.purs | 5 ++++- 7 files changed, 29 insertions(+), 72 deletions(-) delete mode 100644 src/PursLoader/LoaderUtil.js delete mode 100644 src/PursLoader/LoaderUtil.purs delete mode 100644 src/PursLoader/Options.purs (limited to 'src') 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 diff --git a/src/PursLoader/LoaderRef.js b/src/PursLoader/LoaderRef.js index 3ce0970..e92c72c 100644 --- a/src/PursLoader/LoaderRef.js +++ b/src/PursLoader/LoaderRef.js @@ -21,10 +21,6 @@ function cacheable(ref){ }; } -function query(ref){ - return ref.query; -} - function clearDependencies(ref){ return function(){ return ref.clearDependencies(); diff --git a/src/PursLoader/LoaderRef.purs b/src/PursLoader/LoaderRef.purs index 87d6006..140d94a 100644 --- a/src/PursLoader/LoaderRef.purs +++ b/src/PursLoader/LoaderRef.purs @@ -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 index 45f2703..0000000 --- a/src/PursLoader/LoaderUtil.js +++ /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 index 45495c8..0000000 --- a/src/PursLoader/LoaderUtil.purs +++ /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 index 0c1453e..0000000 --- a/src/PursLoader/Options.purs +++ /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" diff --git a/src/PursLoader/Plugin.purs b/src/PursLoader/Plugin.purs index 23f8600..520c786 100644 --- a/src/PursLoader/Plugin.purs +++ b/src/PursLoader/Plugin.purs @@ -2,6 +2,7 @@ module PursLoader.Plugin ( Result() , Compile() , Context() + , Options() , ImmutableMap() , DependencyGraph() , get @@ -22,7 +23,9 @@ type Result = { srcMap :: ImmutableMap String String, ffiMap :: ImmutableMap Str type Compile eff = Nullable Error -> Result -> Eff eff Unit -type Context eff = { compile :: Compile eff -> Eff eff Unit } +type Context eff = { compile :: Compile eff -> Eff eff Unit, options :: Options } + +type Options = { bundle :: Boolean, output :: String, bundleOutput :: String } get :: forall key value. ImmutableMap key value -> key -> Maybe value get = runFn4 getFn Nothing Just -- 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 ++++------------------ src/PursLoader/LoaderRef.js | 2 -- src/PursLoader/Plugin.js | 6 ------ src/PursLoader/Plugin.purs | 22 ++-------------------- 4 files changed, 6 insertions(+), 46 deletions(-) (limited to 'src') 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 = diff --git a/src/PursLoader/LoaderRef.js b/src/PursLoader/LoaderRef.js index e92c72c..a5d8e1f 100644 --- a/src/PursLoader/LoaderRef.js +++ b/src/PursLoader/LoaderRef.js @@ -43,8 +43,6 @@ exports.asyncFn = asyncFn; exports.cacheable = cacheable; -exports.query = query; - exports.clearDependencies = clearDependencies; exports.resourcePath = resourcePath; diff --git a/src/PursLoader/Plugin.js b/src/PursLoader/Plugin.js index 90feb33..ded6df5 100644 --- a/src/PursLoader/Plugin.js +++ b/src/PursLoader/Plugin.js @@ -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); diff --git a/src/PursLoader/Plugin.purs b/src/PursLoader/Plugin.purs index 520c786..c798c83 100644 --- a/src/PursLoader/Plugin.purs +++ b/src/PursLoader/Plugin.purs @@ -1,11 +1,8 @@ module PursLoader.Plugin - ( Result() - , Compile() + ( Compile() , Context() , Options() - , ImmutableMap() , DependencyGraph() - , get , dependenciesOf ) where @@ -16,34 +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 -> Result -> Eff eff Unit +type Compile eff = Nullable Error -> DependencyGraph -> Eff eff Unit type Context eff = { compile :: Compile eff -> Eff eff Unit, options :: Options } type Options = { bundle :: Boolean, output :: String, bundleOutput :: String } -get :: forall key value. ImmutableMap key value -> key -> Maybe value -get = runFn4 getFn Nothing Just - 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)) -- cgit v1.2.3