From 2b620717603a6a2ba4d93ee1ca7334dc31500acf Mon Sep 17 00:00:00 2001 From: Nathan Faubion Date: Mon, 2 Nov 2015 14:04:58 -0600 Subject: Remove `require-path`, use relative paths for PS Fixes #15 Removes the `require-path` option and fixes it to '../'. When generating the temporary module for Webpack, use a relative path to the output directory so it doesn't need to be in `modulesDirectories`. --- README.md | 9 +------- docs/PursLoader/Loader.md | 2 +- docs/PursLoader/Options.md | 19 ++++++++++++++- src/PursLoader/Loader.js | 10 ++++++++ src/PursLoader/Loader.purs | 56 ++++++++++++++++++++++++++++----------------- src/PursLoader/Options.purs | 40 +++++++++++++++++--------------- 6 files changed, 86 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index a45573f..bef16af 100644 --- a/README.md +++ b/README.md @@ -44,10 +44,6 @@ Sets `--output=` the specifies the output directory, `output` by default Toggles `--no-prefix` that does not include the comment header. -###### `requirePath` (String) - -Sets `--require-path=` that specifies the path prefix to use for `require()` calls in the generated JavaScript. - ###### `ffi` (String Array) Specifies the PureScript FFI files setting `--ffi=`. Glob syntax is supported. This option is specified as `ffi[]=path`. @@ -73,10 +69,7 @@ var modulesDirectories = [ 'node_modules', // The bower component for purescript-prelude is specified here to // allow JavaScript files to require the 'Prelude' module globally. - 'bower_components/purescript-prelude/src', - // The output directory is specified here to allow PureScript files in - // your source to import other PureScript modules in your source. - output + 'bower_components/purescript-prelude/src' ]; var config diff --git a/docs/PursLoader/Loader.md b/docs/PursLoader/Loader.md index 8e91c4a..f81c486 100644 --- a/docs/PursLoader/Loader.md +++ b/docs/PursLoader/Loader.md @@ -3,7 +3,7 @@ #### `Effects` ``` purescript -type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader | eff) +type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader, err :: EXCEPTION | eff) ``` #### `loader` diff --git a/docs/PursLoader/Options.md b/docs/PursLoader/Options.md index d04b721..4202475 100644 --- a/docs/PursLoader/Options.md +++ b/docs/PursLoader/Options.md @@ -1,9 +1,26 @@ ## Module PursLoader.Options +#### `Options` + +``` purescript +newtype Options +``` + +##### Instances +``` purescript +instance isForeignOptions :: IsForeign Options +``` + +#### `output` + +``` purescript +output :: Options -> String +``` + #### `pscOptions` ``` purescript -pscOptions :: Foreign -> Array String +pscOptions :: Options -> Array String ``` #### `loaderSrcOption` diff --git a/src/PursLoader/Loader.js b/src/PursLoader/Loader.js index 98459b6..45e9c2f 100644 --- a/src/PursLoader/Loader.js +++ b/src/PursLoader/Loader.js @@ -12,8 +12,18 @@ function relative(from) { }; } +function joinPath(a) { + return function(b) { + return path.join(a, b); + }; +} + exports.cwd = cwd; exports.relative = relative; +exports.joinPath = joinPath; + exports.resolve = path.resolve; + +exports.dirname = path.dirname; diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs index b579577..205d3eb 100644 --- a/src/PursLoader/Loader.purs +++ b/src/PursLoader/Loader.purs @@ -4,28 +4,31 @@ module PursLoader.Loader , loaderFn ) where -import Prelude (Unit(), ($), (<>), (>>=), (<$>), (++), bind, flip, id, pure, return, unit) +import Prelude (Unit(), ($), (<>), (>>=), (<$>), (++), bind, flip, id, pure, return, unit, show) import Control.Monad.Aff (Aff(), runAff) import Control.Monad.Eff (Eff()) import Control.Monad.Eff.Class (liftEff) -import Control.Monad.Eff.Exception (error) +import Control.Monad.Eff.Exception (throwException, error, EXCEPTION()) import Data.Array ((!!), concat) import Data.Function (Fn2(), mkFn2) import Data.Maybe (Maybe(..), fromMaybe, maybe) +import Data.Either (Either(..)) import Data.String (joinWith) import Data.String.Regex (match, noFlags, regex, test) import Data.Traversable (sequence) +import Data.Foreign (F()) +import Data.Foreign.Class (read) import PursLoader.ChildProcess (ChildProcess(), spawn) import PursLoader.FS (FS(), writeFileUtf8, findFileUtf8) import PursLoader.Glob (Glob(), globAll) import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query, clearDependencies, addDependency, resourcePath) import PursLoader.LoaderUtil (parseQuery) -import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions) +import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions, Options(), output) -type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader | eff) +type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader, err :: EXCEPTION | eff) moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } @@ -45,6 +48,10 @@ foreign import relative :: String -> String -> String foreign import resolve :: String -> String +foreign import dirname :: String -> String + +foreign import joinPath :: String -> String -> String + mkPsci :: Array (Array String) -> Array (Array String) -> String mkPsci srcs ffis = joinWith "\n" ((loadModule <$> concat srcs) <> (loadForeign <$> concat ffis)) where @@ -66,32 +73,39 @@ loader' ref source = do let parsed = parseQuery $ query ref srcs = fromMaybe [] (loaderSrcOption parsed) ffis = fromMaybe [] (loaderFFIOption parsed) - opts = pscOptions parsed - srcss <- globAll srcs - ffiss <- globAll ffis + case read parsed :: F Options of + Left e -> liftEff (throwException (error (show e))) + Right opts -> do + let pscOpts = pscOptions opts + + srcss <- globAll srcs + ffiss <- globAll ffis - let psciFile = mkPsci srcss ffiss + let psciFile = mkPsci srcss ffiss - writeFileUtf8 psciFilename psciFile + writeFileUtf8 psciFilename psciFile - let moduleName = match moduleRegex source >>= (!!!) 1 >>= id - hasForeign = test foreignRegex source - result = (\a -> "module.exports = require('" ++ a ++ "');") <$> moduleName + let moduleName = match moduleRegex source >>= (!!!) 1 >>= id + hasForeign = test foreignRegex source + outputDir = resolve (output opts) + resourceDir = dirname (resourcePath ref) + result = (\a -> "module.exports = require('" ++ relative resourceDir (joinPath outputDir a) ++ "');") <$> moduleName - liftEff (clearDependencies ref) - liftEff (addDependency ref (resourcePath ref)) - liftEff (sequence $ (\src -> addDependency ref (resolve src)) <$> concat srcss) + liftEff do + clearDependencies ref + addDependency ref (resourcePath ref) + sequence $ (\src -> addDependency ref (resolve src)) <$> concat srcss - foreignPath <- if hasForeign - then fromMaybe (pure Nothing) (findFFI ffiss <$> moduleName) - else pure Nothing + foreignPath <- if hasForeign + then fromMaybe (pure Nothing) (findFFI ffiss <$> moduleName) + else pure Nothing - fromMaybe (pure unit) ((\path -> liftEff (addDependency ref path)) <$> foreignPath) + fromMaybe (pure unit) ((\path -> liftEff (addDependency ref path)) <$> foreignPath) - spawn pscCommand (srcs <> opts) + spawn pscCommand (srcs <> pscOpts) - return result + return result loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit loader ref source = do diff --git a/src/PursLoader/Options.purs b/src/PursLoader/Options.purs index e3957eb..1650652 100644 --- a/src/PursLoader/Options.purs +++ b/src/PursLoader/Options.purs @@ -2,9 +2,11 @@ module PursLoader.Options ( pscOptions , loaderSrcOption , loaderFFIOption + , Options() + , output ) where -import Prelude (Unit(), (<>), (<$>), (<<<), (++), (<*>), const) +import Prelude (Unit(), (<>), (<$>), (<<<), (++), (<*>), ($), const, id) import Data.Array (concat) import Data.Either (either) @@ -44,13 +46,16 @@ newtype Options , noTco :: NullOrUndefined Boolean , verboseErrors :: NullOrUndefined Boolean , comments :: NullOrUndefined Boolean - , output :: NullOrUndefined String + , output :: String , noPrefix :: NullOrUndefined Boolean - , requirePath :: NullOrUndefined String + , requirePath :: String , src :: NullOrUndefined (Array String) , ffi :: NullOrUndefined (Array String) } +output :: Options -> String +output (Options o) = o.output + instance isForeignOptions :: IsForeign Options where read obj = Options <$> ({ noPrelude: _ , noOpts: _ @@ -60,7 +65,7 @@ instance isForeignOptions :: IsForeign Options where , comments: _ , output: _ , noPrefix: _ - , requirePath: _ + , requirePath: "../" , src: _ , ffi: _ } <$> readProp noPreludeOpt obj @@ -69,9 +74,8 @@ instance isForeignOptions :: IsForeign Options where <*> readProp noTcoOpt obj <*> readProp verboseErrorsOpt obj <*> readProp commentsOpt obj - <*> readProp outputOpt obj + <*> (maybe "output" id <<< runNullOrUndefined <$> readProp outputOpt obj) <*> readProp noPrefixOpt obj - <*> readProp requirePathOpt obj <*> readProp srcOpt obj <*> readProp ffiOpt obj) @@ -88,19 +92,17 @@ instance arrayLoaderOption :: (LoaderOption a) => LoaderOption (Array a) where opt key val = concat (opt key <$> (NullOrUndefined <<< Just) <$> (fromMaybe [] (runNullOrUndefined val))) -pscOptions :: Foreign -> Array String -pscOptions query = either (const []) fold parsed - where parsed = read query :: F Options - fold (Options a) = opt noPreludeOpt a.noPrelude <> - opt noOptsOpt a.noOpts <> - opt noMagicDoOpt a.noMagicDo <> - opt noTcoOpt a.noTco <> - opt verboseErrorsOpt a.verboseErrors <> - opt commentsOpt a.comments <> - opt outputOpt a.output <> - opt noPrefixOpt a.noPrefix <> - opt requirePathOpt a.requirePath <> - opt ffiOpt a.ffi +pscOptions :: Options -> Array String +pscOptions (Options a) = opt noPreludeOpt a.noPrelude <> + opt noOptsOpt a.noOpts <> + opt noMagicDoOpt a.noMagicDo <> + opt noTcoOpt a.noTco <> + opt verboseErrorsOpt a.verboseErrors <> + opt commentsOpt a.comments <> + opt outputOpt (NullOrUndefined $ Just a.output) <> + opt noPrefixOpt a.noPrefix <> + opt requirePathOpt (NullOrUndefined $ Just a.requirePath) <> + opt ffiOpt a.ffi loaderSrcOption :: Foreign -> Maybe (Array String) loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query) -- cgit v1.2.3