File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
npm-packages/ruby-wasm-wasi/test-e2e/integrations Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -43,11 +43,29 @@ 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
+ # Initialize the instance.
53
+ #
54
+ # If you want to resolve relative paths to a starting point other than the HTML file that executes ruby.wasm,
55
+ # you can set the base_url property.
56
+ # For example, if you want to use the `lib` directory as the starting point, specify base_url as follows
57
+ #
58
+ # == Example
59
+ # require 'js/require_remote'
60
+ # JS::RequireRemote.instance.base_url = "lib"
61
+ # JS::RequireRemote.instance.load("foo") # => 'lib/foo.rb' will be loaded.
62
+ #
63
+ def base_url = ( base_url )
64
+ base_url = base_url . end_with? ( "/" ) ? base_url : "#{ base_url } /"
65
+ url = JS . global [ :URL ] . new ( base_url , JS . global [ :location ] [ :href ] )
66
+ @resolver = URLResolver . new ( url )
67
+ end
68
+
51
69
# Load the given feature from remote.
52
70
def load ( relative_feature )
53
71
location = @resolver . get_location ( relative_feature )
Original file line number Diff line number Diff line change @@ -115,5 +115,24 @@ 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 a file with a relative path from base_url option" , 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'
131
+ JS.global.checkResolved JS::RequireRemote.instance.load 'error_on_load_twice'
132
+ </script>
133
+ ` ) ;
134
+
135
+ expect ( await resolve ( ) ) . toBe ( true ) ;
136
+ } ) ;
118
137
} ) ;
119
138
}
You can’t perform that action at this time.
0 commit comments