Skip to content

Commit

Permalink
Revert removal of createRequire (emscripten-core#23265)
Browse files Browse the repository at this point in the history
This change effectively reverts emscripten-core#23169 because it turns out there are
quite a few other places in the codebase that depend on being able to
call `require`.
  • Loading branch information
sbc100 authored Dec 31, 2024
1 parent 09a662a commit e352c4e
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/closure-externs/closure-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
var EMSCRIPTEN$IMPORT$META;
var EMSCRIPTEN$AWAIT$IMPORT;

// Don't minify createRequire
var createRequire;

// Don't minify startWorker which we use to start workers once the runtime is ready.
/**
* @param {Object} Module
Expand Down
26 changes: 14 additions & 12 deletions src/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,16 @@ if (ENVIRONMENT_IS_PTHREAD) {

#if ENVIRONMENT_MAY_BE_NODE
if (ENVIRONMENT_IS_NODE) {
#if PTHREADS || WASM_WORKERS
#if EXPORT_ES6
var worker_threads = await import('worker_threads');
#else
var worker_threads = require('worker_threads');
// When building an ES module `require` is not normally available.
// We need to use `createRequire()` to construct the require()` function.
const { createRequire } = await import('module');
/** @suppress{duplicate} */
var require = createRequire('/');
#endif

#if PTHREADS || WASM_WORKERS
var worker_threads = require('worker_threads');
global.Worker = worker_threads.Worker;
ENVIRONMENT_IS_WORKER = !worker_threads.isMainThread;
#if PTHREADS
Expand Down Expand Up @@ -191,21 +195,19 @@ if (ENVIRONMENT_IS_NODE) {
}
#endif

// These modules will usually be used on Node.js. Load them eagerly to avoid
// the complexity of lazy-loading.
var fs = require('fs');
var nodePath = require('path');

#if EXPORT_ES6
var fs = await import('fs');
var nodePath = await import('path');
var url = await import('url');
// EXPORT_ES6 + ENVIRONMENT_IS_NODE always requires use of import.meta.url,
// since there's no way getting the current absolute path of the module when
// support for that is not available.
if (!import.meta.url.startsWith('data:')) {
scriptDirectory = nodePath.dirname(url.fileURLToPath(import.meta.url)) + '/';
scriptDirectory = nodePath.dirname(require('url').fileURLToPath(import.meta.url)) + '/';
}
#else
// These modules will usually be used on Node.js. Load them eagerly to avoid
// the complexity of lazy-loading.
var fs = require('fs');
var nodePath = require('path');
scriptDirectory = __dirname + '/';
#endif

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 @@
1509
1541
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 @@
3157
3210
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
52851
52853
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_no_asserts.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
28642
28644
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_strict.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
51634
51636

0 comments on commit e352c4e

Please sign in to comment.