2-- Bundles compiled PureScript modules for the browser.
4-- This module takes as input the individual generated modules from 'Language.PureScript.Make' and
5-- performs dead code elimination, filters empty modules,
6-- and generates the final Javascript bundle.
7module Language.PureScript.Bundle
9 , guessModuleIdentifier
10 , ModuleIdentifier(..)
15 , getExportedIdentifiers
21import Control.Monad.Error.Class
23import Data.Char (chr, digitToInt)
24import Data.Generics (everything, everywhere, mkQ, mkT)
26import Data.List (nub, stripPrefix)
27import Data.Maybe (mapMaybe, catMaybes)
28import Data.Version (showVersion)
29import qualified Data.Set as S
31import Language.JavaScript.Parser
32import Language.JavaScript.Parser.AST
34import qualified Paths_purescript as Paths
36import System.FilePath (takeFileName, takeDirectory)
38-- | The type of error messages. We separate generation and rendering of errors using a data
39-- type, in case we need to match on error types later.
41 = UnsupportedModulePath String
43 | UnableToParseModule String
45 | ErrorInModule ModuleIdentifier ErrorMessage
48-- | Modules are either "regular modules" (i.e. those generated by psc) or foreign modules.
52 deriving (Show, Eq, Ord)
54showModuleType :: ModuleType -> String
55showModuleType Regular = "Regular"
56showModuleType Foreign = "Foreign"
58-- | A module is identified by its module name and its type.
59data ModuleIdentifier = ModuleIdentifier String ModuleType deriving (Show, Eq, Ord)
61moduleName :: ModuleIdentifier -> String
62moduleName (ModuleIdentifier name _) = name
64-- | Given a filename, assuming it is in the correct place on disk, infer a ModuleIdentifier.
65guessModuleIdentifier :: MonadError ErrorMessage m => FilePath -> m ModuleIdentifier
66guessModuleIdentifier filename = ModuleIdentifier (takeFileName (takeDirectory filename)) <$> guessModuleType (takeFileName filename)
68 guessModuleType "index.js" = pure Regular
69 guessModuleType "foreign.js" = pure Foreign
70 guessModuleType name = throwError $ UnsupportedModulePath name
72-- | A piece of code is identified by its module and its name. These keys are used to label vertices
73-- in the dependency graph.
74type Key = (ModuleIdentifier, String)
76-- | An export is either a "regular export", which exports a name from the regular module we are in,
77-- or a reexport of a declaration in the corresponding foreign module.
79-- Regular exports are labelled, since they might re-export an operator with another name.
81 = RegularExport String
83 deriving (Show, Eq, Ord)
85-- | There are four types of module element we are interested in:
87-- 1) Require statements
88-- 2) Member declarations
92-- Each is labelled with the original AST node which generated it, so that we can dump it back
93-- into the output during codegen.
95 = Require JSStatement String (Either String ModuleIdentifier)
96 | Member JSStatement Bool String JSExpression [Key]
97 | ExportsList [(ExportType, String, JSExpression, [Key])]
101-- | A module is just a list of elements of the types listed above.
102data Module = Module ModuleIdentifier [ModuleElement] deriving (Show)
104-- | Prepare an error message for consumption by humans.
105printErrorMessage :: ErrorMessage -> [String]
106printErrorMessage (UnsupportedModulePath s) =
107 [ "A CommonJS module has an unsupported name (" ++ show s ++ ")."
108 , "The following file names are supported:"
109 , " 1) index.js (psc native modules)"
110 , " 2) foreign.js (psc foreign modules)"
112printErrorMessage InvalidTopLevel =
113 [ "Expected a list of source elements at the top level." ]
114printErrorMessage (UnableToParseModule err) =
115 [ "The module could not be parsed:"
118printErrorMessage UnsupportedExport =
119 [ "An export was unsupported. Exports can be defined in one of two ways: "
120 , " 1) exports.name = ..."
121 , " 2) exports = { ... }"
123printErrorMessage (ErrorInModule mid e) =
124 ("Error in module " ++ displayIdentifier mid ++ ":")
126 : map (" " ++) (printErrorMessage e)
128 displayIdentifier (ModuleIdentifier name ty) =
129 name ++ " (" ++ showModuleType ty ++ ")"
131-- | Calculate the ModuleIdentifier which a require(...) statement imports.
132checkImportPath :: String -> ModuleIdentifier -> S.Set String -> Either String ModuleIdentifier
133checkImportPath "./foreign" m _ =
134 Right (ModuleIdentifier (moduleName m) Foreign)
135checkImportPath name _ names
136 | Just name' <- stripPrefix "../" name
137 , name' `S.member` names = Right (ModuleIdentifier name' Regular)
138checkImportPath name _ _ = Left name
140-- | Compute the dependencies of all elements in a module, and add them to the tree.
142-- Members and exports can have dependencies. A dependency is of one of the following forms:
144-- 1) module.name or member["name"]
146-- where module was imported using
148-- var module = require("Module.Name");
152-- where name is the name of a member defined in the current module.
153withDeps :: Module -> Module
154withDeps (Module modulePath es) = Module modulePath (map expandDeps es)
156 -- | Collects all modules which are imported, so that we can identify dependencies of the first type.
157 imports :: [(String, ModuleIdentifier)]
158 imports = mapMaybe toImport es
160 toImport :: ModuleElement -> Maybe (String, ModuleIdentifier)
161 toImport (Require _ nm (Right mid)) = Just (nm, mid)
164 -- | Collects all member names in scope, so that we can identify dependencies of the second type.
165 boundNames :: [String]
166 boundNames = mapMaybe toBoundName es
168 toBoundName :: ModuleElement -> Maybe String
169 toBoundName (Member _ _ nm _ _) = Just nm
170 toBoundName _ = Nothing
172 -- | Calculate dependencies and add them to the current element.
173 expandDeps :: ModuleElement -> ModuleElement
174 expandDeps (Member n f nm decl _) = Member n f nm decl (nub $ dependencies modulePath decl)
175 expandDeps (ExportsList exps) = ExportsList (map expand exps)
177 expand (ty, nm, n1, _) = (ty, nm, n1, nub (dependencies modulePath n1))
178 expandDeps other = other
180 dependencies :: ModuleIdentifier -> JSExpression -> [(ModuleIdentifier, String)]
181 dependencies m = everything (++) (mkQ [] toReference)
183 toReference :: JSExpression -> [(ModuleIdentifier, String)]
184 toReference (JSMemberDot mn _ nm)
185 | JSIdentifier _ mn' <- mn
186 , JSIdentifier _ nm' <- nm
187 , Just mid <- lookup mn' imports
189 toReference (JSMemberSquare mn _ nm _)
190 | JSIdentifier _ mn' <- mn
191 , Just nm' <- fromStringLiteral nm
192 , Just mid <- lookup mn' imports
194 toReference (JSIdentifier _ nm)
195 | nm `elem` boundNames
199-- String literals include the quote chars
200fromStringLiteral :: JSExpression -> Maybe String
201fromStringLiteral (JSStringLiteral _ str) = Just $ strValue str
202fromStringLiteral _ = Nothing
204strValue :: String -> String
205strValue str = go $ drop 1 str
207 go ('\\' : 'b' : xs) = '\b' : go xs
208 go ('\\' : 'f' : xs) = '\f' : go xs
209 go ('\\' : 'n' : xs) = '\n' : go xs
210 go ('\\' : 'r' : xs) = '\r' : go xs
211 go ('\\' : 't' : xs) = '\t' : go xs
212 go ('\\' : 'v' : xs) = '\v' : go xs
213 go ('\\' : '0' : xs) = '\0' : go xs
214 go ('\\' : 'x' : a : b : xs) = chr (a' + b') : go xs
216 a' = 16 * digitToInt a
218 go ('\\' : 'u' : a : b : c : d : xs) = chr (a' + b' + c' + d') : go xs
220 a' = 16 * 16 * 16 * digitToInt a
221 b' = 16 * 16 * digitToInt b
222 c' = 16 * digitToInt c
224 go ('\\' : x : xs) = x : go xs
227 go (x : xs) = x : go xs
230commaList :: JSCommaList a -> [a]
232commaList (JSLOne x) = [x]
233commaList (JSLCons l _ x) = commaList l ++ [x]
235trailingCommaList :: JSCommaTrailingList a -> [a]
236trailingCommaList (JSCTLComma l _) = commaList l
237trailingCommaList (JSCTLNone l) = commaList l
239-- | Attempt to create a Module from a Javascript AST.
241-- Each type of module element is matched using pattern guards, and everything else is bundled into the
243toModule :: forall m. (MonadError ErrorMessage m) => S.Set String -> ModuleIdentifier -> JSAST -> m Module
245 | JSAstProgram smts _ <- top = Module mid <$> traverse toModuleElement smts
246 | otherwise = err InvalidTopLevel
248 err = throwError . ErrorInModule mid
250 toModuleElement :: JSStatement -> m ModuleElement
252 | Just (importName, importPath) <- matchRequire mids mid stmt
253 = pure (Require stmt importName importPath)
255 | Just (exported, name, decl) <- matchMember stmt
256 = pure (Member stmt exported name decl [])
258 | Just props <- matchExportsAssignment stmt
259 = (ExportsList <$> traverse toExport (trailingCommaList props))
261 toExport :: JSObjectProperty -> m (ExportType, String, JSExpression, [Key])
262 toExport (JSPropertyNameandValue name _ [val]) =
263 (,,val,[]) <$> exportType val
264 <*> extractLabel' name
265 toExport _ = err UnsupportedExport
267 exportType :: JSExpression -> m ExportType
268 exportType (JSMemberDot f _ _)
269 | JSIdentifier _ "$foreign" <- f
270 = pure ForeignReexport
271 exportType (JSMemberSquare f _ _ _)
272 | JSIdentifier _ "$foreign" <- f
273 = pure ForeignReexport
274 exportType (JSIdentifier _ s) = pure (RegularExport s)
275 exportType _ = err UnsupportedExport
277 extractLabel' = maybe (err UnsupportedExport) pure . extractLabel
279 toModuleElement other = pure (Other other)
281-- Get a list of all the exported identifiers from a foreign module.
283-- TODO: what if we assign to exports.foo and then later assign to
284-- module.exports (presumably overwriting exports.foo)?
285getExportedIdentifiers :: (MonadError ErrorMessage m)
289getExportedIdentifiers mname top
290 | JSAstProgram stmts _ <- top = concat <$> traverse go stmts
291 | otherwise = err InvalidTopLevel
293 err = throwError . ErrorInModule (ModuleIdentifier mname Foreign)
296 | Just props <- matchExportsAssignment stmt
297 = traverse toIdent (trailingCommaList props)
298 | Just (True, name, _) <- matchMember stmt
303 toIdent (JSPropertyNameandValue name _ [_]) =
306 err UnsupportedExport
308 extractLabel' = maybe (err UnsupportedExport) pure . extractLabel
310-- Matches JS statements like this:
311-- var ModuleName = require("file");
312matchRequire :: S.Set String
315 -> Maybe (String, Either String ModuleIdentifier)
316matchRequire mids mid stmt
317 | JSVariable _ jsInit _ <- stmt
318 , [JSVarInitExpression var varInit] <- commaList jsInit
319 , JSIdentifier _ importName <- var
320 , JSVarInit _ jsInitEx <- varInit
321 , JSMemberExpression req _ argsE _ <- jsInitEx
322 , JSIdentifier _ "require" <- req
323 , [ Just importPath ] <- map fromStringLiteral (commaList argsE)
324 , importPath' <- checkImportPath importPath mid mids
325 = Just (importName, importPath')
329-- Matches JS member declarations.
330matchMember :: JSStatement -> Maybe (Bool, String, JSExpression)
333 | JSVariable _ jsInit _ <- stmt
334 , [JSVarInitExpression var varInit] <- commaList jsInit
335 , JSIdentifier _ name <- var
336 , JSVarInit _ decl <- varInit
337 = Just (False, name, decl)
338 -- exports.foo = expr; exports["foo"] = expr;
339 | JSAssignStatement e (JSAssign _) decl _ <- stmt
340 , Just name <- accessor e
341 = Just (True, name, decl)
345 accessor :: JSExpression -> Maybe String
346 accessor (JSMemberDot exports _ nm)
347 | JSIdentifier _ "exports" <- exports
348 , JSIdentifier _ name <- nm
350 accessor (JSMemberSquare exports _ nm _)
351 | JSIdentifier _ "exports" <- exports
352 , Just name <- fromStringLiteral nm
356-- Matches assignments to module.exports, like this:
357-- module.exports = { ... }
358matchExportsAssignment :: JSStatement -> Maybe JSObjectPropertyList
359matchExportsAssignment stmt
360 | JSAssignStatement e (JSAssign _) decl _ <- stmt
361 , JSMemberDot module' _ exports <- e
362 , JSIdentifier _ "module" <- module'
363 , JSIdentifier _ "exports" <- exports
364 , JSObjectLiteral _ props _ <- decl
369extractLabel :: JSPropertyName -> Maybe String
370extractLabel (JSPropertyString _ nm) = Just $ strValue nm
371extractLabel (JSPropertyIdent _ nm) = Just nm
372extractLabel _ = Nothing
374-- | Eliminate unused code based on the specified entry point set.
375compile :: [Module] -> [ModuleIdentifier] -> [Module]
376compile modules [] = modules
377compile modules entryPoints = filteredModules
379 (graph, _, vertexFor) = graphFromEdges verts
382 verts :: [(ModuleElement, Key, [Key])]
384 Module mid els <- modules
385 concatMap (toVertices mid) els
387 -- | Create a set of vertices for a module element.
389 -- Some special cases worth commenting on:
391 -- 1) Regular exports which simply export their own name do not count as dependencies.
392 -- Regular exports which rename and reexport an operator do count, however.
394 -- 2) Require statements don't contribute towards dependencies, since they effectively get
395 -- inlined wherever they are used inside other module elements.
396 toVertices :: ModuleIdentifier -> ModuleElement -> [(ModuleElement, Key, [Key])]
397 toVertices p m@(Member _ _ nm _ deps) = [(m, (p, nm), deps)]
398 toVertices p m@(ExportsList exps) = mapMaybe toVertex exps
400 toVertex (ForeignReexport, nm, _, ks) = Just (m, (p, nm), ks)
401 toVertex (RegularExport nm, nm1, _, ks) | nm /= nm1 = Just (m, (p, nm1), ks)
405 -- | The set of vertices whose connected components we are interested in keeping.
406 entryPointVertices :: [Vertex]
407 entryPointVertices = catMaybes $ do
408 (_, k@(mid, _), _) <- verts
409 guard $ mid `elem` entryPoints
412 -- | The set of vertices reachable from an entry point
413 reachableSet :: S.Set Vertex
414 reachableSet = S.fromList (concatMap (reachable graph) entryPointVertices)
416 filteredModules :: [Module]
417 filteredModules = map filterUsed modules
419 filterUsed :: Module -> Module
420 filterUsed (Module mid ds) = Module mid (map filterExports (go ds))
422 go :: [ModuleElement] -> [ModuleElement]
425 | not (isDeclUsed d) = go rest
426 | otherwise = d : go rest
428 -- | Filter out the exports for members which aren't used.
429 filterExports :: ModuleElement -> ModuleElement
430 filterExports (ExportsList exps) = ExportsList (filter (\(_, nm, _, _) -> isKeyUsed (mid, nm)) exps)
431 filterExports me = me
433 isDeclUsed :: ModuleElement -> Bool
434 isDeclUsed (Member _ _ nm _ _) = isKeyUsed (mid, nm)
437 isKeyUsed :: Key -> Bool
439 | Just me <- vertexFor k = me `S.member` reachableSet
442-- | Topologically sort the module dependency graph, so that when we generate code, modules can be
443-- defined in the right order.
444sortModules :: [Module] -> [Module]
445sortModules modules = map (\v -> case nodeFor v of (n, _, _) -> n) (reverse (topSort graph))
447 (graph, nodeFor, _) = graphFromEdges $ do
448 m@(Module mid els) <- modules
449 return (m, mid, mapMaybe getKey els)
451 getKey :: ModuleElement -> Maybe ModuleIdentifier
452 getKey (Require _ _ (Right mi)) = Just mi
455-- | A module is empty if it contains no exported members (in other words,
456-- if the only things left after dead code elimination are module imports and
457-- "other" foreign code).
459-- If a module is empty, we don't want to generate code for it.
460isModuleEmpty :: Module -> Bool
461isModuleEmpty (Module _ els) = all isElementEmpty els
463 isElementEmpty :: ModuleElement -> Bool
464 isElementEmpty (ExportsList exps) = null exps
465 isElementEmpty Require{} = True
466 isElementEmpty (Other _) = True
467 isElementEmpty _ = False
469-- | Generate code for a set of modules, including a call to main().
471-- Modules get defined on the global PS object, as follows:
474-- (function(exports) {
476-- })(PS["Module.Name"] = PS["Module.Name"] || {});
478-- In particular, a module and its foreign imports share the same namespace inside PS.
479-- This saves us from having to generate unique names for a module and its foreign imports,
480-- and is safe since a module shares a namespace with its foreign imports in PureScript as well
481-- (so there is no way to have overlaps in code generated by psc).
482codeGen :: Maybe String -- ^ main module
483 -> String -- ^ namespace
484 -> [Module] -- ^ input modules
486codeGen optionsMainModule optionsNamespace ms = renderToString (JSAstProgram (prelude : concatMap moduleToJS ms ++ maybe [] runMain optionsMainModule) JSNoAnnot)
488 moduleToJS :: Module -> [JSStatement]
489 moduleToJS (Module mn ds) = wrap (moduleName mn) (indent (concatMap declToJS ds))
491 declToJS :: ModuleElement -> [JSStatement]
492 declToJS (Member n _ _ _ _) = [n]
493 declToJS (Other n) = [n]
494 declToJS (Require _ nm req) =
498 JSVarInitExpression (JSIdentifier sp nm)
499 (JSVarInit sp $ either require (moduleReference sp . moduleName) req )
500 ]) (JSSemi JSNoAnnot)
502 declToJS (ExportsList exps) = map toExport exps
505 toExport :: (ExportType, String, JSExpression, [Key]) -> JSStatement
506 toExport (_, nm, val, _) =
508 (JSMemberSquare (JSIdentifier lfsp "exports") JSNoAnnot
514 -- comma lists are reverse-consed
515 cList :: [a] -> JSCommaList a
518 cList l = go $ reverse l
521 go (h:t)= JSLCons (go t) JSNoAnnot h
522 go [] = error "Invalid case in comma-list"
524 indent :: [JSStatement] -> [JSStatement]
525 indent = everywhere (mkT squash)
527 squash JSNoAnnot = (JSAnnot (TokenPn 0 0 2) [])
528 squash (JSAnnot pos ann) = JSAnnot (keepCol pos) (map splat ann)
530 splat (CommentA pos s) = CommentA (keepCol pos) s
531 splat (WhiteSpace pos w) = WhiteSpace (keepCol pos) w
534 keepCol (TokenPn _ _ c) = TokenPn 0 0 (if c >= 0 then c + 2 else 2)
536 prelude :: JSStatement
537 prelude = JSVariable (JSAnnot tokenPosnEmpty [ CommentA tokenPosnEmpty $ "// Generated by psc-bundle " ++ showVersion Paths.version
538 , WhiteSpace tokenPosnEmpty "\n" ])
540 JSVarInitExpression (JSIdentifier sp optionsNamespace)
541 (JSVarInit sp (emptyObj sp))
542 ]) (JSSemi JSNoAnnot)
544 require :: String -> JSExpression
546 JSMemberExpression (JSIdentifier JSNoAnnot "require") JSNoAnnot (cList [ str mn ]) JSNoAnnot
548 moduleReference :: JSAnnot -> String -> JSExpression
549 moduleReference a mn =
550 JSMemberSquare (JSIdentifier a optionsNamespace) JSNoAnnot
553 str :: String -> JSExpression
554 str s = JSStringLiteral JSNoAnnot $ "\"" ++ s ++ "\""
557 emptyObj :: JSAnnot -> JSExpression
558 emptyObj a = JSObjectLiteral a (JSCTLNone JSLNil) JSNoAnnot
560 wrap :: String -> [JSStatement] -> [JSStatement]
563 JSMethodCall (JSExpressionParen lf (JSFunctionExpression JSNoAnnot JSIdentNone JSNoAnnot
564 (JSLOne (JSIdentName JSNoAnnot "exports")) JSNoAnnot
565 (JSBlock sp (lfHead ds) lf)) -- \n not quite in right place
568 (JSLOne (JSAssignExpression (moduleReference JSNoAnnot mn) (JSAssign sp)
569 (JSExpressionBinary (moduleReference sp mn) (JSBinOpOr sp) (emptyObj sp))))
574 lfHead (h:t) = (addAnn (WhiteSpace tokenPosnEmpty "\n ") h) : t
577 addAnn :: CommentAnnotation -> JSStatement -> JSStatement
578 addAnn a (JSExpressionStatement (JSStringLiteral ann s) _) =
579 (JSExpressionStatement (JSStringLiteral (appendAnn a ann) s) (JSSemi JSNoAnnot))
582 appendAnn a JSNoAnnot = (JSAnnot tokenPosnEmpty [a])
583 appendAnn a (JSAnnot _ anns) = JSAnnot tokenPosnEmpty (a:anns ++ [WhiteSpace tokenPosnEmpty " "])
585 runMain :: String -> [JSStatement]
588 (JSMemberDot (moduleReference lf mn) JSNoAnnot
589 (JSIdentifier JSNoAnnot "main"))
590 JSNoAnnot (cList []) JSNoAnnot (JSSemi JSNoAnnot)]
593 lf = JSAnnot tokenPosnEmpty [ WhiteSpace tokenPosnEmpty "\n" ]
597 lfsp = JSAnnot tokenPosnEmpty [ WhiteSpace tokenPosnEmpty "\n " ]
600 sp = JSAnnot tokenPosnEmpty [ WhiteSpace tokenPosnEmpty " " ]
602-- | The bundling function.
603-- This function performs dead code elimination, filters empty modules
604-- and generates and prints the final Javascript bundle.
605bundle :: (MonadError ErrorMessage m)
606 => [(ModuleIdentifier, String)] -- ^ The input modules. Each module should be javascript rendered from 'Language.PureScript.Make' or @psc@.
607 -> [ModuleIdentifier] -- ^ Entry points. These module identifiers are used as the roots for dead-code elimination
608 -> Maybe String -- ^ An optional main module.
609 -> String -- ^ The namespace (e.g. PS).
611bundle inputStrs entryPoints mainModule namespace = do
612 input <- forM inputStrs $ \(ident, js) -> do
613 ast <- either (throwError . ErrorInModule ident . UnableToParseModule) pure $ parse js (moduleName ident)
616 let mids = S.fromList (map (moduleName . fst) input)
618 modules <- traverse (fmap withDeps . uncurry (toModule mids)) input
620 let compiled = compile modules entryPoints
621 sorted = sortModules (filter (not . isModuleEmpty) compiled)
623 return (codeGen mainModule namespace sorted)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list