Skip to content

Commit

Permalink
POC minifying bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
stoeffel committed Nov 6, 2018
1 parent 6e04110 commit f701309
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/Builder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Builder
( build
) where

import CliArguments (Args(..))
import CliArguments (Args(..), Minify(..))
import qualified Compile
import ConcatModule
import Config
Expand All @@ -19,13 +19,15 @@ import qualified Data.Text.IO as TIO
import qualified Data.Tree as Tree
import qualified DependencyTree
import qualified EntryPoints
import GHC.IO.Handle
import qualified Init
import qualified Logger
import qualified Message
import ProgressBar (ProgressBar, complete, start, tick)
import qualified System.Console.AsciiProgress as AsciiProgress
import qualified System.Exit
import System.Exit
import System.FilePath ((<.>), (</>))
import System.Process

build :: Config.Config -> Args -> IO ()
build config args = do
Expand Down Expand Up @@ -87,12 +89,28 @@ buildHelp config args = do
do createdModulesJson pg config modules
complete pg
traverse (Compile.printTime args) result
_minified <- Concurrent.mapConcurrently (minfiyModule pg config args) modules
-- RETURN WARNINGS IF ANY
let warnings = Data.Maybe.catMaybes (fmap Compile.warnings result)
case warnings of
[] -> return $ Success entryPoints
xs -> return $ Warnings entryPoints xs

minfiyModule :: ProgressBar -> Config -> Args -> FilePath -> IO ()
minfiyModule _pg Config {minifierPath} Args {minify} output = do
case minify of
DontMinify -> return ()
Minify -> do
(_, Just out, Just err, ph) <-
createProcess
(proc "bash" ["-c", minifierPath <> " " <> output])
{std_out = CreatePipe, std_err = CreatePipe}
_ec <- waitForProcess ph
content <- hGetContents out
print content
_errContent <- hGetContents err
pure ()

createdModulesJson :: ProgressBar -> Config -> [FilePath] -> IO ()
createdModulesJson pg config paths = do
let encodedPaths = Aeson.encode paths
Expand Down
11 changes: 11 additions & 0 deletions src/CliArguments.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module CliArguments
( Args(..)
, RunMode(..)
, Minify(..)
, readArguments
) where

Expand All @@ -17,6 +18,7 @@ data Args = Args
, warn :: Bool
, time :: Bool
, clean :: Bool
, minify :: Minify
, runMode :: RunMode
}

Expand All @@ -25,6 +27,10 @@ data RunMode
| Watch
| Version

data Minify
= Minify
| DontMinify

readArguments :: IO Args
readArguments =
execParser (info (parser <**> helper) $ fullDesc <> progDesc "🚀 📦")
Expand All @@ -48,6 +54,7 @@ parser = do
clean <-
switch
(long "clean" <> short 'c' <> help "Cleans elm-stuff and removes .jetpack")
minify <- switch (long "minify" <> help "Minify the compiled js bundle.")
return
Args
{ entryPointGlob = entryPointGlob
Expand All @@ -62,6 +69,10 @@ parser = do
else warn
, time = time
, clean = clean
, minify =
if minify
then Minify
else DontMinify
, runMode =
if version
then Version
Expand Down
2 changes: 2 additions & 0 deletions src/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ data Config = Config
, outputDir :: FilePath
, elmMakePath :: Maybe FilePath
, coffeePath :: Maybe FilePath
, minifierPath :: FilePath
, noParse :: [FilePath]
, watchFileExt :: [T.Text]
, watchIgnorePatterns :: [T.Text]
Expand All @@ -43,6 +44,7 @@ instance Aeson.FromJSON Config where
v .: "output_js_directory" <*>
v .:? "elm_make_path" <*>
v .:? "coffee_path" <*>
v .: "minifier_path" <*>
v .:? "no_parse" .!= [] <*>
v .:? "watch_file_extensions" .!= [".elm", ".coffee", ".js", ".json"] <*>
v .:? "watch_file_ignore_patterns" .!= ["/[.]#[^/]*$", "/~[^/]*$"]
Expand Down
1 change: 1 addition & 0 deletions test/ConfigSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ suite =
, outputDir = "app" </> "js"
, elmMakePath = Nothing
, coffeePath = Nothing
, minifierPath = "uglify"
, noParse =
["." </> "node_modules" </> "clipboard" </> "clipboard.js"]
, watchFileExt = [".elm", ".coffee", ".js", ".json"]
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/jetpack.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"temp_directory" : "app/tmp",
"log_directory" : "app/logs",
"output_js_directory" : "app/js",
"minifier_path" : "uglify",
"no_parse": [
"./node_modules/clipboard/clipboard.js"
]
Expand Down

0 comments on commit f701309

Please sign in to comment.