@@ -14,6 +14,7 @@ module Language.PureScript.Bundle
1414 , ErrorMessage (.. )
1515 , printErrorMessage
1616 , getExportedIdentifiers
17+ , Module
1718 ) where
1819
1920import Prelude.Compat
@@ -23,6 +24,7 @@ import Control.Monad
2324import Control.Monad.Error.Class
2425import Control.Arrow ((&&&) )
2526
27+ import Data.Aeson ((.=) )
2628import Data.Array ((!) )
2729import Data.Char (chr , digitToInt )
2830import Data.Foldable (fold )
@@ -31,11 +33,14 @@ import Data.Graph
3133import Data.List (stripPrefix )
3234import Data.Maybe (catMaybes , fromMaybe , mapMaybe )
3335import Data.Version (showVersion )
36+ import qualified Data.Aeson as A
3437import qualified Data.Map as M
3538import qualified Data.Set as S
39+ import qualified Data.Text.Lazy as T
3640
3741import Language.JavaScript.Parser
3842import Language.JavaScript.Parser.AST
43+ import Language.JavaScript.Process.Minify
3944
4045import qualified Paths_purescript as Paths
4146
@@ -69,6 +74,12 @@ showModuleType Foreign = "Foreign"
6974-- | A module is identified by its module name and its type.
7075data ModuleIdentifier = ModuleIdentifier String ModuleType deriving (Show , Eq , Ord )
7176
77+ instance A. ToJSON ModuleIdentifier where
78+ toJSON (ModuleIdentifier name mt) =
79+ A. object [ " name" .= name
80+ , " type" .= show mt
81+ ]
82+
7283moduleName :: ModuleIdentifier -> String
7384moduleName (ModuleIdentifier name _) = name
7485
@@ -110,9 +121,71 @@ data ModuleElement
110121 | Skip JSStatement
111122 deriving (Show )
112123
124+ instance A. ToJSON ModuleElement where
125+ toJSON = \ case
126+ (Require _ name (Right target)) ->
127+ A. object [ " type" .= A. String " Require"
128+ , " name" .= name
129+ , " target" .= target
130+ ]
131+ (Require _ name (Left targetPath)) ->
132+ A. object [ " type" .= A. String " Require"
133+ , " name" .= name
134+ , " targetPath" .= targetPath
135+ ]
136+ (Member _ public name _ dependsOn) ->
137+ A. object [ " type" .= A. String " Member"
138+ , " name" .= name
139+ , " visibility" .= A. String (if public then " Public" else " Internal" )
140+ , " dependsOn" .= map keyToJSON dependsOn
141+ ]
142+ (ExportsList exports) ->
143+ A. object [ " type" .= A. String " ExportsList"
144+ , " exports" .= map exportToJSON exports
145+ ]
146+ (Other stmt) ->
147+ A. object [ " type" .= A. String " Other"
148+ , " js" .= getFragment stmt
149+ ]
150+ (Skip stmt) ->
151+ A. object [ " type" .= A. String " Skip"
152+ , " js" .= getFragment stmt
153+ ]
154+
155+ where
156+
157+ keyToJSON (mid, member) =
158+ A. object [ " module" .= mid
159+ , " member" .= member
160+ ]
161+
162+ exportToJSON (RegularExport sourceName, name, _, dependsOn) =
163+ A. object [ " type" .= A. String " RegularExport"
164+ , " name" .= name
165+ , " sourceName" .= sourceName
166+ , " dependsOn" .= map keyToJSON dependsOn
167+ ]
168+ exportToJSON (ForeignReexport , name, _, dependsOn) =
169+ A. object [ " type" .= A. String " ForeignReexport"
170+ , " name" .= name
171+ , " dependsOn" .= map keyToJSON dependsOn
172+ ]
173+
174+ getFragment = ellipsize . renderToText . minifyJS . flip JSAstStatement JSNoAnnot
175+ where
176+ ellipsize text = if T. compareLength text 20 == GT then T. take 19 text `T.snoc` ellipsis else text
177+ ellipsis = ' \x2026 '
178+
113179-- | A module is just a list of elements of the types listed above.
114180data Module = Module ModuleIdentifier (Maybe FilePath ) [ModuleElement ] deriving (Show )
115181
182+ instance A. ToJSON Module where
183+ toJSON (Module moduleId filePath elements) =
184+ A. object [ " moduleId" .= moduleId
185+ , " filePath" .= filePath
186+ , " elements" .= elements
187+ ]
188+
116189-- | Prepare an error message for consumption by humans.
117190printErrorMessage :: ErrorMessage -> [String ]
118191printErrorMessage (UnsupportedModulePath s) =
@@ -730,8 +803,9 @@ bundleSM :: (MonadError ErrorMessage m)
730803 -> Maybe String -- ^ An optional main module.
731804 -> String -- ^ The namespace (e.g. PS).
732805 -> Maybe FilePath -- ^ The output file name (if there is one - in which case generate source map)
806+ -> Maybe ([Module ] -> m () ) -- ^ Optionally report the parsed modules prior to DCE -- used by "bundle --debug"
733807 -> m (Maybe SourceMapping , String )
734- bundleSM inputStrs entryPoints mainModule namespace outFilename = do
808+ bundleSM inputStrs entryPoints mainModule namespace outFilename reportRawModules = do
735809 let mid (a,_,_) = a
736810 forM_ mainModule $ \ mname ->
737811 when (mname `notElem` map (moduleName . mid) inputStrs) (throwError (MissingMainModule mname))
@@ -745,6 +819,8 @@ bundleSM inputStrs entryPoints mainModule namespace outFilename = do
745819
746820 modules <- traverse (fmap withDeps . (\ (a,fn,c) -> toModule mids a fn c)) input
747821
822+ forM_ reportRawModules ($ modules)
823+
748824 let compiled = compile modules entryPoints
749825 sorted = sortModules (filter (not . isModuleEmpty) compiled)
750826
@@ -759,4 +835,4 @@ bundle :: (MonadError ErrorMessage m)
759835 -> Maybe String -- ^ An optional main module.
760836 -> String -- ^ The namespace (e.g. PS).
761837 -> m String
762- bundle inputStrs entryPoints mainModule namespace = snd <$> bundleSM (map (\ (a,b) -> (a,Nothing ,b)) inputStrs) entryPoints mainModule namespace Nothing
838+ bundle inputStrs entryPoints mainModule namespace = snd <$> bundleSM (map (\ (a,b) -> (a,Nothing ,b)) inputStrs) entryPoints mainModule namespace Nothing Nothing
0 commit comments