Skip to content

Commit 27c3d08

Browse files
committed
Revert removal of createRequire
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`.
1 parent 09a662a commit 27c3d08

7 files changed

+22
-17
lines changed

src/closure-externs/closure-externs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
var EMSCRIPTEN$IMPORT$META;
1616
var EMSCRIPTEN$AWAIT$IMPORT;
1717

18+
// Don't minify createRequire
19+
var createRequire;
20+
1821
// Don't minify startWorker which we use to start workers once the runtime is ready.
1922
/**
2023
* @param {Object} Module

src/shell.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,16 @@ if (ENVIRONMENT_IS_PTHREAD) {
101101

102102
#if ENVIRONMENT_MAY_BE_NODE
103103
if (ENVIRONMENT_IS_NODE) {
104-
#if PTHREADS || WASM_WORKERS
105104
#if EXPORT_ES6
106-
var worker_threads = await import('worker_threads');
107-
#else
108-
var worker_threads = require('worker_threads');
105+
// When building an ES module `require` is not normally available.
106+
// We need to use `createRequire()` to construct the require()` function.
107+
const { createRequire } = await import('module');
108+
/** @suppress{duplicate} */
109+
var require = createRequire('/');
109110
#endif
111+
112+
#if PTHREADS || WASM_WORKERS
113+
var worker_threads = require('worker_threads');
110114
global.Worker = worker_threads.Worker;
111115
ENVIRONMENT_IS_WORKER = !worker_threads.isMainThread;
112116
#if PTHREADS
@@ -191,21 +195,19 @@ if (ENVIRONMENT_IS_NODE) {
191195
}
192196
#endif
193197

198+
// These modules will usually be used on Node.js. Load them eagerly to avoid
199+
// the complexity of lazy-loading.
200+
var fs = require('fs');
201+
var nodePath = require('path');
202+
194203
#if EXPORT_ES6
195-
var fs = await import('fs');
196-
var nodePath = await import('path');
197-
var url = await import('url');
198204
// EXPORT_ES6 + ENVIRONMENT_IS_NODE always requires use of import.meta.url,
199205
// since there's no way getting the current absolute path of the module when
200206
// support for that is not available.
201207
if (!import.meta.url.startsWith('data:')) {
202-
scriptDirectory = nodePath.dirname(url.fileURLToPath(import.meta.url)) + '/';
208+
scriptDirectory = nodePath.dirname(require('url').fileURLToPath(import.meta.url)) + '/';
203209
}
204210
#else
205-
// These modules will usually be used on Node.js. Load them eagerly to avoid
206-
// the complexity of lazy-loading.
207-
var fs = require('fs');
208-
var nodePath = require('path');
209211
scriptDirectory = __dirname + '/';
210212
#endif
211213

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1509
1+
1541
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3157
1+
3210
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
52851
1+
52853
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
28642
1+
28644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
51634
1+
51636

0 commit comments

Comments
 (0)