]> git.immae.eu Git - github/fretlink/purs-loader.git/commitdiff
Remove `require-path`, use relative paths for PS
authorNathan Faubion <nathan@n-son.com>
Mon, 2 Nov 2015 20:04:58 +0000 (14:04 -0600)
committerNathan Faubion <nathan@n-son.com>
Mon, 2 Nov 2015 20:16:21 +0000 (14:16 -0600)
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
docs/PursLoader/Loader.md
docs/PursLoader/Options.md
src/PursLoader/Loader.js
src/PursLoader/Loader.purs
src/PursLoader/Options.purs

index a45573f346dfb71d00a927b4f50cd60fcc3b1d6a..bef16af0be7f7f9b7ff0dab2d82737bf77055f48 100644 (file)
--- a/README.md
+++ b/README.md
@@ -44,10 +44,6 @@ Sets `--output=<string>` the specifies the output directory, `output` by default
 
 Toggles `--no-prefix` that does not include the comment header.
 
-###### `requirePath` (String)
-
-Sets `--require-path=<string>` that specifies the path prefix to use for `require()` calls in the generated JavaScript.
-
 ###### `ffi` (String Array)
 
 Specifies the PureScript FFI files setting `--ffi=<string>`. 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
index 8e91c4ae8d99b5406ceba274e3e4a986c50aab78..f81c4861d094593efb65faedfb5eba68f931af6c 100644 (file)
@@ -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`
index d04b721a1c67a71a87ec9211823d3ed6cc9142d5..4202475ddb1c483cc8c5de6e517b879c7d7ad7de 100644 (file)
@@ -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`
index 98459b69e5247a3c70f845d04903e329fc6e280e..45e9c2fbb24a29bb8fc2fdb973c31d294df3362d 100644 (file)
@@ -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;
index b5795771134f0d2722c0607ea70c986a1c008fe6..205d3eb49cfaff556060b266ca48a98d328211b9 100644 (file)
@@ -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
index e3957ebc9c69a4c838c69dfcbb18535ebe82fafc..165065249fb028a8f52a6b538002b7ce739a8699 100644 (file)
@@ -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)