]>
Commit | Line | Data |
---|---|---|
c194f84c | 1 | module PursLoader.Loader |
1983893b | 2 | ( Effects() |
1c49c88e | 3 | , Effects_() |
c194f84c | 4 | , loader |
5 | , loaderFn | |
6 | ) where | |
7 | ||
0f0403a8 | 8 | import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), (<<<), bind, const, id, pure, unit) |
03b840cb | 9 | |
63d6a244 | 10 | import Control.Bind (join) |
46039343 | 11 | import Control.Monad.Eff (Eff(), foreachE) |
1c49c88e | 12 | import Control.Monad.Eff.Console (CONSOLE()) |
0f0403a8 | 13 | import Control.Monad.Eff.Exception (EXCEPTION(), Error(), error) |
c194f84c | 14 | |
63d6a244 | 15 | import Data.Array ((!!)) |
46039343 | 16 | import Data.Either (Either(..), either) |
c194f84c | 17 | import Data.Function (Fn2(), mkFn2) |
cb2f4c88 | 18 | import Data.Maybe (maybe) |
46039343 | 19 | import Data.Nullable (toMaybe) |
63d6a244 | 20 | import Data.String.Regex (Regex(), match, noFlags, regex) |
21 | ||
22 | import Unsafe.Coerce (unsafeCoerce) | |
23 | ||
47973be1 | 24 | import PursLoader.Debug (debug) |
25 | import PursLoader.JsStringEscape (jsStringEscape) | |
63d6a244 | 26 | import PursLoader.LoaderRef |
46039343 | 27 | ( AsyncCallback() |
28 | , LoaderRef() | |
63d6a244 | 29 | , Loader() |
30 | , async | |
31 | , cacheable | |
63d6a244 | 32 | , addDependency |
33 | , resourcePath | |
34 | ) | |
87145c4d | 35 | import PursLoader.Path (dirname, joinPath, relative) |
46039343 | 36 | import PursLoader.Plugin as Plugin |
c194f84c | 37 | |
1c49c88e | 38 | type Effects eff = (console :: CONSOLE, err :: EXCEPTION | eff) |
39 | ||
40 | type Effects_ eff = Effects (loader :: Loader | eff) | |
c194f84c | 41 | |
1c49c88e | 42 | loader :: forall eff. LoaderRef -> String -> Eff (Effects_ eff) Unit |
63d6a244 | 43 | loader ref source = do |
44 | callback <- async ref | |
3610dff1 | 45 | |
63d6a244 | 46 | cacheable ref |
c194f84c | 47 | |
3afbe2fd | 48 | debug "Invoke PureScript plugin compilation" |
49 | ||
46039343 | 50 | pluginContext.compile (compile callback) |
63d6a244 | 51 | where |
1c49c88e | 52 | pluginContext :: Plugin.Context (Effects_ eff) |
63d6a244 | 53 | pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext |
c194f84c | 54 | |
1c49c88e | 55 | compile :: AsyncCallback (Effects eff) -> Plugin.Compile (Effects_ eff) |
0f0403a8 | 56 | compile callback error' graph = do |
3afbe2fd | 57 | either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name |
58 | ||
46039343 | 59 | addDependency ref (resourcePath ref) |
60 | ||
1c49c88e | 61 | either (const $ callback (pure fixedError) "") id |
46039343 | 62 | (handle <$> name <*> dependencies <*> exports) |
63 | where | |
1c49c88e | 64 | fixedError :: Error |
65 | fixedError = error "PureScript compilation has failed." | |
66 | ||
67 | handle :: String -> Array String -> String -> Eff (Effects_ eff) Unit | |
46039343 | 68 | handle name' deps res = do |
845f3ec3 | 69 | debug ("Adding PureScript dependencies for " ++ name') |
70 | foreachE deps (addDependency ref) | |
b1b6e146 | 71 | debug "Generated loader result" |
72 | debug res | |
1c49c88e | 73 | callback (const fixedError <$> toMaybe error') res |
46039343 | 74 | |
75 | exports :: Either Error String | |
87145c4d | 76 | exports = |
77 | if pluginContext.options.bundle | |
78 | then bundleExport <$> name | |
79 | else moduleExport <<< modulePath <$> name | |
80 | where | |
81 | bundleExport :: String -> String | |
47973be1 | 82 | bundleExport name' = "module.exports = require('" ++ jsStringEscape path ++ "')['" ++ name' ++ "'];" |
87145c4d | 83 | where |
84 | path :: String | |
85 | path = relative resourceDir pluginContext.options.bundleOutput | |
46039343 | 86 | |
87145c4d | 87 | moduleExport :: String -> String |
47973be1 | 88 | moduleExport path = "module.exports = require('" ++ jsStringEscape path ++ "');" |
46039343 | 89 | |
87145c4d | 90 | modulePath :: String -> String |
91 | modulePath = relative resourceDir <<< joinPath pluginContext.options.output | |
92 | ||
93 | resourceDir :: String | |
94 | resourceDir = dirname (resourcePath ref) | |
46039343 | 95 | |
96 | dependencies :: Either Error (Array String) | |
845f3ec3 | 97 | dependencies = Plugin.dependenciesOf graph (resourcePath ref) |
46039343 | 98 | |
99 | name :: Either Error String | |
100 | name = | |
101 | maybe (Left $ error "Failed to parse module name") Right | |
102 | (join $ match re source >>= \as -> as !! 1) | |
103 | where | |
104 | re :: Regex | |
105 | re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } | |
106 | ||
1c49c88e | 107 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects_ eff) Unit) |
c194f84c | 108 | loaderFn = mkFn2 loader |