Skip to content

Commit c2ba38b

Browse files
authored
Remove createRequire in favor of ES6 import (#23169)
1 parent a9108f8 commit c2ba38b

File tree

7 files changed

+21
-49
lines changed

7 files changed

+21
-49
lines changed

src/closure-externs/closure-externs.js

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

18-
// Don't minify createRequire
19-
var createRequire;
20-
2118
// Don't minify startWorker which we use to start workers once the runtime is ready.
2219
/**
2320
* @param {Object} Module

src/shell.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,12 @@ if (ENVIRONMENT_IS_PTHREAD) {
103103

104104
#if ENVIRONMENT_MAY_BE_NODE
105105
if (ENVIRONMENT_IS_NODE) {
106-
// `require()` is no-op in an ESM module, use `createRequire()` to construct
107-
// the require()` function. This is only necessary for multi-environment
108-
// builds, `-sENVIRONMENT=node` emits a static import declaration instead.
109-
// TODO: Swap all `require()`'s with `import()`'s?
110-
#if EXPORT_ES6 && ENVIRONMENT_MAY_BE_WEB
111-
const { createRequire } = await import('module');
112-
let dirname = import.meta.url;
113-
if (dirname.startsWith("data:")) {
114-
dirname = '/';
115-
}
116-
/** @suppress{duplicate} */
117-
var require = createRequire(dirname);
118-
#endif
119-
120106
#if PTHREADS || WASM_WORKERS
107+
#if EXPORT_ES6
108+
var worker_threads = await import('worker_threads');
109+
#else
121110
var worker_threads = require('worker_threads');
111+
#endif
122112
global.Worker = worker_threads.Worker;
123113
ENVIRONMENT_IS_WORKER = !worker_threads.isMainThread;
124114
#if PTHREADS
@@ -203,19 +193,21 @@ if (ENVIRONMENT_IS_NODE) {
203193
}
204194
#endif
205195

206-
// These modules will usually be used on Node.js. Load them eagerly to avoid
207-
// the complexity of lazy-loading.
208-
var fs = require('fs');
209-
var nodePath = require('path');
210-
211196
#if EXPORT_ES6
197+
var fs = await import('fs');
198+
var nodePath = await import('path');
199+
var url = await import('url');
212200
// EXPORT_ES6 + ENVIRONMENT_IS_NODE always requires use of import.meta.url,
213201
// since there's no way getting the current absolute path of the module when
214202
// support for that is not available.
215203
if (!import.meta.url.startsWith('data:')) {
216-
scriptDirectory = nodePath.dirname(require('url').fileURLToPath(import.meta.url)) + '/';
204+
scriptDirectory = nodePath.dirname(url.fileURLToPath(import.meta.url)) + '/';
217205
}
218206
#else
207+
// These modules will usually be used on Node.js. Load them eagerly to avoid
208+
// the complexity of lazy-loading.
209+
var fs = require('fs');
210+
var nodePath = require('path');
219211
scriptDirectory = __dirname + '/';
220212
#endif
221213

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
54298
1+
54011
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
29718
1+
29431
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
53094
1+
52807

third_party/closure-compiler/node-externs/url.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
/**
3232
* @type {Object.<string,*>}
33+
* @suppress {duplicate}
3334
*/
3435
var url = {};
3536

tools/link.py

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,21 +2365,6 @@ def phase_binaryen(target, options, wasm_target):
23652365
write_file(final_js, js)
23662366

23672367

2368-
def node_es6_imports():
2369-
if not settings.EXPORT_ES6 or not settings.ENVIRONMENT_MAY_BE_NODE:
2370-
return ''
2371-
2372-
# Multi-environment builds uses `await import` in `shell.js`
2373-
if settings.ENVIRONMENT_MAY_BE_WEB:
2374-
return ''
2375-
2376-
# Use static import declaration if we only target Node.js
2377-
return '''
2378-
import { createRequire } from 'module';
2379-
const require = createRequire(import.meta.url);
2380-
'''
2381-
2382-
23832368
def node_pthread_detection():
23842369
# Under node we detect that we are running in a pthread by checking the
23852370
# workerData property.
@@ -2394,11 +2379,10 @@ def modularize():
23942379
logger.debug(f'Modularizing, assigning to var {settings.EXPORT_NAME}')
23952380
src = read_file(final_js)
23962381

2397-
# Multi-environment ES6 builds require an async function
2382+
# When targetting node and ES6 we use `await import ..` in the generated code
2383+
# so the outer function needs to be marked as async.
23982384
async_emit = ''
2399-
if settings.EXPORT_ES6 and \
2400-
settings.ENVIRONMENT_MAY_BE_NODE and \
2401-
settings.ENVIRONMENT_MAY_BE_WEB:
2385+
if settings.EXPORT_ES6 and settings.ENVIRONMENT_MAY_BE_NODE:
24022386
async_emit = 'async '
24032387

24042388
# TODO: Remove when https://bugs.webkit.org/show_bug.cgi?id=223533 is resolved.
@@ -2449,25 +2433,23 @@ def modularize():
24492433
if settings.ENVIRONMENT_MAY_BE_NODE:
24502434
script_url_node = "if (typeof __filename != 'undefined') _scriptName = _scriptName || __filename;"
24512435
if settings.MODULARIZE == 'instance':
2452-
src = '''%(node_imports)s
2436+
src = '''\
24532437
var _scriptName = %(script_url)s;
24542438
%(script_url_node)s
24552439
%(src)s
24562440
''' % {
2457-
'node_imports': node_es6_imports(),
24582441
'script_url': script_url,
24592442
'script_url_node': script_url_node,
24602443
'src': src,
24612444
}
24622445
else:
2463-
src = '''%(node_imports)s
2446+
src = '''\
24642447
var %(EXPORT_NAME)s = (() => {
24652448
var _scriptName = %(script_url)s;
24662449
%(script_url_node)s
24672450
return (%(src)s);
24682451
})();
24692452
''' % {
2470-
'node_imports': node_es6_imports(),
24712453
'EXPORT_NAME': settings.EXPORT_NAME,
24722454
'script_url': script_url,
24732455
'script_url_node': script_url_node,

0 commit comments

Comments
 (0)