From d93bfe41bcb500ea04eb9b8fd2f3cab2ab513f40 Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Fri, 20 Mar 2020 15:10:39 -0500 Subject: [PATCH 1/4] esm: allow configuring default package type use ./configure --default-package-type --- configure.py | 11 +++++++++++ lib/internal/modules/esm/get_format.js | 4 +++- lib/internal/modules/run_main.js | 8 ++++++-- node.gyp | 4 ++++ src/node_config.cc | 16 ++++++++++++++++ 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/configure.py b/configure.py index e98801f2279f6b..eb60e3ab99ec63 100755 --- a/configure.py +++ b/configure.py @@ -641,6 +641,12 @@ default=False, help='node will load builtin modules from disk instead of from binary') +parser.add_option('--default-package-type', + action='store', + dest='node_default_package_type', + default=False, + help='node will treat files outside of package boundaries as this type') + # Create compile_commands.json in out/Debug and out/Release. parser.add_option('-C', action='store_true', @@ -1185,6 +1191,11 @@ def configure_node(o): print('Warning! Loading builtin modules from disk is for development') o['variables']['node_builtin_modules_path'] = options.node_builtin_modules_path + if options.node_default_package_type: + if not ( options.node_default_package_type in ['module', 'commonjs'] ): + raise Exception('--default-package-type must be either "module" or "commonjs"'); + o['variables']['node_default_package_type'] = options.node_default_package_type + def configure_napi(output): version = getnapibuildversion.get_napi_version() output['variables']['napi_build_version'] = version diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 616b2cf52309ea..951a12f5e83c6b 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -10,6 +10,7 @@ const experimentalWasmModules = getOptionValue('--experimental-wasm-modules'); const { getPackageType } = require('internal/modules/esm/resolve'); const { URL, fileURLToPath } = require('internal/url'); const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes; +const { defaultPackageType } = internalBinding('config'); const extensionFormatMap = { '__proto__': null, @@ -51,7 +52,8 @@ function defaultGetFormat(url, context, defaultGetFormatUnused) { const ext = extname(parsed.pathname); let format; if (ext === '.js') { - format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs'; + const type = getPackageType(parsed.href); + format = type !== 'none' ? type : defaultPackageType; } else { format = extensionFormatMap[ext]; } diff --git a/lib/internal/modules/run_main.js b/lib/internal/modules/run_main.js index 5f8b1f53d33768..f3b6cad32d0285 100644 --- a/lib/internal/modules/run_main.js +++ b/lib/internal/modules/run_main.js @@ -4,6 +4,7 @@ const CJSLoader = require('internal/modules/cjs/loader'); const { Module, toRealPath, readPackageScope } = CJSLoader; const { getOptionValue } = require('internal/options'); const path = require('path'); +const { defaultPackageType } = internalBinding('config'); function resolveMainPath(main) { // Note extension resolution for the main entry point can be deprecated in a @@ -33,8 +34,11 @@ function shouldUseESMLoader(mainPath) { return true; if (!mainPath || mainPath.endsWith('.cjs')) return false; - const pkg = readPackageScope(mainPath); - return pkg && pkg.data.type === 'module'; + const pkgType = readPackageScope(mainPath)?.data?.type ?? 'none'; + return pkgType === 'module' || ( + pkgType === 'none' && + defaultPackageType === 'module' + ); } function runMainESM(mainPath) { diff --git a/node.gyp b/node.gyp index c8e7387c2a8cf1..a56770971b300b 100644 --- a/node.gyp +++ b/node.gyp @@ -26,6 +26,7 @@ 'node_lib_target_name%': 'libnode', 'node_intermediate_lib_type%': 'static_library', 'node_builtin_modules_path%': '', + 'node_default_package_type%': '', 'library_files': [ 'lib/internal/bootstrap/environment.js', 'lib/internal/bootstrap/loaders.js', @@ -748,6 +749,9 @@ [ 'node_builtin_modules_path!=""', { 'defines': [ 'NODE_BUILTIN_MODULES_PATH="<(node_builtin_modules_path)"' ] }], + [ 'node_default_package_type!=""', { + 'defines': [ 'NODE_DEFAULT_PACKAGE_TYPE="<(node_default_package_type)"' ] + }], [ 'node_shared=="true"', { 'sources': [ 'src/node_snapshot_stub.cc', diff --git a/src/node_config.cc b/src/node_config.cc index 6ee3164a134fe8..8ec9bc0cefc008 100644 --- a/src/node_config.cc +++ b/src/node_config.cc @@ -13,6 +13,7 @@ using v8::Isolate; using v8::Local; using v8::Number; using v8::Object; +using v8::String; using v8::Value; // The config binding is used to provide an internal view of compile time @@ -85,6 +86,21 @@ static void Initialize(Local target, READONLY_TRUE_PROPERTY(target, "hasDtrace"); #endif +#if defined NODE_DEFAULT_PACKAGE_TYPE + READONLY_PROPERTY(target, + "defaultPackageType", + String::NewFromOneByte(isolate, + reinterpret_cast( + NODE_DEFAULT_PACKAGE_TYPE)) + .ToLocalChecked()); +#else + READONLY_PROPERTY(target, + "defaultPackageType", + String::NewFromOneByte(isolate, + reinterpret_cast("commonjs")) + .ToLocalChecked()); +#endif + READONLY_PROPERTY(target, "hasCachedBuiltins", v8::Boolean::New(isolate, native_module::has_code_cache)); } // InitConfig From 7430e1750c5879c9e923dfa3a6598f7f556dcff4 Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Fri, 20 Mar 2020 15:50:24 -0500 Subject: [PATCH 2/4] Update src/node_config.cc Co-Authored-By: Anna Henningsen --- src/node_config.cc | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/node_config.cc b/src/node_config.cc index 8ec9bc0cefc008..5b98e51b5c3823 100644 --- a/src/node_config.cc +++ b/src/node_config.cc @@ -89,10 +89,7 @@ static void Initialize(Local target, #if defined NODE_DEFAULT_PACKAGE_TYPE READONLY_PROPERTY(target, "defaultPackageType", - String::NewFromOneByte(isolate, - reinterpret_cast( - NODE_DEFAULT_PACKAGE_TYPE)) - .ToLocalChecked()); + FIXED_ONE_BYTE_STRING(isolate, NODE_DEFAULT_PACKAGE_TYPE)); #else READONLY_PROPERTY(target, "defaultPackageType", From 6687e9f9d8b5f098649e98ed2e428a0bf3da325c Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Fri, 20 Mar 2020 15:50:32 -0500 Subject: [PATCH 3/4] Update src/node_config.cc Co-Authored-By: Anna Henningsen --- src/node_config.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/node_config.cc b/src/node_config.cc index 5b98e51b5c3823..b5834d2f6aa8e4 100644 --- a/src/node_config.cc +++ b/src/node_config.cc @@ -93,9 +93,7 @@ static void Initialize(Local target, #else READONLY_PROPERTY(target, "defaultPackageType", - String::NewFromOneByte(isolate, - reinterpret_cast("commonjs")) - .ToLocalChecked()); + FIXED_ONE_BYTE_STRING(isolate, "commonjs")); #endif READONLY_PROPERTY(target, "hasCachedBuiltins", From a0b18403d2f1a3b64b8f60ea50c2d623214144f6 Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Fri, 20 Mar 2020 15:51:03 -0500 Subject: [PATCH 4/4] Update src/node_config.cc --- src/node_config.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/node_config.cc b/src/node_config.cc index b5834d2f6aa8e4..06e94f87a05d02 100644 --- a/src/node_config.cc +++ b/src/node_config.cc @@ -13,7 +13,6 @@ using v8::Isolate; using v8::Local; using v8::Number; using v8::Object; -using v8::String; using v8::Value; // The config binding is used to provide an internal view of compile time