Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix createRequire so it works for local node_modules. #23516

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ if (ENVIRONMENT_IS_NODE) {
// We need to use `createRequire()` to construct the require()` function.
const { createRequire } = await import('module');
/** @suppress{duplicate} */
var require = createRequire('/');
var require = createRequire(import.meta.url);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know enough about support for this - didn't we have an issue with it breaking in some bundler or environment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you are thinking of the use of import.meta.url of in new Worker. This usage is perfectly fine AFAIK. See #23169 which removed it prematurely.

#endif

#if PTHREADS || WASM_WORKERS
Expand Down
5 changes: 4 additions & 1 deletion test/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ def verify_es5(self, filename):
self.fail('es-check failed to verify ES5 output compliance')

# Build JavaScript code from source code
def build(self, filename, libraries=None, includes=None, force_c=False, output_suffix='.js', emcc_args=None, output_basename=None):
def build(self, filename, libraries=None, includes=None, force_c=False, emcc_args=None, output_basename=None, output_suffix=None):
if not os.path.exists(filename):
filename = test_file(filename)
compiler = [compiler_for(filename, force_c)]
Expand All @@ -1343,6 +1343,9 @@ def build(self, filename, libraries=None, includes=None, force_c=False, output_s
assert shared.suffix(filename) != '.c', 'force_c is not needed for source files ending in .c'
compiler.append('-xc')

if not output_suffix:
output_suffix = '.mjs' if emcc_args and '-sEXPORT_ES6' in emcc_args else '.js'

if output_basename:
output = output_basename + output_suffix
else:
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_esm.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1539
1540
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_esm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3209
3221
12 changes: 4 additions & 8 deletions test/test_sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ def __enter__(self):
npm_checked = True

# compile the server
proc = run_process([EMCC, '-Werror', test_file(self.filename), '-o', 'server.js', '-DSOCKK=%d' % self.listen_port] + self.args)
suffix = '.mjs' if '-sEXPORT_ES6' in self.args else '.js'
proc = run_process([EMCC, '-Werror', test_file(self.filename), '-o', 'server' + suffix, '-DSOCKK=%d' % self.listen_port] + self.args)
print('Socket server build: out:', proc.stdout or '', '/ err:', proc.stderr or '')

process = Popen(config.NODE_JS + ['server.js'])
process = Popen(config.NODE_JS + ['server' + suffix])
self.processes.append(process)
return self

Expand Down Expand Up @@ -166,11 +167,6 @@ def setUpClass(cls):
print('Running the socket tests. Make sure the browser allows popups from localhost.')
print()

# Use emscripten root for node module lookup. This is needed because the unit tests each
# run with CWD set to a temporary directory outside the emscripten tree.
print('Setting NODE_PATH=' + path_from_root('node_modules'))
os.environ['NODE_PATH'] = path_from_root('node_modules')

# Note: in the WebsockifyServerHarness and CompiledServerHarness tests below, explicitly use
# consecutive server listen ports, because server teardown might not occur deterministically
# (python dtor time) and is a bit racy.
Expand Down Expand Up @@ -283,7 +279,7 @@ def test_enet(self):
@crossplatform
@parameterized({
'native': [WebsockifyServerHarness, 59160, ['-DTEST_DGRAM=0']],
'tcp': [CompiledServerHarness, 59162, ['-DTEST_DGRAM=0']],
'tcp': [CompiledServerHarness, 59162, ['-DTEST_DGRAM=0', '-sEXPORT_ES6', '--extern-post-js', test_file('modularize_post_js.js')]],
'udp': [CompiledServerHarness, 59164, ['-DTEST_DGRAM=1']],
'pthread': [CompiledServerHarness, 59166, ['-pthread', '-sPROXY_TO_PTHREAD']],
})
Expand Down