actionview-svelte-handler 0.5.7 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -1
- data/README.md +1 -1
- data/actionview-svelte-handler.gemspec +3 -0
- data/lib/actionview-svelte-handler.rb +1 -0
- data/lib/generators/svelte/install_generator.rb +2 -1
- data/lib/svelte/errors.rb +140 -138
- data/lib/svelte/handler.rb +45 -41
- data/lib/svelte/helpers.rb +1 -1
- data/lib/svelte/js/builder.js +78 -37
- data/lib/svelte/railtie.rb +3 -0
- data/lib/svelte/templates/assembler.js.erb +5 -3
- data/lib/svelte/templates/island.html.erb +7 -9
- data/lib/svelte/version.rb +1 -1
- data/lib/svelte.rb +9 -6
- data/lib/ts/builder.ts +72 -34
- data/lib/ts/types/builder.d.ts +4 -2
- data/package-lock.json +970 -114
- data/package.json +11 -3
- data/rbs_collection.lock.yaml +60 -28
- data/sig/lib/svelte/errors.rbs +35 -34
- data/sig/lib/svelte.rbs +1 -1
- data/tmp/.gitkeep +0 -0
- metadata +19 -4
- data/svelte-on-rails.png +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1fcb603925b52a51a8488d83da752370c24135602c464b51993f86b58fedf1af
|
4
|
+
data.tar.gz: 7f3e9f80901d4b567758e259ab37e5ed7a315459a3b1b867b9f2f847b1697b29
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4260e66a28e3006940465ef5158f3bdf9cc2559327ca14ca4f4ff7c75d405bf051abc7e95de2452e5083a6ef68d51375735735340039ea4fc54ce1f0a4eb6e8
|
7
|
+
data.tar.gz: 0b131aebc945768f2a7bfd0a4d16cddf80e4198a54d59dffa48a4da6b76573bfc5e439a332e5ee3c4189fc2f29bc2f5cde3e3045dc44330482244c0406a90f46
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
<img src="https://codeberg.org/reesericci/actionview-svelte-handler/raw/branch/main/
|
1
|
+
<img src="https://codeberg.org/reesericci/actionview-svelte-handler/raw/branch/main/docs/public/logo.png" width="100px">
|
2
2
|
|
3
3
|
# `actionview-svelte-handler`
|
4
4
|
|
@@ -2,6 +2,7 @@ require_relative "lib/svelte/version"
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
4
|
spec.name = "actionview-svelte-handler"
|
5
|
+
spec.autorequire = "svelte"
|
5
6
|
spec.version = Svelte::VERSION
|
6
7
|
spec.authors = ["reesericci"]
|
7
8
|
spec.email = ["me@reeseric.ci"]
|
@@ -18,4 +19,6 @@ Gem::Specification.new do |spec|
|
|
18
19
|
end
|
19
20
|
|
20
21
|
spec.add_runtime_dependency "rails", "~> 7.0"
|
22
|
+
|
23
|
+
spec.add_dependency "zeitwerk", "~> 2"
|
21
24
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "svelte"
|
@@ -9,7 +9,8 @@ module Svelte
|
|
9
9
|
insert_into_file "app/views/layouts/application.html.erb",
|
10
10
|
"\n <%= svelte_tags %>",
|
11
11
|
after: "<head>"
|
12
|
-
`npm
|
12
|
+
`npm link #{Gem::Specification.find_by_name("actionview-svelte-handler").gem_dir} --save`
|
13
|
+
`npm install`
|
13
14
|
end
|
14
15
|
end
|
15
16
|
end
|
data/lib/svelte/errors.rb
CHANGED
@@ -1,148 +1,150 @@
|
|
1
1
|
require "active_support/syntax_error_proxy"
|
2
2
|
require "action_view/template/error"
|
3
3
|
module Svelte
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
def message
|
16
|
-
to_s
|
17
|
-
end
|
18
|
-
|
19
|
-
def backtrace
|
20
|
-
if suggestion
|
21
|
-
["#{Rails.root.join location[:file]}:#{location[:line]}:#{message}, #{suggestion}"] + caller
|
22
|
-
else
|
23
|
-
["#{Rails.root.join location[:file]}:#{location[:line]}:#{message}"] + caller
|
4
|
+
module Errors
|
5
|
+
class CompilerError < ActiveSupport::SyntaxErrorProxy # steep:ignore UnknownConstant
|
6
|
+
attr_accessor :location, :suggestion, :template
|
7
|
+
|
8
|
+
def initialize(message, location)
|
9
|
+
@location = location
|
10
|
+
@sugggestion = @location.dig(:suggestion) || ""
|
11
|
+
# steep:ignore:start
|
12
|
+
super(message) # : self
|
13
|
+
# steep:ignore:end
|
24
14
|
end
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
nil
|
29
|
-
end
|
30
|
-
|
31
|
-
def bindings
|
32
|
-
[template.send(:binding)]
|
33
|
-
end
|
34
|
-
|
35
|
-
def backtrace_locations
|
36
|
-
traces = backtrace.map { |trace|
|
37
|
-
file, line = trace.match(/^(.+?):(\d+).*$/, &:captures) || trace
|
38
|
-
ActiveSupport::SyntaxErrorProxy::BacktraceLocation.new(file, line.to_i, trace) # steep:ignore UnknownConstant
|
39
|
-
}
|
40
|
-
|
41
|
-
traces.map { |loc| ActiveSupport::SyntaxErrorProxy::BacktraceLocationProxy.new(loc, self) } # steep:ignore UnknownConstant
|
42
|
-
end
|
43
|
-
|
44
|
-
def annotated_source_code
|
45
|
-
location[:lineText].split("\n").map.with_index(1) { |line, index|
|
46
|
-
indentation = " " * 4
|
47
|
-
"#{index}:#{indentation}#{line}"
|
48
|
-
}
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
class TemplateError < StandardError
|
53
|
-
attr_reader :cause, :template
|
54
|
-
|
55
|
-
SOURCE_CODE_RADIUS = 3
|
56
|
-
|
57
|
-
def initialize(template, error)
|
58
|
-
raise("Did not provide cause error") unless error
|
59
|
-
@cause = error
|
60
|
-
|
61
|
-
raise("Did not provide template") unless template
|
62
|
-
@template, @sub_templates = template, nil
|
63
|
-
|
64
|
-
raise("Cause error is nil for TemplateError") unless @cause
|
65
|
-
@cause.template = template
|
66
|
-
|
67
|
-
# steep:ignore:start
|
68
|
-
super(@cause.message) # : self
|
69
|
-
# steep:ignore:end
|
70
|
-
end
|
71
|
-
|
72
|
-
def message
|
73
|
-
@cause.message
|
74
|
-
end
|
75
|
-
|
76
|
-
def annotated_source_code
|
77
|
-
@cause.annotated_source_code
|
78
|
-
end
|
79
|
-
|
80
|
-
# Following is copypasta-ed from ActionView::Template::Error
|
81
|
-
def backtrace
|
82
|
-
@cause.backtrace
|
83
|
-
end
|
84
|
-
|
85
|
-
def backtrace_locations
|
86
|
-
@cause.backtrace_locations
|
87
|
-
end
|
88
|
-
|
89
|
-
def file_name
|
90
|
-
@template.identifier
|
91
|
-
end
|
92
|
-
|
93
|
-
def sub_template_message
|
94
|
-
if @sub_templates
|
95
|
-
"Trace of template inclusion: " +
|
96
|
-
@sub_templates.collect(&:inspect).join(", ")
|
97
|
-
else
|
98
|
-
""
|
15
|
+
|
16
|
+
def message
|
17
|
+
to_s
|
99
18
|
end
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
source_code = @template.encode!.split("\n")
|
107
|
-
|
108
|
-
start_on_line = [num - SOURCE_CODE_RADIUS - 1, 0].max
|
109
|
-
end_on_line = [num + SOURCE_CODE_RADIUS - 1, source_code.length].min
|
110
|
-
|
111
|
-
indent = end_on_line.to_s.size + indentation
|
112
|
-
return [] unless (source_code = source_code[start_on_line..end_on_line])
|
113
|
-
|
114
|
-
formatted_code_for(source_code, start_on_line, indent)
|
115
|
-
end
|
116
|
-
|
117
|
-
def sub_template_of(template_path)
|
118
|
-
@sub_templates ||= []
|
119
|
-
@sub_templates << template_path
|
120
|
-
end
|
121
|
-
|
122
|
-
def line_number
|
123
|
-
@line_number ||=
|
124
|
-
if file_name
|
125
|
-
regexp = /#{Regexp.escape File.basename(file_name)}:(\d+)/
|
126
|
-
$1 if message =~ regexp || backtrace.find { |line| line =~ regexp }
|
19
|
+
|
20
|
+
def backtrace
|
21
|
+
if suggestion
|
22
|
+
["#{Rails.root.join location[:file]}:#{location[:line]}:#{message}, #{suggestion}"] + caller
|
23
|
+
else
|
24
|
+
["#{Rails.root.join location[:file]}:#{location[:line]}:#{message}"] + caller
|
127
25
|
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def cause
|
29
|
+
nil
|
30
|
+
end
|
31
|
+
|
32
|
+
def bindings
|
33
|
+
[template.send(:binding)]
|
34
|
+
end
|
35
|
+
|
36
|
+
def backtrace_locations
|
37
|
+
traces = backtrace.map { |trace|
|
38
|
+
file, line = trace.match(/^(.+?):(\d+).*$/, &:captures) || trace
|
39
|
+
ActiveSupport::SyntaxErrorProxy::BacktraceLocation.new(file, line.to_i, trace) # steep:ignore UnknownConstant
|
40
|
+
}
|
41
|
+
|
42
|
+
traces.map { |loc| ActiveSupport::SyntaxErrorProxy::BacktraceLocationProxy.new(loc, self) } # steep:ignore UnknownConstant
|
43
|
+
end
|
44
|
+
|
45
|
+
def annotated_source_code
|
46
|
+
location[:lineText].split("\n").map.with_index(1) { |line, index|
|
47
|
+
indentation = " " * 4
|
48
|
+
"#{index}:#{indentation}#{line}"
|
49
|
+
}
|
50
|
+
end
|
128
51
|
end
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
"
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
52
|
+
|
53
|
+
class TemplateError < StandardError
|
54
|
+
attr_reader :cause, :template
|
55
|
+
|
56
|
+
SOURCE_CODE_RADIUS = 3
|
57
|
+
|
58
|
+
def initialize(template, error)
|
59
|
+
raise("Did not provide cause error") unless error
|
60
|
+
@cause = error
|
61
|
+
|
62
|
+
raise("Did not provide template") unless template
|
63
|
+
@template, @sub_templates = template, nil
|
64
|
+
|
65
|
+
raise("Cause error is nil for TemplateError") unless @cause
|
66
|
+
@cause.template = template
|
67
|
+
|
68
|
+
# steep:ignore:start
|
69
|
+
super(@cause.message) # : self
|
70
|
+
# steep:ignore:end
|
71
|
+
end
|
72
|
+
|
73
|
+
def message
|
74
|
+
@cause.message
|
75
|
+
end
|
76
|
+
|
77
|
+
def annotated_source_code
|
78
|
+
@cause.annotated_source_code
|
79
|
+
end
|
80
|
+
|
81
|
+
# Following is copypasta-ed from ActionView::Template::Error
|
82
|
+
def backtrace
|
83
|
+
@cause.backtrace
|
84
|
+
end
|
85
|
+
|
86
|
+
def backtrace_locations
|
87
|
+
@cause.backtrace_locations
|
88
|
+
end
|
89
|
+
|
90
|
+
def file_name
|
91
|
+
@template.identifier
|
92
|
+
end
|
93
|
+
|
94
|
+
def sub_template_message
|
95
|
+
if @sub_templates
|
96
|
+
"Trace of template inclusion: " +
|
97
|
+
@sub_templates.collect(&:inspect).join(", ")
|
98
|
+
else
|
99
|
+
""
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def source_extract(indentation = 0)
|
104
|
+
return [] unless (num = line_number)
|
105
|
+
num = num.to_i
|
106
|
+
|
107
|
+
source_code = @template.encode!.split("\n")
|
108
|
+
|
109
|
+
start_on_line = [num - SOURCE_CODE_RADIUS - 1, 0].max
|
110
|
+
end_on_line = [num + SOURCE_CODE_RADIUS - 1, source_code.length].min
|
111
|
+
|
112
|
+
indent = end_on_line.to_s.size + indentation
|
113
|
+
return [] unless (source_code = source_code[start_on_line..end_on_line])
|
114
|
+
|
115
|
+
formatted_code_for(source_code, start_on_line, indent)
|
116
|
+
end
|
117
|
+
|
118
|
+
def sub_template_of(template_path)
|
119
|
+
@sub_templates ||= []
|
120
|
+
@sub_templates << template_path
|
121
|
+
end
|
122
|
+
|
123
|
+
def line_number
|
124
|
+
@line_number ||=
|
125
|
+
if file_name
|
126
|
+
regexp = /#{Regexp.escape File.basename(file_name)}:(\d+)/
|
127
|
+
$1 if message =~ regexp || backtrace.find { |line| line =~ regexp }
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
private
|
132
|
+
|
133
|
+
def source_location
|
134
|
+
if line_number
|
135
|
+
"on line ##{line_number} of "
|
136
|
+
else
|
137
|
+
"in "
|
138
|
+
end + file_name
|
139
|
+
end
|
140
|
+
|
141
|
+
def formatted_code_for(source_code, line_counter, indent)
|
142
|
+
raise("line counter is nil") if line_counter.nil?
|
143
|
+
indent_template = "%#{indent}s: %s"
|
144
|
+
source_code.map do |line|
|
145
|
+
line_counter += 1
|
146
|
+
indent_template % [line_counter, line]
|
147
|
+
end
|
146
148
|
end
|
147
149
|
end
|
148
150
|
end
|
data/lib/svelte/handler.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "erb"
|
2
2
|
require "svelte/helpers"
|
3
|
+
require "ostruct"
|
3
4
|
|
4
5
|
module Svelte
|
5
6
|
DISCARD_PROPS = %w[lookup_context view_renderer current_template output_buffer view_flow rendered_format marked_for_same_origin_verification virtual_path]
|
@@ -13,50 +14,53 @@ module Svelte
|
|
13
14
|
digest = Digest::SHA256.base64digest(source)
|
14
15
|
|
15
16
|
<<-RUBY
|
16
|
-
|
17
|
-
|
18
|
-
Svelte.props.merge!(
|
19
|
-
instance_values.reject { |k,v|
|
20
|
-
k.start_with?("_") || Svelte::DISCARD_PROPS.include?(k)
|
21
|
-
}
|
22
|
-
) { |key,old,new| old }
|
23
|
-
|
24
|
-
Svelte.props.deep_symbolize_keys!
|
25
|
-
|
26
|
-
if local_assigns.dig(:svelte) == nil
|
27
|
-
local_assigns[:svelte] = {}
|
28
|
-
end
|
29
|
-
|
30
|
-
assembler = nil
|
31
|
-
|
32
|
-
assembler = Tempfile.new(['assembler', ".mjs"], '#{Svelte.gem_dir}/tmp')
|
17
|
+
Dir.mktmpdir("svelte", Rails.root.join('tmp')) do |dir|
|
18
|
+
template = OpenStruct.new(JSON.parse('#{template.to_json}').deep_symbolize_keys)
|
33
19
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
20
|
+
Svelte.props.merge!(
|
21
|
+
instance_values.reject { |k,v|
|
22
|
+
k.start_with?("_") || Svelte::DISCARD_PROPS.include?(k)
|
23
|
+
}
|
24
|
+
) { |key,old,new| old }
|
25
|
+
|
26
|
+
Svelte.props.deep_symbolize_keys!
|
27
|
+
|
28
|
+
if local_assigns.dig(:svelte) == nil
|
29
|
+
local_assigns[:svelte] = {}
|
30
|
+
end
|
31
|
+
|
32
|
+
assembler = nil
|
43
33
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
34
|
+
assembler = Tempfile.new(['assembler', ".mjs"], dir)
|
35
|
+
|
36
|
+
assembler.write(ERB.new(File.read('#{Svelte.gem_dir}/lib/svelte/templates/assembler.js.erb')).result_with_hash({
|
37
|
+
path: '#{Rails.root.join template.identifier}',
|
38
|
+
locals: local_assigns,
|
39
|
+
compiled_client: j(Rails.cache.read('svelte/template/client' + '#{digest}')),
|
40
|
+
compiled_server: j(Rails.cache.read('svelte/template/server' + '#{digest}')),
|
41
|
+
ssr: Svelte.precedence(local_assigns.dig(:svelte, :ssr), template.variant.nil? ? nil : (template.variant == "server"), Svelte.ssr)
|
42
|
+
}))
|
43
|
+
|
44
|
+
assembler.rewind
|
45
|
+
|
46
|
+
result = JSON.parse(`NODE_PATH=#{Rails.root.join("node_modules")} NODE_NO_WARNINGS=1 node --experimental-vm-modules \#{assembler.path}`).deep_symbolize_keys
|
47
|
+
|
48
|
+
if result[:error]
|
49
|
+
e = Svelte::Errors::CompilerError.new(result.dig(:error, :text), result.dig(:error, :location))
|
50
|
+
raise Svelte::Errors::TemplateError.new(template, e)
|
51
|
+
end
|
52
|
+
|
53
|
+
assembler&.close
|
54
|
+
assembler&.unlink
|
55
|
+
|
56
|
+
Rails.cache.write("svelte/template/client/" + '#{digest}', result[:compiled][:client], expires_in: 14.days)
|
57
|
+
Rails.cache.write("svelte/template/server/" + '#{digest}', result[:compiled][:server], expires_in: 14.days)
|
58
|
+
|
59
|
+
content_for(:head, (result.dig(:server, :head) || "").html_safe)
|
60
|
+
|
61
|
+
ERB.new(File.read("#{Svelte.gem_dir}/lib/svelte/templates/island.html.erb")).result_with_hash({ result:, locals: local_assigns, digest: "#{digest}", bind: binding })
|
62
|
+
|
49
63
|
end
|
50
|
-
|
51
|
-
assembler&.close
|
52
|
-
assembler&.unlink
|
53
|
-
|
54
|
-
Rails.cache.write("svelte/template/client/" + '#{digest}', result[:compiled][:client], expires_in: 14.days)
|
55
|
-
Rails.cache.write("svelte/template/server/" + '#{digest}', result[:compiled][:server], expires_in: 14.days)
|
56
|
-
|
57
|
-
content_for(:head, (result.dig(:server, :head) || "").html_safe)
|
58
|
-
|
59
|
-
ERB.new(File.read("#{Svelte.gem_dir}/lib/svelte/templates/island.html.erb")).result_with_hash({ result:, locals: local_assigns, digest: "#{digest}", bind: binding })
|
60
64
|
RUBY
|
61
65
|
end
|
62
66
|
|
data/lib/svelte/helpers.rb
CHANGED
@@ -9,7 +9,7 @@ module Svelte
|
|
9
9
|
window.props = readable(JSON.parse("#{j(Svelte.props.to_json)}"))
|
10
10
|
JS
|
11
11
|
|
12
|
-
content_for(:head) + "\n" + tag.script(js.html_safe, type: "module") # steep:ignore RequiredBlockMissing
|
12
|
+
((content_for(:head).presence || "") + "\n" + tag.script(js.html_safe, type: "module")).html_safe # steep:ignore RequiredBlockMissing
|
13
13
|
end
|
14
14
|
|
15
15
|
delegate :destructure_attrs, to: :class
|
data/lib/svelte/js/builder.js
CHANGED
@@ -6,10 +6,16 @@
|
|
6
6
|
|
7
7
|
import { readable } from "svelte/store";
|
8
8
|
import { importFromStringSync } from "module-from-string";
|
9
|
-
import * as esbuild from "esbuild";
|
10
|
-
import sveltePlugin from "esbuild-svelte";
|
11
9
|
import { sveltePreprocess } from "svelte-preprocess";
|
12
10
|
import * as recast from "recast";
|
11
|
+
import { rollup } from "rollup";
|
12
|
+
import svelte from "rollup-plugin-svelte";
|
13
|
+
import alias from "@rollup/plugin-alias";
|
14
|
+
import commonjs from "@rollup/plugin-commonjs";
|
15
|
+
import { nodeResolve } from "@rollup/plugin-node-resolve";
|
16
|
+
import virtual from "@rollup/plugin-virtual";
|
17
|
+
import swc from "@rollup/plugin-swc";
|
18
|
+
import path from "node:path";
|
13
19
|
class Builder {
|
14
20
|
path;
|
15
21
|
props;
|
@@ -18,9 +24,12 @@ class Builder {
|
|
18
24
|
ssr;
|
19
25
|
workingDir;
|
20
26
|
preprocess;
|
21
|
-
|
22
|
-
|
23
|
-
|
27
|
+
pathAliases;
|
28
|
+
root;
|
29
|
+
constructor(path2, root, props, locals, client, server, ssr, workingDir, preprocess, pathAliases) {
|
30
|
+
this.path = path2;
|
31
|
+
this.root = root;
|
32
|
+
this.props = readable(Object.assign(props, locals, props));
|
24
33
|
this.locals = locals;
|
25
34
|
this.compiled = {
|
26
35
|
client,
|
@@ -29,61 +38,93 @@ class Builder {
|
|
29
38
|
this.ssr = ssr;
|
30
39
|
this.workingDir = workingDir;
|
31
40
|
this.preprocess = preprocess;
|
41
|
+
this.pathAliases = pathAliases;
|
32
42
|
}
|
33
|
-
async bundle(generate
|
34
|
-
const bundle = await
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
metafile: true,
|
43
|
+
async bundle(generate) {
|
44
|
+
const bundle = (await (await rollup({
|
45
|
+
input: "entry",
|
46
|
+
output: {
|
47
|
+
format: "esm",
|
48
|
+
sourcemap: true
|
49
|
+
},
|
50
|
+
watch: {
|
51
|
+
skipWrite: true
|
52
|
+
},
|
44
53
|
plugins: [
|
45
|
-
// @ts-expect-error
|
46
|
-
|
54
|
+
// @ts-expect-error see https://github.com/rollup/plugins/issues/1662
|
55
|
+
virtual({
|
56
|
+
entry: `import App from '${this.path}'; export default App`
|
57
|
+
}),
|
58
|
+
// @ts-expect-error see https://github.com/rollup/plugins/issues/1662
|
59
|
+
svelte({
|
47
60
|
compilerOptions: {
|
48
61
|
generate,
|
49
62
|
css: "injected",
|
50
|
-
hydratable: true
|
51
|
-
sveltePath
|
63
|
+
hydratable: true
|
52
64
|
},
|
65
|
+
emitCss: false,
|
53
66
|
preprocess: sveltePreprocess(this.preprocess),
|
54
|
-
|
55
|
-
if (warning.code === "missing-declaration" && warning.message.includes("'props'"))
|
56
|
-
|
57
|
-
|
58
|
-
|
67
|
+
onwarn: (warning, handler) => {
|
68
|
+
if (warning.code === "missing-declaration" && warning.message.includes("'props'")) return;
|
69
|
+
throw new Error(warning.message);
|
70
|
+
}
|
71
|
+
}),
|
72
|
+
// @ts-expect-error see https://github.com/rollup/plugins/issues/1662
|
73
|
+
alias({
|
74
|
+
entries: Object.entries(this.pathAliases || {}).map((k, v) => {
|
75
|
+
return new Object({ find: k, replacement: v });
|
76
|
+
})
|
77
|
+
}),
|
78
|
+
// @ts-expect-error see https://github.com/rollup/plugins/issues/1662
|
79
|
+
commonjs(),
|
80
|
+
nodeResolve({
|
81
|
+
browser: true,
|
82
|
+
exportConditions: ["svelte"],
|
83
|
+
extensions: [".svelte"]
|
84
|
+
}),
|
85
|
+
// @ts-expect-error see https://github.com/rollup/plugins/issues/1662
|
86
|
+
swc({
|
87
|
+
swc: {
|
88
|
+
jsc: {
|
89
|
+
target: "es6",
|
90
|
+
minify: {
|
91
|
+
format: {
|
92
|
+
comments: "all"
|
93
|
+
}
|
94
|
+
}
|
95
|
+
},
|
96
|
+
minify: true,
|
97
|
+
sourceMaps: true,
|
98
|
+
inlineSourcesContent: true
|
59
99
|
}
|
60
100
|
})
|
61
101
|
]
|
102
|
+
})).generate({ format: "esm", sourcemap: true })).output;
|
103
|
+
bundle[0].map.sources = bundle[0].map.sources.map((el) => {
|
104
|
+
return path.relative(this.path, this.root) + "/" + path.relative(this.root, el);
|
62
105
|
});
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
}
|
67
|
-
return bundle.outputFiles[0].text;
|
106
|
+
return `//# sourceMappingURL=${bundle[0].map.toUrl()}
|
107
|
+
//# sourceURL=${path.relative(this.path, this.root) + "/" + path.relative(this.root, this.path)}
|
108
|
+
${bundle[0].code}`.trim();
|
68
109
|
}
|
69
110
|
async client() {
|
70
|
-
return this.compiled?.client || this.standardizeClient(await this.bundle("dom"
|
111
|
+
return this.compiled?.client || this.standardizeClient(await this.bundle("dom"));
|
71
112
|
}
|
72
113
|
standardizeClient(code) {
|
73
114
|
const ast = recast.parse(code);
|
74
115
|
let name;
|
75
116
|
recast.visit(ast, {
|
76
|
-
visitExportNamedDeclaration: (
|
77
|
-
const stagingName =
|
117
|
+
visitExportNamedDeclaration: (path2) => {
|
118
|
+
const stagingName = path2.node?.specifiers?.[0].local?.name;
|
78
119
|
name = typeof stagingName !== "string" ? "" : stagingName;
|
79
|
-
|
120
|
+
path2.prune();
|
80
121
|
return false;
|
81
122
|
}
|
82
123
|
});
|
83
124
|
recast.visit(ast, {
|
84
|
-
visitIdentifier: (
|
85
|
-
if (
|
86
|
-
|
125
|
+
visitIdentifier: (path2) => {
|
126
|
+
if (path2.node.name === name) {
|
127
|
+
path2.node.name = "App";
|
87
128
|
}
|
88
129
|
return false;
|
89
130
|
}
|
data/lib/svelte/railtie.rb
CHANGED
@@ -4,6 +4,9 @@ require "svelte/helpers"
|
|
4
4
|
module Svelte
|
5
5
|
class Railtie < ::Rails::Railtie
|
6
6
|
initializer "svelte" do |app|
|
7
|
+
`npm link #{Gem::Specification.find_by_name("actionview-svelte-handler").gem_dir} --save`
|
8
|
+
`npm install --install-links`
|
9
|
+
|
7
10
|
ActiveSupport.on_load :action_view do
|
8
11
|
ActionView::Template.register_template_handler :svelte, Svelte::Handler
|
9
12
|
ActionView::Base.send :include, Svelte::Helpers
|
@@ -1,14 +1,16 @@
|
|
1
|
-
import Builder from "
|
1
|
+
import Builder from "actionview-svelte-handler";
|
2
2
|
|
3
3
|
const bob = new Builder(
|
4
|
-
'<%= path %>',
|
4
|
+
'<%= path %>',
|
5
|
+
'<%= Rails.root %>',
|
5
6
|
JSON.parse('<%= Svelte.props.to_json || "{}" %>'),
|
6
7
|
JSON.parse('<%= locals.to_json || "{}" %>'),
|
7
8
|
'<%= compiled_client || "" %>',
|
8
9
|
'<%= compiled_server || "" %>',
|
9
10
|
<%= ssr %>,
|
10
11
|
'<%= Rails.root %>',
|
11
|
-
JSON.parse('<%= Svelte.preprocess.to_json || "{}" %>')
|
12
|
+
JSON.parse('<%= Svelte.preprocess.to_json || "{}" %>'),
|
13
|
+
JSON.parse('<%= Svelte.aliases.to_json || "{}" %>')
|
12
14
|
)
|
13
15
|
|
14
16
|
const built = await bob.build()
|