File tree 3 files changed +40
-0
lines changed
npm-packages/ruby-wasm-wasi/test-e2e/integrations
3 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -43,11 +43,27 @@ class RequireRemote
43
43
include Singleton
44
44
45
45
def initialize
46
+ # By default, the base_url is the URL of the HTML file that invoked ruby.wasm vm.
46
47
base_url = JS . global [ :URL ] . new ( JS . global [ :location ] [ :href ] )
47
48
@resolver = URLResolver . new ( base_url )
48
49
@evaluator = Evaluator . new
49
50
end
50
51
52
+ # If you want to resolve relative paths to a starting point other than the HTML file that executes ruby.wasm,
53
+ # you can set the base_url property.
54
+ # For example, if you want to use the `lib` directory as the starting point, specify base_url as follows
55
+ #
56
+ # == Example
57
+ # require 'js/require_remote'
58
+ # JS::RequireRemote.instance.base_url = "lib"
59
+ # JS::RequireRemote.instance.load("foo") # => 'lib/foo.rb' will be loaded.
60
+ #
61
+ def base_url = ( base_url )
62
+ base_url = base_url . end_with? ( "/" ) ? base_url : "#{ base_url } /"
63
+ url = JS . global [ :URL ] . new ( base_url , JS . global [ :location ] [ :href ] )
64
+ @resolver = URLResolver . new ( url )
65
+ end
66
+
51
67
# Load the given feature from remote.
52
68
def load ( relative_feature )
53
69
location = @resolver . get_location ( relative_feature )
Original file line number Diff line number Diff line change @@ -26,6 +26,10 @@ def pop()
26
26
@url_stack . pop
27
27
end
28
28
29
+ def inspect
30
+ "#{ self . class } (#{ @url_stack } )"
31
+ end
32
+
29
33
private
30
34
31
35
def filename_from ( relative_feature )
Original file line number Diff line number Diff line change @@ -115,5 +115,25 @@ if (!process.env.RUBY_NPM_PACKAGE_ROOT) {
115
115
116
116
expect ( await resolve ( ) ) . toBe ( "Hello from RecursiveRequire::B" ) ;
117
117
} ) ;
118
+
119
+ test ( "JS::RequireRemote#load loads the file with a path relative to the base_url specified by the base_url property." , async ( {
120
+ page,
121
+ } ) => {
122
+ const resolve = await resolveBinding ( page , "checkResolved" ) ;
123
+ await page . goto (
124
+ "https://cdn.jsdelivr.net/npm/@ruby/head-wasm-wasi@latest/dist/" ,
125
+ ) ;
126
+ await page . setContent ( `
127
+ <script src="browser.script.iife.js"></script>
128
+ <script type="text/ruby" data-eval="async">
129
+ require 'js/require_remote'
130
+ JS::RequireRemote.instance.base_url = 'fixtures/recursive_require'
131
+ JS::RequireRemote.instance.load 'b'
132
+ JS.global.checkResolved RecursiveRequire::B.new.message
133
+ </script>
134
+ ` ) ;
135
+
136
+ expect ( await resolve ( ) ) . toBe ( "Hello from RecursiveRequire::B" ) ;
137
+ } ) ;
118
138
} ) ;
119
139
}
You can’t perform that action at this time.
0 commit comments