diff options
author | eric <thul.eric@gmail.com> | 2015-11-07 15:06:39 -0500 |
---|---|---|
committer | eric <thul.eric@gmail.com> | 2015-11-07 15:06:39 -0500 |
commit | d18adcf960b5df464971e7c06366feeb0d173f1b (patch) | |
tree | 6821a4e1fb1f5ab0c54f4e8434e1c5c717890967 | |
parent | 57585908acc300daa55b40156eeff7a065fe2832 (diff) | |
parent | 04eb392f345262e340717812e51a3ba810f7eaee (diff) | |
download | purs-loader-d18adcf960b5df464971e7c06366feeb0d173f1b.tar.gz purs-loader-d18adcf960b5df464971e7c06366feeb0d173f1b.tar.zst purs-loader-d18adcf960b5df464971e7c06366feeb0d173f1b.zip |
Merge pull request #30 from natefaubion/fix-paths
Fix paths
-rw-r--r-- | README.md | 9 | ||||
-rw-r--r-- | bower.json | 6 | ||||
-rw-r--r-- | docs/PursLoader/Loader.md | 2 | ||||
-rw-r--r-- | docs/PursLoader/Options.md | 19 | ||||
-rw-r--r-- | example/webpack.config.js | 3 | ||||
-rw-r--r-- | src/PursLoader/Loader.js | 10 | ||||
-rw-r--r-- | src/PursLoader/Loader.purs | 56 | ||||
-rw-r--r-- | src/PursLoader/Options.purs | 40 |
8 files changed, 90 insertions, 55 deletions
@@ -44,10 +44,6 @@ Sets `--output=<string>` the specifies the output directory, `output` by default | |||
44 | 44 | ||
45 | Toggles `--no-prefix` that does not include the comment header. | 45 | Toggles `--no-prefix` that does not include the comment header. |
46 | 46 | ||
47 | ###### `requirePath` (String) | ||
48 | |||
49 | Sets `--require-path=<string>` that specifies the path prefix to use for `require()` calls in the generated JavaScript. | ||
50 | |||
51 | ###### `ffi` (String Array) | 47 | ###### `ffi` (String Array) |
52 | 48 | ||
53 | Specifies the PureScript FFI files setting `--ffi=<string>`. Glob syntax is supported. This option is specified as `ffi[]=path`. | 49 | 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 = [ | |||
73 | 'node_modules', | 69 | 'node_modules', |
74 | // The bower component for purescript-prelude is specified here to | 70 | // The bower component for purescript-prelude is specified here to |
75 | // allow JavaScript files to require the 'Prelude' module globally. | 71 | // allow JavaScript files to require the 'Prelude' module globally. |
76 | 'bower_components/purescript-prelude/src', | 72 | 'bower_components/purescript-prelude/src' |
77 | // The output directory is specified here to allow PureScript files in | ||
78 | // your source to import other PureScript modules in your source. | ||
79 | output | ||
80 | ]; | 73 | ]; |
81 | 74 | ||
82 | var config | 75 | var config |
@@ -2,8 +2,8 @@ | |||
2 | "name": "purs-loader", | 2 | "name": "purs-loader", |
3 | "private": true, | 3 | "private": true, |
4 | "devDependencies": { | 4 | "devDependencies": { |
5 | "purescript-aff": "~0.11.3", | 5 | "purescript-aff": "^0.13.0", |
6 | "purescript-strings": "~0.6.0", | 6 | "purescript-strings": "^0.7.0", |
7 | "purescript-foreign": "~0.6.0" | 7 | "purescript-foreign": "^0.7.0" |
8 | } | 8 | } |
9 | } | 9 | } |
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 @@ | |||
3 | #### `Effects` | 3 | #### `Effects` |
4 | 4 | ||
5 | ``` purescript | 5 | ``` purescript |
6 | type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader | eff) | 6 | type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader, err :: EXCEPTION | eff) |
7 | ``` | 7 | ``` |
8 | 8 | ||
9 | #### `loader` | 9 | #### `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 @@ | |||
1 | ## Module PursLoader.Options | 1 | ## Module PursLoader.Options |
2 | 2 | ||
3 | #### `Options` | ||
4 | |||
5 | ``` purescript | ||
6 | newtype Options | ||
7 | ``` | ||
8 | |||
9 | ##### Instances | ||
10 | ``` purescript | ||
11 | instance isForeignOptions :: IsForeign Options | ||
12 | ``` | ||
13 | |||
14 | #### `output` | ||
15 | |||
16 | ``` purescript | ||
17 | output :: Options -> String | ||
18 | ``` | ||
19 | |||
3 | #### `pscOptions` | 20 | #### `pscOptions` |
4 | 21 | ||
5 | ``` purescript | 22 | ``` purescript |
6 | pscOptions :: Foreign -> Array String | 23 | pscOptions :: Options -> Array String |
7 | ``` | 24 | ``` |
8 | 25 | ||
9 | #### `loaderSrcOption` | 26 | #### `loaderSrcOption` |
diff --git a/example/webpack.config.js b/example/webpack.config.js index 1a0eef6..f1456d5 100644 --- a/example/webpack.config.js +++ b/example/webpack.config.js | |||
@@ -8,8 +8,7 @@ var output = 'output'; | |||
8 | 8 | ||
9 | var modulesDirectories = [ | 9 | var modulesDirectories = [ |
10 | 'node_modules', | 10 | 'node_modules', |
11 | 'bower_components/purescript-prelude/src', | 11 | 'bower_components/purescript-prelude/src' |
12 | output | ||
13 | ]; | 12 | ]; |
14 | 13 | ||
15 | var config | 14 | var config |
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) { | |||
12 | }; | 12 | }; |
13 | } | 13 | } |
14 | 14 | ||
15 | function joinPath(a) { | ||
16 | return function(b) { | ||
17 | return path.join(a, b); | ||
18 | }; | ||
19 | } | ||
20 | |||
15 | exports.cwd = cwd; | 21 | exports.cwd = cwd; |
16 | 22 | ||
17 | exports.relative = relative; | 23 | exports.relative = relative; |
18 | 24 | ||
25 | exports.joinPath = joinPath; | ||
26 | |||
19 | exports.resolve = path.resolve; | 27 | exports.resolve = path.resolve; |
28 | |||
29 | 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 | |||
4 | , loaderFn | 4 | , loaderFn |
5 | ) where | 5 | ) where |
6 | 6 | ||
7 | import Prelude (Unit(), ($), (<>), (>>=), (<$>), (++), bind, flip, id, pure, return, unit) | 7 | import Prelude (Unit(), ($), (<>), (>>=), (<$>), (++), bind, flip, id, pure, return, unit, show) |
8 | 8 | ||
9 | import Control.Monad.Aff (Aff(), runAff) | 9 | import Control.Monad.Aff (Aff(), runAff) |
10 | import Control.Monad.Eff (Eff()) | 10 | import Control.Monad.Eff (Eff()) |
11 | import Control.Monad.Eff.Class (liftEff) | 11 | import Control.Monad.Eff.Class (liftEff) |
12 | import Control.Monad.Eff.Exception (error) | 12 | import Control.Monad.Eff.Exception (throwException, error, EXCEPTION()) |
13 | 13 | ||
14 | import Data.Array ((!!), concat) | 14 | import Data.Array ((!!), concat) |
15 | import Data.Function (Fn2(), mkFn2) | 15 | import Data.Function (Fn2(), mkFn2) |
16 | import Data.Maybe (Maybe(..), fromMaybe, maybe) | 16 | import Data.Maybe (Maybe(..), fromMaybe, maybe) |
17 | import Data.Either (Either(..)) | ||
17 | import Data.String (joinWith) | 18 | import Data.String (joinWith) |
18 | import Data.String.Regex (match, noFlags, regex, test) | 19 | import Data.String.Regex (match, noFlags, regex, test) |
19 | import Data.Traversable (sequence) | 20 | import Data.Traversable (sequence) |
21 | import Data.Foreign (F()) | ||
22 | import Data.Foreign.Class (read) | ||
20 | 23 | ||
21 | import PursLoader.ChildProcess (ChildProcess(), spawn) | 24 | import PursLoader.ChildProcess (ChildProcess(), spawn) |
22 | import PursLoader.FS (FS(), writeFileUtf8, findFileUtf8) | 25 | import PursLoader.FS (FS(), writeFileUtf8, findFileUtf8) |
23 | import PursLoader.Glob (Glob(), globAll) | 26 | import PursLoader.Glob (Glob(), globAll) |
24 | import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query, clearDependencies, addDependency, resourcePath) | 27 | import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query, clearDependencies, addDependency, resourcePath) |
25 | import PursLoader.LoaderUtil (parseQuery) | 28 | import PursLoader.LoaderUtil (parseQuery) |
26 | import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions) | 29 | import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions, Options(), output) |
27 | 30 | ||
28 | type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader | eff) | 31 | type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader, err :: EXCEPTION | eff) |
29 | 32 | ||
30 | moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } | 33 | moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } |
31 | 34 | ||
@@ -45,6 +48,10 @@ foreign import relative :: String -> String -> String | |||
45 | 48 | ||
46 | foreign import resolve :: String -> String | 49 | foreign import resolve :: String -> String |
47 | 50 | ||
51 | foreign import dirname :: String -> String | ||
52 | |||
53 | foreign import joinPath :: String -> String -> String | ||
54 | |||
48 | mkPsci :: Array (Array String) -> Array (Array String) -> String | 55 | mkPsci :: Array (Array String) -> Array (Array String) -> String |
49 | mkPsci srcs ffis = joinWith "\n" ((loadModule <$> concat srcs) <> (loadForeign <$> concat ffis)) | 56 | mkPsci srcs ffis = joinWith "\n" ((loadModule <$> concat srcs) <> (loadForeign <$> concat ffis)) |
50 | where | 57 | where |
@@ -66,32 +73,39 @@ loader' ref source = do | |||
66 | let parsed = parseQuery $ query ref | 73 | let parsed = parseQuery $ query ref |
67 | srcs = fromMaybe [] (loaderSrcOption parsed) | 74 | srcs = fromMaybe [] (loaderSrcOption parsed) |
68 | ffis = fromMaybe [] (loaderFFIOption parsed) | 75 | ffis = fromMaybe [] (loaderFFIOption parsed) |
69 | opts = pscOptions parsed | ||
70 | 76 | ||
71 | srcss <- globAll srcs | 77 | case read parsed :: F Options of |
72 | ffiss <- globAll ffis | 78 | Left e -> liftEff (throwException (error (show e))) |
79 | Right opts -> do | ||
80 | let pscOpts = pscOptions opts | ||
81 | |||
82 | srcss <- globAll srcs | ||
83 | ffiss <- globAll ffis | ||
73 | 84 | ||
74 | let psciFile = mkPsci srcss ffiss | 85 | let psciFile = mkPsci srcss ffiss |
75 | 86 | ||
76 | writeFileUtf8 psciFilename psciFile | 87 | writeFileUtf8 psciFilename psciFile |
77 | 88 | ||
78 | let moduleName = match moduleRegex source >>= (!!!) 1 >>= id | 89 | let moduleName = match moduleRegex source >>= (!!!) 1 >>= id |
79 | hasForeign = test foreignRegex source | 90 | hasForeign = test foreignRegex source |
80 | result = (\a -> "module.exports = require('" ++ a ++ "');") <$> moduleName | 91 | outputDir = resolve (output opts) |
92 | resourceDir = dirname (resourcePath ref) | ||
93 | result = (\a -> "module.exports = require('" ++ relative resourceDir (joinPath outputDir a) ++ "');") <$> moduleName | ||
81 | 94 | ||
82 | liftEff (clearDependencies ref) | 95 | liftEff do |
83 | liftEff (addDependency ref (resourcePath ref)) | 96 | clearDependencies ref |
84 | liftEff (sequence $ (\src -> addDependency ref (resolve src)) <$> concat srcss) | 97 | addDependency ref (resourcePath ref) |
98 | sequence $ (\src -> addDependency ref (resolve src)) <$> concat srcss | ||
85 | 99 | ||
86 | foreignPath <- if hasForeign | 100 | foreignPath <- if hasForeign |
87 | then fromMaybe (pure Nothing) (findFFI ffiss <$> moduleName) | 101 | then fromMaybe (pure Nothing) (findFFI ffiss <$> moduleName) |
88 | else pure Nothing | 102 | else pure Nothing |
89 | 103 | ||
90 | fromMaybe (pure unit) ((\path -> liftEff (addDependency ref path)) <$> foreignPath) | 104 | fromMaybe (pure unit) ((\path -> liftEff (addDependency ref path)) <$> foreignPath) |
91 | 105 | ||
92 | spawn pscCommand (srcs <> opts) | 106 | spawn pscCommand (srcs <> pscOpts) |
93 | 107 | ||
94 | return result | 108 | return result |
95 | 109 | ||
96 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit | 110 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit |
97 | loader ref source = do | 111 | 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 | |||
2 | ( pscOptions | 2 | ( pscOptions |
3 | , loaderSrcOption | 3 | , loaderSrcOption |
4 | , loaderFFIOption | 4 | , loaderFFIOption |
5 | , Options() | ||
6 | , output | ||
5 | ) where | 7 | ) where |
6 | 8 | ||
7 | import Prelude (Unit(), (<>), (<$>), (<<<), (++), (<*>), const) | 9 | import Prelude (Unit(), (<>), (<$>), (<<<), (++), (<*>), ($), const, id) |
8 | 10 | ||
9 | import Data.Array (concat) | 11 | import Data.Array (concat) |
10 | import Data.Either (either) | 12 | import Data.Either (either) |
@@ -44,13 +46,16 @@ newtype Options | |||
44 | , noTco :: NullOrUndefined Boolean | 46 | , noTco :: NullOrUndefined Boolean |
45 | , verboseErrors :: NullOrUndefined Boolean | 47 | , verboseErrors :: NullOrUndefined Boolean |
46 | , comments :: NullOrUndefined Boolean | 48 | , comments :: NullOrUndefined Boolean |
47 | , output :: NullOrUndefined String | 49 | , output :: String |
48 | , noPrefix :: NullOrUndefined Boolean | 50 | , noPrefix :: NullOrUndefined Boolean |
49 | , requirePath :: NullOrUndefined String | 51 | , requirePath :: String |
50 | , src :: NullOrUndefined (Array String) | 52 | , src :: NullOrUndefined (Array String) |
51 | , ffi :: NullOrUndefined (Array String) | 53 | , ffi :: NullOrUndefined (Array String) |
52 | } | 54 | } |
53 | 55 | ||
56 | output :: Options -> String | ||
57 | output (Options o) = o.output | ||
58 | |||
54 | instance isForeignOptions :: IsForeign Options where | 59 | instance isForeignOptions :: IsForeign Options where |
55 | read obj = Options <$> ({ noPrelude: _ | 60 | read obj = Options <$> ({ noPrelude: _ |
56 | , noOpts: _ | 61 | , noOpts: _ |
@@ -60,7 +65,7 @@ instance isForeignOptions :: IsForeign Options where | |||
60 | , comments: _ | 65 | , comments: _ |
61 | , output: _ | 66 | , output: _ |
62 | , noPrefix: _ | 67 | , noPrefix: _ |
63 | , requirePath: _ | 68 | , requirePath: "../" |
64 | , src: _ | 69 | , src: _ |
65 | , ffi: _ | 70 | , ffi: _ |
66 | } <$> readProp noPreludeOpt obj | 71 | } <$> readProp noPreludeOpt obj |
@@ -69,9 +74,8 @@ instance isForeignOptions :: IsForeign Options where | |||
69 | <*> readProp noTcoOpt obj | 74 | <*> readProp noTcoOpt obj |
70 | <*> readProp verboseErrorsOpt obj | 75 | <*> readProp verboseErrorsOpt obj |
71 | <*> readProp commentsOpt obj | 76 | <*> readProp commentsOpt obj |
72 | <*> readProp outputOpt obj | 77 | <*> (maybe "output" id <<< runNullOrUndefined <$> readProp outputOpt obj) |
73 | <*> readProp noPrefixOpt obj | 78 | <*> readProp noPrefixOpt obj |
74 | <*> readProp requirePathOpt obj | ||
75 | <*> readProp srcOpt obj | 79 | <*> readProp srcOpt obj |
76 | <*> readProp ffiOpt obj) | 80 | <*> readProp ffiOpt obj) |
77 | 81 | ||
@@ -88,19 +92,17 @@ instance arrayLoaderOption :: (LoaderOption a) => LoaderOption (Array a) where | |||
88 | opt key val = concat (opt key <$> (NullOrUndefined <<< Just) | 92 | opt key val = concat (opt key <$> (NullOrUndefined <<< Just) |
89 | <$> (fromMaybe [] (runNullOrUndefined val))) | 93 | <$> (fromMaybe [] (runNullOrUndefined val))) |
90 | 94 | ||
91 | pscOptions :: Foreign -> Array String | 95 | pscOptions :: Options -> Array String |
92 | pscOptions query = either (const []) fold parsed | 96 | pscOptions (Options a) = opt noPreludeOpt a.noPrelude <> |
93 | where parsed = read query :: F Options | 97 | opt noOptsOpt a.noOpts <> |
94 | fold (Options a) = opt noPreludeOpt a.noPrelude <> | 98 | opt noMagicDoOpt a.noMagicDo <> |
95 | opt noOptsOpt a.noOpts <> | 99 | opt noTcoOpt a.noTco <> |
96 | opt noMagicDoOpt a.noMagicDo <> | 100 | opt verboseErrorsOpt a.verboseErrors <> |
97 | opt noTcoOpt a.noTco <> | 101 | opt commentsOpt a.comments <> |
98 | opt verboseErrorsOpt a.verboseErrors <> | 102 | opt outputOpt (NullOrUndefined $ Just a.output) <> |
99 | opt commentsOpt a.comments <> | 103 | opt noPrefixOpt a.noPrefix <> |
100 | opt outputOpt a.output <> | 104 | opt requirePathOpt (NullOrUndefined $ Just a.requirePath) <> |
101 | opt noPrefixOpt a.noPrefix <> | 105 | opt ffiOpt a.ffi |
102 | opt requirePathOpt a.requirePath <> | ||
103 | opt ffiOpt a.ffi | ||
104 | 106 | ||
105 | loaderSrcOption :: Foreign -> Maybe (Array String) | 107 | loaderSrcOption :: Foreign -> Maybe (Array String) |
106 | loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query) | 108 | loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query) |