Skip to content
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
3 changes: 0 additions & 3 deletions src/closure-externs/closure-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
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
32 changes: 12 additions & 20 deletions src/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,12 @@ if (ENVIRONMENT_IS_PTHREAD) {

#if ENVIRONMENT_MAY_BE_NODE
if (ENVIRONMENT_IS_NODE) {
// `require()` is no-op in an ESM module, use `createRequire()` to construct
// the require()` function. This is only necessary for multi-environment
// builds, `-sENVIRONMENT=node` emits a static import declaration instead.
// TODO: Swap all `require()`'s with `import()`'s?
#if EXPORT_ES6 && ENVIRONMENT_MAY_BE_WEB
const { createRequire } = await import('module');
let dirname = import.meta.url;
if (dirname.startsWith("data:")) {
dirname = '/';
}
/** @suppress{duplicate} */
var require = createRequire(dirname);
#endif

#if PTHREADS || WASM_WORKERS
#if EXPORT_ES6
var worker_threads = await import('worker_threads');
#else
var worker_threads = require('worker_threads');
#endif
global.Worker = worker_threads.Worker;
ENVIRONMENT_IS_WORKER = !worker_threads.isMainThread;
#if PTHREADS
Expand Down Expand Up @@ -203,19 +193,21 @@ 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(require('url').fileURLToPath(import.meta.url)) + '/';
scriptDirectory = nodePath.dirname(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/test_unoptimized_code_size.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
54298
54011
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 @@
29718
29431
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 @@
53094
52807
1 change: 1 addition & 0 deletions third_party/closure-compiler/node-externs/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

/**
* @type {Object.<string,*>}
* @suppress {duplicate}
*/
var url = {};

Expand Down
28 changes: 5 additions & 23 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -2365,21 +2365,6 @@ def phase_binaryen(target, options, wasm_target):
write_file(final_js, js)


def node_es6_imports():
if not settings.EXPORT_ES6 or not settings.ENVIRONMENT_MAY_BE_NODE:
return ''

# Multi-environment builds uses `await import` in `shell.js`
if settings.ENVIRONMENT_MAY_BE_WEB:
return ''

# Use static import declaration if we only target Node.js
return '''
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
'''


def node_pthread_detection():
# Under node we detect that we are running in a pthread by checking the
# workerData property.
Expand All @@ -2394,11 +2379,10 @@ def modularize():
logger.debug(f'Modularizing, assigning to var {settings.EXPORT_NAME}')
src = read_file(final_js)

# Multi-environment ES6 builds require an async function
# When targetting node and ES6 we use `await import ..` in the generated code
# so the outer function needs to be marked as async.
async_emit = ''
if settings.EXPORT_ES6 and \
settings.ENVIRONMENT_MAY_BE_NODE and \
settings.ENVIRONMENT_MAY_BE_WEB:
if settings.EXPORT_ES6 and settings.ENVIRONMENT_MAY_BE_NODE:
async_emit = 'async '

# TODO: Remove when https://bugs.webkit.org/show_bug.cgi?id=223533 is resolved.
Expand Down Expand Up @@ -2449,25 +2433,23 @@ def modularize():
if settings.ENVIRONMENT_MAY_BE_NODE:
script_url_node = "if (typeof __filename != 'undefined') _scriptName = _scriptName || __filename;"
if settings.MODULARIZE == 'instance':
src = '''%(node_imports)s
src = '''\
var _scriptName = %(script_url)s;
%(script_url_node)s
%(src)s
''' % {
'node_imports': node_es6_imports(),
'script_url': script_url,
'script_url_node': script_url_node,
'src': src,
}
else:
src = '''%(node_imports)s
src = '''\
var %(EXPORT_NAME)s = (() => {
var _scriptName = %(script_url)s;
%(script_url_node)s
return (%(src)s);
})();
''' % {
'node_imports': node_es6_imports(),
'EXPORT_NAME': settings.EXPORT_NAME,
'script_url': script_url,
'script_url_node': script_url_node,
Expand Down
Loading