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 --- README.md | 6 ------ docs/PursLoader/Plugin.md | 8 +++++++- package.json | 3 +-- 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 ++++- 10 files changed, 37 insertions(+), 81 deletions(-) delete mode 100644 src/PursLoader/LoaderUtil.js delete mode 100644 src/PursLoader/LoaderUtil.purs delete mode 100644 src/PursLoader/Options.purs diff --git a/README.md b/README.md index 0e1ce2f..e93e969 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,6 @@ This loader works in conjunction with the [PureScript webpack plugin](https://np npm install purs-loader --save-dev ``` -## Options - -###### `bundleOutput` (String) - -Relative path to the bundled JavaScript file generated by the `PurescriptWebpackPlugin`. The default value is `output/bundle.js`. - ## Example ```js diff --git a/docs/PursLoader/Plugin.md b/docs/PursLoader/Plugin.md index 9abec4d..26e3f26 100644 --- a/docs/PursLoader/Plugin.md +++ b/docs/PursLoader/Plugin.md @@ -15,7 +15,13 @@ type Compile eff = Nullable Error -> Result -> Eff eff Unit #### `Context` ``` purescript -type Context eff = { compile :: Compile eff -> Eff eff Unit } +type Context eff = { compile :: Compile eff -> Eff eff Unit, options :: Options } +``` + +#### `Options` + +``` purescript +type Options = { bundle :: Boolean, output :: String, bundleOutput :: String } ``` #### `get` diff --git a/package.json b/package.json index 1491de6..b0c119d 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,6 @@ "dependencies": { "async": "^1.3.0", "chalk": "^1.1.0", - "debug": "^2.2.0", - "loader-utils": "^0.2.6" + "debug": "^2.2.0" } } 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