Skip to content

Commit

Permalink
Merge pull request #676 from farism/build-extra-flags
Browse files Browse the repository at this point in the history
Extra build mode flags: `--skip-manifest`, `--inline`, and `--watch`
  • Loading branch information
Sija authored Sep 26, 2023
2 parents 2ae428c + d80bf47 commit 00df3cc
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 23 deletions.
61 changes: 46 additions & 15 deletions src/builder.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
module Mint
class Builder
def initialize(relative, skip_service_worker, skip_icons, optimize, runtime_path)
def initialize(relative, skip_manifest, skip_service_worker, skip_icons, optimize, inline, runtime_path, watch)
build(relative, skip_manifest, skip_service_worker, skip_icons, optimize, inline, runtime_path)

if watch
watch_workspace(relative, skip_manifest, skip_service_worker, skip_icons, optimize, inline, runtime_path)
end
end

def build(relative, skip_manifest, skip_service_worker, skip_icons, optimize, inline, runtime_path)
json = MintJson.parse_current

if !skip_icons && !Process.find_executable("convert")
Expand All @@ -27,19 +35,21 @@ module Mint
index_js, artifacts =
index(json.application.css_prefix, relative, optimize, runtime_path, json.web_components)

File.write Path[DIST_DIR, "index.js"], index_js
unless inline
File.write Path[DIST_DIR, "index.js"], index_js

if SourceFiles.external_javascripts?
terminal.measure "#{COG} Writing external javascripts..." do
File.write Path[DIST_DIR, "external-javascripts.js"],
SourceFiles.external_javascripts
if SourceFiles.external_javascripts?
terminal.measure "#{COG} Writing external javascripts..." do
File.write Path[DIST_DIR, "external-javascripts.js"],
SourceFiles.external_javascripts
end
end
end

if SourceFiles.external_stylesheets?
terminal.measure "#{COG} Writing external stylesheets..." do
File.write Path[DIST_DIR, "external-stylesheets.css"],
SourceFiles.external_stylesheets
if SourceFiles.external_stylesheets?
terminal.measure "#{COG} Writing external stylesheets..." do
File.write Path[DIST_DIR, "external-stylesheets.css"],
SourceFiles.external_stylesheets
end
end
end

Expand Down Expand Up @@ -69,12 +79,14 @@ module Mint

terminal.measure "#{COG} Writing index.html..." do
File.write Path[DIST_DIR, "index.html"],
IndexHtml.render(:build, relative, skip_service_worker, skip_icons)
IndexHtml.render(:build, relative, skip_manifest, skip_service_worker, skip_icons, inline, index_js)
end

terminal.measure "#{COG} Writing manifest.webmanifest..." do
File.write Path[DIST_DIR, "manifest.webmanifest"],
manifest(json, skip_icons)
unless skip_manifest
terminal.measure "#{COG} Writing manifest.webmanifest..." do
File.write Path[DIST_DIR, "manifest.webmanifest"],
manifest(json, skip_icons)
end
end

unless skip_icons
Expand Down Expand Up @@ -173,6 +185,25 @@ module Mint
{runtime + compiled.to_s, type_checker.artifacts}
end

def watch_workspace(relative, skip_manifest, skip_service_worker, skip_icons, optimize, inline, runtime_path)
workspace = Workspace.current

workspace.on "change" do |result|
case result
when Ast
terminal.reset
terminal.puts "Rebuilding for production"
terminal.divider
build(relative, skip_manifest, skip_service_worker, skip_icons, optimize, inline, runtime_path)
terminal.divider
when Error
end
end

workspace.update_cache
workspace.watch
end

def get_service_worker_utils
Assets.read("sw-utils.js")
end
Expand Down
23 changes: 22 additions & 1 deletion src/commands/build.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module Mint
default: false,
short: "r"

define_flag skip_manifest : Bool,
description: "If specified the web manifest will not be generated",
default: false

define_flag skip_service_worker : Bool,
description: "If specified the service worker functionality will be disabled",
default: false
Expand All @@ -23,19 +27,36 @@ module Mint
default: true,
short: "m"

define_flag inline : Bool,
description: "If specified the JavaScript and CSS will be inlined with the html",
default: false,
short: "i"

define_flag runtime : String,
description: "Will use supplied runtime path instead of the default distribution",
required: false

define_flag watch : Bool,
description: "Enables watch mode for build",
default: false,
short: "w"

def run
execute "Building for production" do
Builder.new(
flags.relative,
flags.skip_manifest,
flags.skip_service_worker,
flags.skip_icons,
flags.minify,
flags.runtime
flags.inline,
flags.runtime,
flags.watch
)

if flags.watch
sleep
end
end
end
end
Expand Down
9 changes: 6 additions & 3 deletions src/utils/index_html.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ module Mint
# Renders the main HTML file based on a mint.json file and an environment.
class IndexHtml
@relative : Bool
@no_manifest : Bool
@no_service_worker : Bool
@no_icons : Bool
@inline : Bool
@index_js : String
@live_reload : Bool

getter env : Environment
getter json : MintJson

def self.render(env : Environment, relative = false, no_service_worker = false, no_icons = false, live_reload = true) : String
new(env, relative, no_service_worker, no_icons, live_reload).to_s
def self.render(env : Environment, relative = false, no_manifest = false, no_service_worker = false, no_icons = false, inline = false, index_js = "", live_reload = true) : String
new(env, relative, no_manifest, no_service_worker, no_icons, inline, index_js, live_reload).to_s
end

def initialize(@env : Environment, @relative, @no_service_worker, @no_icons, @live_reload)
def initialize(@env : Environment, @relative, @no_manifest, @no_service_worker, @no_icons, @inline, @index_js, @live_reload)
@json = MintJson.parse_current
end

Expand Down
29 changes: 25 additions & 4 deletions src/utils/index_html.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<head>
<meta charset="<%= application.meta["charset"]? || "utf-8" %>">
<title><%= application.title %></title>
<link rel="manifest" href="<%= path_for("manifest.webmanifest") %>">
<% unless @no_manifest %>
<link rel="manifest" href="<%= path_for("manifest.webmanifest") %>">
<% end %>
<% application.meta.each do |name, content| %>
<% next if name == "charset" %>
<meta name="<%= name %>" content="<%= content %>">
Expand All @@ -21,7 +23,13 @@
<% end %>

<% if SourceFiles.external_stylesheets? %>
<link rel="stylesheet" href="<%= path_for("external-stylesheets.css") %>">
<% if @inline %>
<style>
<%= SourceFiles.external_stylesheets %>
</style>
<% else %>
<link rel="stylesheet" href="<%= path_for("external-stylesheets.css") %>">
<% end %>
<% end %>
</head>
<body>
Expand All @@ -45,10 +53,23 @@
<% end %>

<% if SourceFiles.external_javascripts? %>
<script src="<%= path_for("external-javascripts.js") %>"></script>
<% if @inline %>
<script>
<%= SourceFiles.external_javascripts %>
</style>
<% else %>
<script src="<%= path_for("external-javascripts.js") %>"></script>
<% end %>
<% end %>

<% if @inline %>
<script>
<%= @index_js %>
</script>
<% else %>
<script src="<%= path_for("index.js") %>"></script>
<% end %>

<script src="<%= path_for("index.js") %>"></script>
<noscript>
This application requires JavaScript.
</noscript>
Expand Down

0 comments on commit 00df3cc

Please sign in to comment.