From 85d0111f87363c441e980c34b9ab04debee6eb7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szikszai=20Guszt=C3=A1v?= Date: Sun, 24 Sep 2023 13:39:43 +0200 Subject: [PATCH 1/3] Improve Development Server - Don't type check twice - Only check the hot path not everything --- src/reactor.cr | 117 +++++++-------------------------- src/type_checker.cr | 3 +- src/type_checkers/top_level.cr | 12 ++-- src/workspace.cr | 17 ++++- 4 files changed, 49 insertions(+), 100 deletions(-) diff --git a/src/reactor.cr b/src/reactor.cr index 1d722bd48..a861b7272 100644 --- a/src/reactor.cr +++ b/src/reactor.cr @@ -18,7 +18,6 @@ module Mint @sockets = [] of HTTP::WebSocket - getter ast : Ast = Ast.new getter script : String? def self.start(host : String, port : Int32, auto_format : Bool, live_reload : Bool) @@ -30,87 +29,41 @@ module Mint workspace = Workspace.current workspace.format = auto_format - - init(workspace) + workspace.check_everything = false workspace.on "change" do |result| - update result - notify + case result + when Ast + # Compile. + @script = Compiler.compile workspace.type_checker.artifacts, { + css_prefix: workspace.json.application.css_prefix, + web_components: workspace.json.web_components, + relative: false, + optimize: false, + build: false, + } + + @artifacts = workspace.type_checker.artifacts + @error = nil + when Error + @error = result.to_html + @artifacts = nil + @script = nil + end + + # Notifies all connected sockets to reload the page. + @sockets.each(&.send("reload")) end + # Do the initial parsing and type checking. + workspace.update_cache workspace.watch - watch_for_changes setup_kemal Server.run "Development", @host, @port end - def init(workspace) - prefix = "#{COG} Parsing files" - line = "" - - elapsed = Time.measure do - workspace.initialize_cache do |_, index, size| - counter = - "#{index} / #{size}".colorize.mode(:bold) - - line = - "#{prefix}: #{counter}".ljust(line.size) - - terminal.io.print("#{line}\r") - terminal.io.flush - end - end - - elapsed = TimeFormat.auto(elapsed).colorize.mode(:bold) - terminal.io.puts "#{prefix}... #{elapsed}".ljust(line.size) - - @ast = workspace.ast - compile_script - rescue error : Error - @error = error.to_html - end - - def update(result) - case result - when Ast - @ast = result - @error = nil - compile_script - when Error - @error = result.to_html - end - end - - def compile_script - # Fetch options from the applications - json = - MintJson.parse_current - - # Create a brand new TypeChecker. - type_checker = - TypeChecker.new(ast, web_components: json.web_components.keys) - - # Type check. - type_checker.check - - # Compile. - @script = Compiler.compile type_checker.artifacts, { - css_prefix: json.application.css_prefix, - web_components: json.web_components, - relative: false, - optimize: false, - build: false, - } - @artifacts = type_checker.artifacts - @error = nil - rescue error : Error - @error = error.to_html - @artifacts = nil - @script = nil - end - def live_reload if @live_reload %() @@ -231,28 +184,6 @@ module Mint end end - # Notifies all connected sockets to reload the page. - def notify - @sockets.each(&.send("reload")) - end - - # Sets up watchers to detect changes - def watch_for_changes - Env.env.try do |file| - spawn do - Watcher.watch([file]) do - Env.load do - terminal.measure "#{COG} Environment variables changed, recompiling..." do - compile_script - end - - notify - end - end - end - end - end - def terminal Render::Terminal::STDOUT end diff --git a/src/type_checker.cr b/src/type_checker.cr index 77aba5910..33e719c3d 100644 --- a/src/type_checker.cr +++ b/src/type_checker.cr @@ -50,6 +50,7 @@ module Mint ] of Checkable getter records, artifacts, formatter, web_components + getter? check_everything property? checking = true @@ -71,7 +72,7 @@ module Mint @stack = [] of Ast::Node - def initialize(ast : Ast, @check_env = true, @web_components = [] of String) + def initialize(ast : Ast, @check_env = true, @web_components = [] of String, @check_everything = true) ast.normalize @languages = ast.unified_locales.map(&.language) diff --git a/src/type_checkers/top_level.cr b/src/type_checkers/top_level.cr index 222f13639..458d6d48e 100644 --- a/src/type_checkers/top_level.cr +++ b/src/type_checkers/top_level.cr @@ -50,12 +50,14 @@ module Mint # this will not be compiled. self.checking = false - check_all node.components - check_all node.unified_modules + if check_everything? + check_all node.components + check_all node.unified_modules - resolve node.providers - resolve node.stores - resolve node.type_definitions + resolve node.providers + resolve node.stores + resolve node.type_definitions + end VOID end diff --git a/src/workspace.cr b/src/workspace.cr index 8abfc567d..ac00f4a34 100644 --- a/src/workspace.cr +++ b/src/workspace.cr @@ -48,6 +48,7 @@ module Mint @event_handlers = {} of String => Array(ChangeProc) @cache = {} of String => Ast + @env_watcher : Watcher? @pattern = %w[] getter type_checker : TypeChecker @@ -57,6 +58,7 @@ module Mint getter error : Error? getter root : String + property? check_everything : Bool = true property? format : Bool = false def initialize(@root : String) @@ -80,6 +82,11 @@ module Mint @static_watcher = Watcher.new(all_static_pattern) + @env_watcher = + Env.env.try do |file| + Watcher.new([file]) + end + @type_checker = TypeChecker.new(Ast.new) end @@ -160,6 +167,14 @@ module Mint reset_cache end end + + spawn do + @env_watcher.try(&.watch do + Env.load do + update_cache + end + end) + end end def files @@ -239,7 +254,7 @@ module Mint end private def check! - @type_checker = Mint::TypeChecker.new(ast, check_env: false) + @type_checker = Mint::TypeChecker.new(ast, check_env: false, check_everything: check_everything?) @type_checker.check end From 81d06b248bb56c3998c95c213b1838702c5dc49a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szikszai=20Guszt=C3=A1v?= Date: Sun, 24 Sep 2023 13:43:40 +0200 Subject: [PATCH 2/3] Check env in dev server. --- src/reactor.cr | 1 + src/workspace.cr | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/reactor.cr b/src/reactor.cr index a861b7272..121bbed9b 100644 --- a/src/reactor.cr +++ b/src/reactor.cr @@ -29,6 +29,7 @@ module Mint workspace = Workspace.current workspace.format = auto_format + workspace.check_env = true workspace.check_everything = false workspace.on "change" do |result| diff --git a/src/workspace.cr b/src/workspace.cr index ac00f4a34..0736e7b3c 100644 --- a/src/workspace.cr +++ b/src/workspace.cr @@ -59,6 +59,7 @@ module Mint getter root : String property? check_everything : Bool = true + property? check_env : Bool = false property? format : Bool = false def initialize(@root : String) @@ -254,8 +255,12 @@ module Mint end private def check! - @type_checker = Mint::TypeChecker.new(ast, check_env: false, check_everything: check_everything?) - @type_checker.check + @type_checker = + Mint::TypeChecker.new( + check_everything: check_everything?, + check_env: check_env?, + ast: ast + ).tap(&.check) end private def call(event, arg) From 5005f3d99172175eaf2144ef979c04491f5acc3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szikszai=20Guszt=C3=A1v?= Date: Sun, 24 Sep 2023 14:14:21 +0200 Subject: [PATCH 3/3] Update src/workspace.cr Co-authored-by: Sijawusz Pur Rahnama --- src/workspace.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/workspace.cr b/src/workspace.cr index 0736e7b3c..548754ed7 100644 --- a/src/workspace.cr +++ b/src/workspace.cr @@ -170,11 +170,11 @@ module Mint end spawn do - @env_watcher.try(&.watch do + @env_watcher.try &.watch do Env.load do update_cache end - end) + end end end