-
-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathbuild.cr
125 lines (102 loc) · 4 KB
/
build.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
module Mint
class Cli < Admiral::Command
class Build < Admiral::Command
include Command
define_help description: "Builds the project for production."
define_flag runtime : String,
description: "If specified, the supplied runtime will be used instead of the default."
define_flag no_optimize : Bool,
description: "If specified, the resulting JavaScript code will not be optimized.",
default: false
define_flag skip_icons : Bool,
description: "If specified, the application icons will not be generated.",
default: false
define_flag generate_manifest : Bool,
description: "If specified, the web manifest will be generated.",
default: false
define_flag generate_source_maps : Bool,
description: "If specified, source maps will be generated.",
default: false
define_flag verbose : Bool,
description: "If specified, all written files will be logged.",
default: false
define_flag watch : Bool,
description: "If specified, will build on every change.",
default: false,
short: "w"
define_flag timings : Bool,
description: "If specified, timings will be printed.",
default: false
define_flag env : String,
description: "Loads the given .env file.",
short: "e"
def run
execute "Building for production",
check_dependencies: true do
Workspace.new(
path: Path[Dir.current, "mint.json"].to_s,
dot_env: flags.env || ".env",
check: Check::Environment,
include_tests: false,
format: false,
listener: ->(result : TypeChecker | Error) do
terminal.reset if flags.watch
case result
in TypeChecker
terminal.measure %(#{COG} Clearing the "#{DIST_DIR}" directory...) do
FileUtils.rm_rf DIST_DIR
end
files =
terminal.measure "#{COG} Building..." do
Bundler.new(
artifacts: result.artifacts,
config: Bundler::Config.new(
generate_source_maps: flags.generate_source_maps,
generate_manifest: flags.generate_manifest,
skip_icons: flags.skip_icons,
optimize: !flags.no_optimize,
runtime_path: flags.runtime,
json: MintJson.current,
include_program: true,
live_reload: false,
hash_assets: true,
test: nil)).bundle
end || {} of String => Proc(String)
bundle_size = 0
files.keys.sort_by!(&.size).reverse!.each do |path|
chopped =
path.lchop('/')
content =
files[path].call
size =
content.bytesize
proc =
-> { File.write_p(Path[DIST_DIR, chopped], content) }
bundle_size +=
size
if flags.verbose
terminal.measure "#{COG} Writing #{chopped} (#{size.humanize_bytes(format: :JEDEC)})..." do
proc.call
end
else
proc.call
end
end
terminal.divider
terminal.puts "Bundle size: #{bundle_size.humanize_bytes(format: :JEDEC)}"
terminal.puts "Files: #{files.size}"
if flags.timings
terminal.divider
Logger.print(terminal)
end
in Error
terminal.print result.to_terminal
end
end)
# Start wathing for changes if the flag is set.
sleep if flags.watch
end
end
end
end
end