Add --debug flag to bundle command (#3666) · purescript/purescript@135533f · GitHub
Skip to content

Commit 135533f

Browse files
rhendrichdgarrood
authored andcommitted
Add --debug flag to bundle command (#3666)
* Add --debug flag to bundle command This flag causes an optimized-for-humans JSON representation of the modules being bundled to be dumped to stderr, prior to dead code elimination. This feature was requested in the review of #3562. * Add JS fragments to bundle --debug
1 parent e0910da commit 135533f

5 files changed

Lines changed: 104 additions & 4 deletions

File tree

‎app/Command/Bundle.hs‎

Lines changed: 21 additions & 1 deletion

‎package.yaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extra-source-files:
3939
dependencies:
4040
- aeson >=1.0 && <1.5
4141
- aeson-better-errors >=0.8
42+
- aeson-pretty
4243
- ansi-terminal >=0.7.1 && <0.9
4344
- array
4445
- base >=4.11 && <4.13

‎src/Language/PureScript/Bundle.hs‎

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Language.PureScript.Bundle
1414
, ErrorMessage(..)
1515
, printErrorMessage
1616
, getExportedIdentifiers
17+
, Module
1718
) where
1819

1920
import Prelude.Compat
@@ -23,6 +24,7 @@ import Control.Monad
2324
import Control.Monad.Error.Class
2425
import Control.Arrow ((&&&))
2526

27+
import Data.Aeson ((.=))
2628
import Data.Array ((!))
2729
import Data.Char (chr, digitToInt)
2830
import Data.Foldable (fold)
@@ -31,11 +33,14 @@ import Data.Graph
3133
import Data.List (stripPrefix)
3234
import Data.Maybe (catMaybes, fromMaybe, mapMaybe)
3335
import Data.Version (showVersion)
36+
import qualified Data.Aeson as A
3437
import qualified Data.Map as M
3538
import qualified Data.Set as S
39+
import qualified Data.Text.Lazy as T
3640

3741
import Language.JavaScript.Parser
3842
import Language.JavaScript.Parser.AST
43+
import Language.JavaScript.Process.Minify
3944

4045
import 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.
7075
data 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+
7283
moduleName :: ModuleIdentifier -> String
7384
moduleName (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.
114180
data 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.
117190
printErrorMessage :: ErrorMessage -> [String]
118191
printErrorMessage (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

‎stack.yaml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ nix:
1212
- nodejs
1313
- nodePackages.npm
1414
- nodePackages.bower
15+
flags:
16+
aeson-pretty:
17+
lib-only: true

‎tests/TestBundle.hs‎

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)