Skip to content

Commit 88d7ed2

Browse files
Bartek Wronavogel76
authored andcommitted
Add test verifying EXPORT_ES6 output contains no require() calls
Bundlers (webpack, rollup, vite, esbuild) and frameworks (Next.js, Nuxt) cannot resolve CommonJS require() calls inside ES modules. This test statically verifies that EXPORT_ES6 output uses `await import()` instead of `require()` for Node.js built-in modules, and that the `createRequire` polyfill pattern is not present. Parameterized for default, node-only, and pthreads configurations to cover the various code paths that import Node.js built-ins (fs, path, url, util, worker_threads).
1 parent f2d29f0 commit 88d7ed2

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

test/test_other.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,31 @@ def test_esm_implies_modularize(self):
466466
def test_esm_requires_modularize(self):
467467
self.assert_fail([EMCC, test_file('hello_world.c'), '-sEXPORT_ES6', '-sMODULARIZE=0'], 'EXPORT_ES6 requires MODULARIZE to be set')
468468

469+
# Verify that EXPORT_ES6 output uses `await import()` instead of `require()`
470+
# for Node.js built-in modules. Using `require()` in ESM files breaks
471+
# bundlers (webpack, rollup, vite, esbuild) which cannot resolve CommonJS
472+
# require() calls inside ES modules.
473+
@crossplatform
474+
@parameterized({
475+
'default': ([],),
476+
'node': (['-sENVIRONMENT=node'],),
477+
'pthreads': (['-pthread', '-sPTHREAD_POOL_SIZE=1'],),
478+
})
479+
def test_esm_no_require(self, args):
480+
self.run_process([EMCC, '-o', 'hello_world.mjs',
481+
'--extern-post-js', test_file('modularize_post_js.js'),
482+
test_file('hello_world.c')] + args)
483+
src = read_file('hello_world.mjs')
484+
# EXPORT_ES6 output must not contain require() calls as these are
485+
# incompatible with ES modules and break bundlers.
486+
# The only acceptable require-like pattern is inside a string/comment.
487+
require_calls = re.findall(r'(?<![\w.])require\s*\(', src)
488+
self.assertEqual(require_calls, [],
489+
'EXPORT_ES6 output must not contain require() calls '
490+
'(breaks bundlers). Use await import() instead.')
491+
# Also verify createRequire is not used as a polyfill
492+
self.assertNotContained('createRequire', src)
493+
469494
def test_emcc_out_file(self):
470495
# Verify that "-ofile" works in addition to "-o" "file"
471496
self.run_process([EMCC, '-c', '-ofoo.o', test_file('hello_world.c')])

0 commit comments

Comments
 (0)