diff options
author | eric <thul.eric@gmail.com> | 2016-02-25 21:48:42 -0500 |
---|---|---|
committer | eric <thul.eric@gmail.com> | 2016-02-25 21:48:42 -0500 |
commit | d7d4ec32989320d0fb62413d59b137279824de5b (patch) | |
tree | 2ab19c4a3f01925f1ee5c29f99c4f6cd6e5c1166 /src/PursLoader/Loader.purs | |
parent | ef1525b5a0fe3f4561493aaca49907dba827dbd6 (diff) | |
parent | f97818b550b17455448ff7fe09689b326c30912b (diff) | |
download | purs-loader-d7d4ec32989320d0fb62413d59b137279824de5b.tar.gz purs-loader-d7d4ec32989320d0fb62413d59b137279824de5b.tar.zst purs-loader-d7d4ec32989320d0fb62413d59b137279824de5b.zip |
Merge pull request #38 from ethul/topic/dependency-graph
Topic/dependency graph
Diffstat (limited to 'src/PursLoader/Loader.purs')
-rw-r--r-- | src/PursLoader/Loader.purs | 92 |
1 files changed, 58 insertions, 34 deletions
diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs index affce53..f78153f 100644 --- a/src/PursLoader/Loader.purs +++ b/src/PursLoader/Loader.purs | |||
@@ -4,25 +4,27 @@ module PursLoader.Loader | |||
4 | , loaderFn | 4 | , loaderFn |
5 | ) where | 5 | ) where |
6 | 6 | ||
7 | import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (<<<), (++), bind, const) | 7 | import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), bind, const, id, pure, unit) |
8 | 8 | ||
9 | import Control.Apply ((*>)) | ||
9 | import Control.Bind (join) | 10 | import Control.Bind (join) |
10 | import Control.Monad.Eff (Eff()) | 11 | import Control.Monad.Eff (Eff(), foreachE) |
11 | import Control.Monad.Eff.Exception (Error(), error) | 12 | import Control.Monad.Eff.Exception (Error(), error) |
12 | 13 | ||
13 | import Data.Array ((!!)) | 14 | import Data.Array ((!!)) |
15 | import Data.Bifunctor (lmap) | ||
16 | import Data.Either (Either(..), either) | ||
17 | import Data.Foreign.Class (read) | ||
14 | import Data.Function (Fn2(), mkFn2) | 18 | import Data.Function (Fn2(), mkFn2) |
15 | import Data.Maybe (Maybe(..), maybe) | 19 | import Data.Maybe (Maybe(..), maybe) |
16 | import Data.Either (either) | 20 | import Data.Nullable (toMaybe) |
17 | import Data.Foreign (Foreign()) | ||
18 | import Data.Foreign.Class (read) | ||
19 | import Data.Foreign.Null (runNull) | ||
20 | import Data.String.Regex (Regex(), match, noFlags, regex) | 21 | import Data.String.Regex (Regex(), match, noFlags, regex) |
21 | 22 | ||
22 | import Unsafe.Coerce (unsafeCoerce) | 23 | import Unsafe.Coerce (unsafeCoerce) |
23 | 24 | ||
24 | import PursLoader.LoaderRef | 25 | import PursLoader.LoaderRef |
25 | ( LoaderRef() | 26 | ( AsyncCallback() |
27 | , LoaderRef() | ||
26 | , Loader() | 28 | , Loader() |
27 | , async | 29 | , async |
28 | , cacheable | 30 | , cacheable |
@@ -33,46 +35,68 @@ import PursLoader.LoaderRef | |||
33 | ) | 35 | ) |
34 | 36 | ||
35 | import PursLoader.LoaderUtil (parseQuery) | 37 | import PursLoader.LoaderUtil (parseQuery) |
36 | import PursLoader.Options (runOptions) | 38 | import PursLoader.Options (Options(..)) |
37 | import PursLoader.Path (dirname, relative) | 39 | import PursLoader.Path (dirname, relative) |
40 | import PursLoader.Plugin as Plugin | ||
38 | 41 | ||
39 | type Effects eff = (loader :: Loader | eff) | 42 | type Effects eff = (loader :: Loader | eff) |
40 | 43 | ||
41 | type PurescriptWebpackPluginContext eff = { compile :: (Foreign -> Eff (Effects eff) Unit) -> Eff (Effects eff) Unit } | ||
42 | |||
43 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit | 44 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit |
44 | loader ref source = do | 45 | loader ref source = do |
45 | callback <- async ref | 46 | callback <- async ref |
46 | 47 | ||
47 | cacheable ref | 48 | cacheable ref |
48 | 49 | ||
49 | let parsed = parseQuery $ query ref | 50 | pluginContext.compile (compile callback) |
50 | |||
51 | options = either (const Nothing) (Just <<< runOptions) (read parsed) | ||
52 | |||
53 | moduleName = join $ match moduleRegex source >>= \as -> as !! 1 | ||
54 | |||
55 | resourceDir = dirname (resourcePath ref) | ||
56 | |||
57 | modulePath = (\opts -> relative resourceDir opts.bundleOutput) <$> options | ||
58 | |||
59 | result = (\path name -> "module.exports = require('" ++ path ++ "')['" ++ name ++ "'];") <$> modulePath <*> moduleName | ||
60 | |||
61 | clearDependencies ref | ||
62 | |||
63 | addDependency ref (resourcePath ref) | ||
64 | |||
65 | pluginContext.compile (\err -> maybe (callback (Just $ error "Failed to run loader") "") | ||
66 | (callback (compileError err)) result) | ||
67 | where | 51 | where |
68 | moduleRegex :: Regex | 52 | pluginContext :: Plugin.Context (Effects eff) |
69 | moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } | ||
70 | |||
71 | pluginContext :: PurescriptWebpackPluginContext eff | ||
72 | pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext | 53 | pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext |
73 | 54 | ||
74 | compileError :: Foreign -> Maybe Error | 55 | compile :: AsyncCallback eff -> Plugin.Compile (Effects eff) |
75 | compileError value = either (const $ Just (error "Failed to compile")) ((<$>) error) (runNull <$> read value) | 56 | compile callback error' { srcMap, ffiMap, graph } = do |
57 | clearDependencies ref | ||
58 | |||
59 | addDependency ref (resourcePath ref) | ||
60 | |||
61 | either (\err -> callback (Just err) "") id | ||
62 | (handle <$> name <*> dependencies <*> exports) | ||
63 | where | ||
64 | handle :: String -> Array String -> String -> Eff (Effects eff) Unit | ||
65 | handle name' deps res = do | ||
66 | addTransitive name' | ||
67 | foreachE deps addTransitive | ||
68 | callback (toMaybe error') res | ||
69 | |||
70 | exports :: Either Error String | ||
71 | exports = (\a b -> "module.exports = require('" ++ a ++ "')['" ++ b ++ "'];") <$> path <*> name | ||
72 | |||
73 | dependencies :: Either Error (Array String) | ||
74 | dependencies = name >>= Plugin.dependenciesOf graph | ||
75 | |||
76 | addTransitive :: String -> Eff (Effects eff) Unit | ||
77 | addTransitive dep = addDep (Plugin.get srcMap dep) *> addDep (Plugin.get ffiMap dep) | ||
78 | where | ||
79 | addDep :: Maybe String -> Eff (Effects eff) Unit | ||
80 | addDep = maybe (pure unit) (addDependency ref) | ||
81 | |||
82 | name :: Either Error String | ||
83 | name = | ||
84 | maybe (Left $ error "Failed to parse module name") Right | ||
85 | (join $ match re source >>= \as -> as !! 1) | ||
86 | where | ||
87 | re :: Regex | ||
88 | re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } | ||
89 | |||
90 | path :: Either Error String | ||
91 | path = (\(Options opts) -> relative resourceDir opts.bundleOutput) <$> options | ||
92 | where | ||
93 | options :: Either Error Options | ||
94 | options = | ||
95 | lmap (const $ error "Failed to parse loader query") | ||
96 | (read $ parseQuery (query ref)) | ||
97 | |||
98 | resourceDir :: String | ||
99 | resourceDir = dirname (resourcePath ref) | ||
76 | 100 | ||
77 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) | 101 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) |
78 | loaderFn = mkFn2 loader | 102 | loaderFn = mkFn2 loader |