Skip to content

Commit d068717

Browse files
Bartek Wronavogel76
authored andcommitted
Add bundler integration tests verifying EXPORT_ES6 output has no require()
Add two tests that verify EXPORT_ES6 output is valid ESM and works with bundlers: - test_webpack_esm_output_clean: Compiles with EXPORT_ES6 and default environment (web+node), then builds with webpack. On main, webpack hard-fails because it cannot resolve 'node:module' (used by emscripten's createRequire polyfill). This breaks any webpack/Next.js/Nuxt project. - test_vite_esm_output_clean: Compiles with EXPORT_ES6 and default environment, then builds with vite. On main, vite externalizes 'node:module' for browser compatibility, emitting a warning. The resulting bundle contains code referencing unavailable node modules. These tests are expected to fail on main and pass after eliminating require() from EXPORT_ES6 output.
1 parent 92cf3c4 commit d068717

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

test/test_other.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15053,6 +15053,43 @@ def test_rollup(self):
1505315053
shutil.copy('hello.wasm', 'dist/')
1505415054
self.assertContained('hello, world!', self.run_js('dist/bundle.mjs'))
1505515055

15056+
@crossplatform
15057+
@requires_dev_dependency('webpack')
15058+
def test_webpack_esm_output_clean(self):
15059+
"""Verify webpack can build EXPORT_ES6 output without errors.
15060+
15061+
When emscripten generates require() in EXPORT_ES6 output (via
15062+
createRequire from 'node:module'), webpack fails with:
15063+
UnhandledSchemeError: Reading from "node:module" is not handled by plugins
15064+
This breaks any webpack/Next.js/Nuxt project using emscripten's ESM output.
15065+
"""
15066+
copytree(test_file('webpack_es6'), '.')
15067+
# Compile with default environment (web+node). On main, this generates
15068+
# 'import { createRequire } from "node:module"' and require() calls for
15069+
# node support, which webpack cannot resolve for web targets.
15070+
self.run_process([EMCC, test_file('hello_world.c'), '-sEXPORT_ES6', '-sEXIT_RUNTIME',
15071+
'-sMODULARIZE', '-o', 'src/hello.mjs'])
15072+
self.run_process(shared.get_npm_cmd('webpack') + ['--mode=development', '--no-devtool'])
15073+
15074+
@crossplatform
15075+
@requires_dev_dependency('vite')
15076+
def test_vite_esm_output_clean(self):
15077+
"""Verify vite bundles EXPORT_ES6 output without require() or externalizing.
15078+
15079+
When emscripten generates require() in EXPORT_ES6 output, vite externalizes
15080+
the node modules for browser compatibility, emitting a warning. The resulting
15081+
bundle contains code that references modules unavailable in browsers.
15082+
"""
15083+
copytree(test_file('vite'), '.')
15084+
# Compile with default environment (web+node). On main, this generates
15085+
# require() calls for node support which vite externalizes but leaves
15086+
# as require() in the bundle output.
15087+
self.run_process([EMCC, test_file('hello_world.c'), '-sEXPORT_ES6', '-sEXIT_RUNTIME',
15088+
'-sMODULARIZE', '-o', 'hello.mjs'])
15089+
# vite.config.js turns externalization warnings into errors, so vite
15090+
# will fail with non-zero exit code if require() appears in ESM output.
15091+
self.run_process(shared.get_npm_cmd('vite') + ['build'])
15092+
1505615093
def test_rlimit(self):
1505715094
self.do_other_test('test_rlimit.c', cflags=['-O1'])
1505815095

test/vite/vite.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
export default {
22
base: './',
3+
build: {
4+
rollupOptions: {
5+
onwarn(warning, defaultHandler) {
6+
// Treat externalized-for-browser-compatibility warnings as errors.
7+
// This catches require() in ESM output that vite silently externalizes.
8+
if (warning.message && warning.message.includes('externalized for browser compatibility')) {
9+
throw new Error(warning.message);
10+
}
11+
defaultHandler(warning);
12+
},
13+
},
14+
},
315
}

0 commit comments

Comments
 (0)