Skip to content

Commit 2270aac

Browse files
author
Bartek Wrona
committed
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 6c822be commit 2270aac

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
@@ -465,6 +465,31 @@ def test_esm_implies_modularize(self):
465465
def test_esm_requires_modularize(self):
466466
self.assert_fail([EMCC, test_file('hello_world.c'), '-sEXPORT_ES6', '-sMODULARIZE=0'], 'EXPORT_ES6 requires MODULARIZE to be set')
467467

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

0 commit comments

Comments
 (0)