1-----------------------------------------------------------------------------
4-- Copyright : (c) Phil Freeman 2015
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
11-- | Bundles compiled PureScript modules for the browser.
13-----------------------------------------------------------------------------
15{-# LANGUAGE FlexibleContexts #-}
16{-# LANGUAGE ScopedTypeVariables #-}
17{-# LANGUAGE TupleSections #-}
18{-# LANGUAGE RecordWildCards #-}
20module Main (main) where
22import Data.Traversable (for)
23import Data.Version (showVersion)
25import Control.Applicative
27import Control.Monad.Error.Class
28import Control.Monad.Trans.Except
29import Control.Monad.IO.Class
31import System.FilePath (takeFileName, takeDirectory)
32import System.FilePath.Glob (glob)
33import System.Exit (exitFailure)
34import System.IO (stderr, hPutStrLn)
35import System.Directory (createDirectoryIfMissing)
37import Language.PureScript.Bundle
39import Options.Applicative as Opts
41import qualified Paths_purescript as Paths
43-- | Command line options.
45 { optionsInputFiles :: [FilePath]
46 , optionsOutputFile :: Maybe FilePath
47 , optionsEntryPoints :: [String]
48 , optionsMainModule :: Maybe String
49 , optionsNamespace :: String
50 , optionsRequirePath :: Maybe FilePath
53-- | Given a filename, assuming it is in the correct place on disk, infer a ModuleIdentifier.
54guessModuleIdentifier :: (Applicative m, MonadError ErrorMessage m) => FilePath -> m ModuleIdentifier
55guessModuleIdentifier filename = ModuleIdentifier (takeFileName (takeDirectory filename)) <$> guessModuleType (takeFileName filename)
57 guessModuleType "index.js" = pure Regular
58 guessModuleType "foreign.js" = pure Foreign
59 guessModuleType name = throwError $ UnsupportedModulePath name
61-- | The main application function.
62-- This function parses the input files, performs dead code elimination, filters empty modules
63-- and generates and prints the final Javascript bundle.
64app :: (Applicative m, MonadError ErrorMessage m, MonadIO m) => Options -> m String
66 inputFiles <- concat <$> mapM (liftIO . glob) optionsInputFiles
67 when (null inputFiles) . liftIO $ do
68 hPutStrLn stderr "psc-bundle: No input files."
70 input <- for inputFiles $ \filename -> do
71 js <- liftIO (readFile filename)
72 mid <- guessModuleIdentifier filename
73 length js `seq` return (mid, js) -- evaluate readFile till EOF before returning, not to exhaust file handles
75 let entryIds = map (`ModuleIdentifier` Regular) optionsEntryPoints
77 bundle input entryIds optionsMainModule optionsNamespace optionsRequirePath
79-- | Command line options parser.
80options :: Parser Options
81options = Options <$> some inputFile
82 <*> optional outputFile
84 <*> optional mainModule
86 <*> optional requirePath
88 inputFile :: Parser FilePath
89 inputFile = strArgument $
91 <> help "The input .js file(s)"
93 outputFile :: Parser FilePath
94 outputFile = strOption $
97 <> help "The output .js file"
99 entryPoint :: Parser String
100 entryPoint = strOption $
103 <> help "Entry point module name(s). All code which is not a transitive dependency of an entry point module will be removed."
105 mainModule :: Parser String
106 mainModule = strOption $
108 <> help "Generate code to run the main method in the specified module."
110 namespace :: Parser String
111 namespace = strOption $
116 <> help "Specify the namespace that PureScript modules will be exported to when running in the browser."
118 requirePath :: Parser FilePath
119 requirePath = strOption $
121 <> long "require-path"
123 <> help "The path prefix used in require() calls in the generated JavaScript"
128 opts <- execParser (info (version <*> helper <*> options) infoModList)
129 output <- runExceptT (app opts)
132 hPutStrLn stderr (unlines (printErrorMessage err))
135 case optionsOutputFile opts of
136 Just outputFile -> do
137 createDirectoryIfMissing True (takeDirectory outputFile)
138 writeFile outputFile js
139 Nothing -> putStrLn js
141 infoModList = fullDesc <> headerInfo <> footerInfo
142 headerInfo = header "psc-bundle - Bundles compiled PureScript modules for the browser"
143 footerInfo = footer $ "psc-bundle " ++ showVersion Paths.version
145 version :: Parser (a -> a)
146 version = abortOption (InfoMsg (showVersion Paths.version)) $ long "version" <> help "Show the version number" <> hidden
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