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
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+ # 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+
5169 # Load the given feature from remote.
5270 def load ( relative_feature )
5371 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) {
115115
116116 expect ( await resolve ( ) ) . toBe ( "Hello from RecursiveRequire::B" ) ;
117117 } ) ;
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+ } ) ;
118137 } ) ;
119138}
You can’t perform that action at this time.
0 commit comments