From 7de41f10b4ff0f0d6b45d59bee0f166c3cfe3f9f Mon Sep 17 00:00:00 2001 From: Alex Mingoia Date: Tue, 10 May 2016 00:09:28 -0700 Subject: Refactor to compile independently of purescript-webpack-plugin. - Remove dependence on purescript-webpack-plugin - Fixes double-compilation issue by loading compiled JS instead of adding dependency. - Uses `psc-ide-server` for fast rebuilds. --- src/PursLoader/Debug.js | 12 ----- src/PursLoader/Debug.purs | 9 ---- src/PursLoader/JsStringEscape.js | 7 --- src/PursLoader/JsStringEscape.purs | 3 -- src/PursLoader/Loader.purs | 108 ------------------------------------- src/PursLoader/LoaderRef.js | 50 ----------------- src/PursLoader/LoaderRef.purs | 40 -------------- src/PursLoader/Path.js | 24 --------- src/PursLoader/Path.purs | 14 ----- src/PursLoader/Plugin.js | 14 ----- src/PursLoader/Plugin.purs | 34 ------------ 11 files changed, 315 deletions(-) delete mode 100644 src/PursLoader/Debug.js delete mode 100644 src/PursLoader/Debug.purs delete mode 100644 src/PursLoader/JsStringEscape.js delete mode 100644 src/PursLoader/JsStringEscape.purs delete mode 100644 src/PursLoader/Loader.purs delete mode 100644 src/PursLoader/LoaderRef.js delete mode 100644 src/PursLoader/LoaderRef.purs delete mode 100644 src/PursLoader/Path.js delete mode 100644 src/PursLoader/Path.purs delete mode 100644 src/PursLoader/Plugin.js delete mode 100644 src/PursLoader/Plugin.purs (limited to 'src/PursLoader') diff --git a/src/PursLoader/Debug.js b/src/PursLoader/Debug.js deleted file mode 100644 index 85eca10..0000000 --- a/src/PursLoader/Debug.js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict'; - -// module PursLoader.Debug - -var debug_ = require('debug')('purs-loader'); - -function debug(message) { - return function(){ - debug_(message); - }; -} -exports.debug = debug; diff --git a/src/PursLoader/Debug.purs b/src/PursLoader/Debug.purs deleted file mode 100644 index 7a02f69..0000000 --- a/src/PursLoader/Debug.purs +++ /dev/null @@ -1,9 +0,0 @@ -module PursLoader.Debug (debug) where - -import Prelude (Unit()) - -import Control.Monad.Eff (Eff()) - -import PursLoader.LoaderRef (Loader()) - -foreign import debug :: forall eff. String -> Eff (loader :: Loader | eff) Unit diff --git a/src/PursLoader/JsStringEscape.js b/src/PursLoader/JsStringEscape.js deleted file mode 100644 index ff0a1a6..0000000 --- a/src/PursLoader/JsStringEscape.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict'; - -// module PursLoader.JsStringEscape - -var jsStringEscape = require('js-string-escape'); - -exports.jsStringEscape = jsStringEscape; diff --git a/src/PursLoader/JsStringEscape.purs b/src/PursLoader/JsStringEscape.purs deleted file mode 100644 index 79590ae..0000000 --- a/src/PursLoader/JsStringEscape.purs +++ /dev/null @@ -1,3 +0,0 @@ -module PursLoader.JsStringEscape (jsStringEscape) where - -foreign import jsStringEscape :: String -> String diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs deleted file mode 100644 index acb0993..0000000 --- a/src/PursLoader/Loader.purs +++ /dev/null @@ -1,108 +0,0 @@ -module PursLoader.Loader - ( Effects() - , Effects_() - , loader - , loaderFn - ) where - -import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), (<<<), bind, const, id, pure, unit) - -import Control.Bind (join) -import Control.Monad.Eff (Eff(), foreachE) -import Control.Monad.Eff.Console (CONSOLE()) -import Control.Monad.Eff.Exception (EXCEPTION(), Error(), error) - -import Data.Array ((!!)) -import Data.Either (Either(..), either) -import Data.Function (Fn2(), mkFn2) -import Data.Maybe (maybe) -import Data.Nullable (toMaybe) -import Data.String.Regex (Regex(), match, noFlags, regex) - -import Unsafe.Coerce (unsafeCoerce) - -import PursLoader.Debug (debug) -import PursLoader.JsStringEscape (jsStringEscape) -import PursLoader.LoaderRef - ( AsyncCallback() - , LoaderRef() - , Loader() - , async - , cacheable - , addDependency - , resourcePath - ) -import PursLoader.Path (dirname, joinPath, relative) -import PursLoader.Plugin as Plugin - -type Effects eff = (console :: CONSOLE, err :: EXCEPTION | eff) - -type Effects_ eff = Effects (loader :: Loader | eff) - -loader :: forall eff. LoaderRef -> String -> Eff (Effects_ eff) Unit -loader ref source = do - callback <- async ref - - cacheable ref - - debug "Invoke PureScript plugin compilation" - - pluginContext.compile (compile callback) - where - pluginContext :: Plugin.Context (Effects_ eff) - pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext - - compile :: AsyncCallback (Effects eff) -> Plugin.Compile (Effects_ eff) - compile callback error' graph = do - either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name - - addDependency ref (resourcePath ref) - - either (const $ callback (pure fixedError) "") id - (handle <$> name <*> dependencies <*> exports) - where - fixedError :: Error - fixedError = error "PureScript compilation has failed." - - handle :: String -> Array String -> String -> Eff (Effects_ eff) Unit - handle name' deps res = do - debug ("Adding PureScript dependencies for " ++ name') - foreachE deps (addDependency ref) - debug "Generated loader result" - debug res - callback (const fixedError <$> toMaybe error') res - - exports :: Either Error String - exports = - if pluginContext.options.bundle - then bundleExport <$> name - else moduleExport <<< modulePath <$> name - where - bundleExport :: String -> String - bundleExport name' = "module.exports = require('" ++ jsStringEscape path ++ "')['" ++ name' ++ "'];" - where - path :: String - path = relative resourceDir pluginContext.options.bundleOutput - - moduleExport :: String -> String - moduleExport path = "module.exports = require('" ++ jsStringEscape path ++ "');" - - 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 = - maybe (Left $ error "Failed to parse module name") Right - (join $ match re source >>= \as -> as !! 1) - where - re :: Regex - re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } - -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 deleted file mode 100644 index a5d8e1f..0000000 --- a/src/PursLoader/LoaderRef.js +++ /dev/null @@ -1,50 +0,0 @@ -'use strict'; - -// module PursLoader.LoaderRef - -function asyncFn(isJust, fromMaybe, ref){ - return function(){ - var callback = ref.async(); - return function(error){ - return function(value){ - return function(){ - return isJust(error) ? callback(fromMaybe(new Error())(error)) - : callback(null, value); - }; - }; - }; - }; -} -function cacheable(ref){ - return function(){ - return ref.cacheable && ref.cacheable(); - }; -} - -function clearDependencies(ref){ - return function(){ - return ref.clearDependencies(); - }; -} - -function resourcePath(ref){ - return ref.resourcePath; -} - -function addDependency(ref){ - return function(dep){ - return function(){ - return ref.addDependency(dep); - }; - }; -} - -exports.asyncFn = asyncFn; - -exports.cacheable = cacheable; - -exports.clearDependencies = clearDependencies; - -exports.resourcePath = resourcePath; - -exports.addDependency = addDependency; diff --git a/src/PursLoader/LoaderRef.purs b/src/PursLoader/LoaderRef.purs deleted file mode 100644 index 140d94a..0000000 --- a/src/PursLoader/LoaderRef.purs +++ /dev/null @@ -1,40 +0,0 @@ -module PursLoader.LoaderRef - ( LoaderRef() - , Loader() - , AsyncCallback() - , async - , cacheable - , clearDependencies - , addDependency - , resourcePath - ) where - -import Prelude (Unit()) - -import Control.Monad.Eff (Eff()) -import Control.Monad.Eff.Exception (Error()) - -import Data.Function (Fn3(), runFn3) -import Data.Maybe (Maybe(), fromMaybe, isJust) - -type AsyncCallback eff = Maybe Error -> String -> Eff (loader :: Loader | eff) Unit - -data LoaderRef - -foreign import data Loader :: ! - -foreign import asyncFn :: forall eff. Fn3 (Maybe Error -> Boolean) - (Error -> Maybe Error -> Error) - LoaderRef - (Eff (loader :: Loader | eff) (AsyncCallback eff)) - -async :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) (Maybe Error -> String -> Eff (loader :: Loader | eff) Unit) -async ref = runFn3 asyncFn isJust fromMaybe ref - -foreign import cacheable :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit - -foreign import clearDependencies :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit - -foreign import resourcePath :: LoaderRef -> String - -foreign import addDependency :: forall eff. LoaderRef -> String -> Eff (loader :: Loader | eff) Unit diff --git a/src/PursLoader/Path.js b/src/PursLoader/Path.js deleted file mode 100644 index 878f256..0000000 --- a/src/PursLoader/Path.js +++ /dev/null @@ -1,24 +0,0 @@ -'use strict' - -// module PursLoader.Path - -var path = require('path'); - -function relative(from) { - return function(to){ - return path.relative(from, to); - }; -} -exports.relative = relative; - - -function joinPath(a) { - return function(b) { - return path.join(a, b); - }; -} -exports.joinPath = joinPath; - -exports.resolve = path.resolve; - -exports.dirname = path.dirname; diff --git a/src/PursLoader/Path.purs b/src/PursLoader/Path.purs deleted file mode 100644 index 98cad5a..0000000 --- a/src/PursLoader/Path.purs +++ /dev/null @@ -1,14 +0,0 @@ -module PursLoader.Path - ( relative - , resolve - , dirname - , joinPath - ) where - -foreign import relative :: String -> String -> String - -foreign import resolve :: String -> String - -foreign import dirname :: String -> String - -foreign import joinPath :: String -> String -> String diff --git a/src/PursLoader/Plugin.js b/src/PursLoader/Plugin.js deleted file mode 100644 index ded6df5..0000000 --- a/src/PursLoader/Plugin.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -// module PursLoader.Plugin - -function dependenciesOfFn(left, right, graph, node) { - try { - var dependencies = graph.dependenciesOf(node); - return right(dependencies); - } - catch (error) { - return left(error); - } -} -exports.dependenciesOfFn = dependenciesOfFn; diff --git a/src/PursLoader/Plugin.purs b/src/PursLoader/Plugin.purs deleted file mode 100644 index c798c83..0000000 --- a/src/PursLoader/Plugin.purs +++ /dev/null @@ -1,34 +0,0 @@ -module PursLoader.Plugin - ( Compile() - , Context() - , Options() - , DependencyGraph() - , dependenciesOf - ) where - -import Prelude (Unit()) - -import Control.Monad.Eff (Eff()) -import Control.Monad.Eff.Exception (Error()) - -import Data.Either (Either(..)) -import Data.Function (Fn4(), runFn4) -import Data.Nullable (Nullable()) - -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 } - -dependenciesOf :: DependencyGraph -> String -> Either Error (Array String) -dependenciesOf = runFn4 dependenciesOfFn Left Right - -foreign import data DependencyGraph :: * - -foreign import dependenciesOfFn - :: Fn4 (Error -> Either Error (Array String)) - (Array String -> Either Error (Array String)) - DependencyGraph - String - (Either Error (Array String)) -- cgit v1.2.3