File tree Expand file tree Collapse file tree 3 files changed +40
-0
lines changed
npm-packages/ruby-wasm-wasi/test-e2e/integrations Expand file tree Collapse file tree 3 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -43,11 +43,27 @@ class RequireRemote
4343 include Singleton
4444
4545 def initialize
46+ # By default, the base_url is the URL of the HTML file that invoked ruby.wasm vm.
4647 base_url = JS . global [ :URL ] . new ( JS . global [ :location ] [ :href ] )
4748 @resolver = URLResolver . new ( base_url )
4849 @evaluator = Evaluator . new
4950 end
5051
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+
5167 # Load the given feature from remote.
5268 def load ( relative_feature )
5369 location = @resolver . get_location ( relative_feature )
Original file line number Diff line number Diff line change @@ -26,6 +26,10 @@ def pop()
2626 @url_stack . pop
2727 end
2828
29+ def inspect
30+ "#{ self . class } (#{ @url_stack } )"
31+ end
32+
2933 private
3034
3135 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) {
115115
116116 expect ( await resolve ( ) ) . toBe ( "Hello from RecursiveRequire::B" ) ;
117117 } ) ;
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+ } ) ;
118138 } ) ;
119139}
You can’t perform that action at this time.
0 commit comments