Module: SvelteOnRails::Lib::WatchAssetChanges

Included in:
Configuration
Defined in:
lib/svelte_on_rails/lib/watch_asset_changes.rb

Instance Method Summary collapse

Instance Method Details

#build_statusObject



66
67
68
69
70
71
72
# File 'lib/svelte_on_rails/lib/watch_asset_changes.rb', line 66

def build_status
  @build_status ||= if File.exist?(build_status_file)
                      JSON.parse(File.read(build_status_file)) rescue {}
                    else
                      {}
                    end
end

#component_mtime_changed?(component_path, debug, component_basename) ⇒ Boolean

go recursively through the entrypoints subtree and check if any of them changed

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
85
# File 'lib/svelte_on_rails/lib/watch_asset_changes.rb', line 75

def component_mtime_changed?(component_path, debug, component_basename)
  @component_files ||= {}
  paths = @component_files[component_path] ||= fetch_source_files(component_path, debug, component_basename)
  paths.each do |file|
    mtime = File.mtime(file).to_f.round(3)
    if mtime > (@last_build || @manifest_mtime - 1.0)
      return true
    end
  end
  false
end

#fetch_source_files(component, debug, component_basename, files = []) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/svelte_on_rails/lib/watch_asset_changes.rb', line 125

def fetch_source_files(component, debug, component_basename, files = [])
  _manifest = manifest(debug, component_basename)[component]
  raise "ERROR: Could not find manifest entry for «#{component}»\n\nmanifest:\n+++\n#{manifest}\n+++" unless _manifest

  if _manifest['src']
    add_file(files, rails_root.join(_manifest['src']).to_s)
    add_files(files, chunk_files(component, debug, component_basename))
  elsif _manifest['file']
    add_files(files, chunk_files(component, debug, component_basename))
  end

  _manifest['imports'].to_a.each do |import|
    files = fetch_source_files(import, debug, component_basename, files)
  end

  files
end

#fingerprint(component_path, debug, component_basename) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/svelte_on_rails/lib/watch_asset_changes.rb', line 87

def fingerprint(component_path, debug, component_basename)
  # We do not cache this as this function is only for development.
  # Therefore, it is fast enough.
  debug_log(debug, component_basename, "Building fingerprint") do
    build_fingerprint(component_path, debug, component_basename)
  end
end

#manifest(debug, component_basename) ⇒ Object



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
# File 'lib/svelte_on_rails/lib/watch_asset_changes.rb', line 95

def manifest(debug, component_basename)

  # ensure we have a current manifest loaded

  if !@manifest.present?
    msg = "Manifest loaded"
    if watch_changes? && !File.exist?(manifest_json_path) && !@last_build.present?
      vite_build
    end
  elsif watch_changes? && File.mtime(manifest_json_path).to_f.round(3) != @manifest_mtime
    msg = "Manifest reloaded"
    @manifest = {}
  else
    msg = "Just return manifest"
  end

  if !@manifest.present? && File.exist?(manifest_json_path)
    debug_log(
      debug,
      component_basename,
      msg
    ) do
      @manifest_mtime = File.mtime(manifest_json_path).to_f.round(3)
      @manifest = JSON.parse(File.read(manifest_json_path))
    end
  end

  @manifest ||= {}
end

#vite_buildObject



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
# File 'lib/svelte_on_rails/lib/watch_asset_changes.rb', line 5

def vite_build
  @last_build = Time.now.to_f.round(3)
  @manifest = {}
  @component_files = {}
  @dev_modules_map = {}
  utils = SvelteOnRails::Lib::Utils
  previous_mtime = utils.manifest_file_mtime

  stdout, stderr, status = Open3.capture3("npm run build:ssr", chdir: rails_root)

  passed = previous_mtime != utils.manifest_file_mtime

  if passed

    warnings_count = stderr.scan(/\[vite-plugin-svelte\]/).length

    @build_status = {
      'mtime' => utils.manifest_file_mtime,
      'warnings_count' => warnings_count,
      'passed' => true
    }

    if warnings_count >= 1
      utils.error_log(
        "VITE BUILD PASSED, #{warnings_count} WARNINGS",
        [
          "stderr:\n#{stderr}"
        ]
      )
    else
      puts "  [SOR] Vite build passed, no warnings"
    end
    $stdout.flush
  else

    @build_status = {
      'mtime' => utils.manifest_file_mtime,
      'warnings_count' => '???',
      'passed' => false
    }

    puts <<~TEXT
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      [SOR] ERROR: VITE BUILD FAILED
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      #{stderr}
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      END error vite build failed 
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    TEXT
  end
  $stdout.flush

  build_status_file.dirname.mkpath
  build_status_file.write(@build_status.to_json)

  {
    status: (passed ? "SUCCESS" : "failed"),
  }
end