Skip to content

Commit 08e031c

Browse files
authored
Use new URL(..., import.meta.url) pattern in minimal runtime if EXPORT_ES6 is enabled (#26039)
This should make it possible to use `MINIMAL_RUNTIME_STREAMING_WASM_INSTANTIATION` with [bundlers like Vite](https://vite.dev/guide/assets) that recognize and transform `new URL('...', import.meta.url)`.
1 parent b3ba0ad commit 08e031c

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/postamble_minimal.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ var imports = {
182182
};
183183

184184
#if MINIMAL_RUNTIME_STREAMING_WASM_INSTANTIATION
185+
{{{
186+
#if EXPORT_ES6 && !ENVIRONMENT_MAY_BE_AUDIO_WORKLET
187+
const moduleUrl = `new URL('${TARGET_BASENAME}.wasm', import.meta.url)`;
188+
#elif !EXPORT_ES6 || AUDIO_WORKLET
189+
const moduleUrl = `'${TARGET_BASENAME}.wasm'`;
190+
#else
191+
const moduleUrl = `ENVIRONMENT_IS_AUDIO_WORKLET ? '${TARGET_BASENAME}.wasm' : new URL('${TARGET_BASENAME}.wasm', import.meta.url)`;
192+
#endif
193+
}}}
185194
// https://caniuse.com/#feat=wasm and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiateStreaming
186195
#if MIN_SAFARI_VERSION < 150000 || ENVIRONMENT_MAY_BE_NODE
187196
#if ASSERTIONS && !WASM2JS
@@ -197,13 +206,13 @@ instantiatePromise =
197206
// Node's fetch API cannot be used for local files, so we cannot use instantiateStreaming
198207
&& !ENVIRONMENT_IS_NODE
199208
#endif
200-
? WebAssembly.instantiateStreaming(fetch('{{{ TARGET_BASENAME }}}.wasm'), imports)
209+
? WebAssembly.instantiateStreaming(fetch({{{ moduleUrl }}}), imports)
201210
: WebAssembly.instantiate(Module['wasm'], imports)).then((output) => {
202211
#else
203212
#if AUDIO_WORKLET
204213
instantiatePromise =
205214
#endif
206-
WebAssembly.instantiateStreaming(fetch('{{{ TARGET_BASENAME }}}.wasm'), imports).then((output) => {
215+
WebAssembly.instantiateStreaming(fetch({{{ moduleUrl }}}), imports).then((output) => {
207216
#endif
208217

209218
#else // Non-streaming instantiation

test/test_browser.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5556,9 +5556,13 @@ def test_webpack(self, args):
55565556

55575557
@also_with_threads
55585558
@requires_dev_dependency('vite')
5559-
def test_vite(self):
5559+
@parameterized({
5560+
'': ([],),
5561+
'minimal': (['-sMINIMAL_RUNTIME', '-sMINIMAL_RUNTIME_STREAMING_WASM_INSTANTIATION'],),
5562+
})
5563+
def test_vite(self, args):
55605564
copytree(test_file('vite'), '.')
5561-
self.compile_btest('hello_world.c', ['-sEXIT_RUNTIME', '-sENVIRONMENT=web', '-o', 'hello.mjs'])
5565+
self.compile_btest('hello_world.c', ['-sEXIT_RUNTIME', '-sENVIRONMENT=web', '-o', 'hello.mjs'] + args)
55625566
self.run_process(shared.get_npm_cmd('vite') + ['build'])
55635567
self.run_browser('dist/index.html', '/report_result?exit:0')
55645568

0 commit comments

Comments
 (0)