summaryrefslogtreecommitdiff
path: root/ext/node/polyfills
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2024-11-13 10:10:09 -0500
committerGitHub <noreply@github.com>2024-11-13 15:10:09 +0000
commitf091d1ad69b4e5217ae3272b641171781a372c4f (patch)
tree4ef4f90ec8a6b5c977efb187449f8c59c45de5e1 /ext/node/polyfills
parent6a4c6d83bacf5f03628a494778a30bce970f7cbc (diff)
feat(node): stabilize detecting if CJS via `"type": "commonjs"` in a package.json (#26439)
This will respect `"type": "commonjs"` in a package.json to determine if `.js`/`.jsx`/`.ts`/.tsx` files are CJS or ESM. If the file is found to be ESM it will be loaded as ESM though.
Diffstat (limited to 'ext/node/polyfills')
-rw-r--r--ext/node/polyfills/01_require.js31
-rw-r--r--ext/node/polyfills/process.ts2
2 files changed, 5 insertions, 28 deletions
diff --git a/ext/node/polyfills/01_require.js b/ext/node/polyfills/01_require.js
index 0d267ca44..083d4e49b 100644
--- a/ext/node/polyfills/01_require.js
+++ b/ext/node/polyfills/01_require.js
@@ -11,6 +11,7 @@ import {
op_require_can_parse_as_esm,
op_require_init_paths,
op_require_is_deno_dir_package,
+ op_require_is_maybe_cjs,
op_require_is_request_relative,
op_require_node_module_paths,
op_require_package_imports_resolve,
@@ -19,7 +20,6 @@ import {
op_require_path_is_absolute,
op_require_path_resolve,
op_require_proxy_path,
- op_require_read_closest_package_json,
op_require_read_file,
op_require_read_package_scope,
op_require_real_path,
@@ -1060,36 +1060,13 @@ Module.prototype._compile = function (content, filename, format) {
return result;
};
-Module._extensions[".js"] = function (module, filename) {
- const content = op_require_read_file(filename);
-
- let format;
- if (StringPrototypeEndsWith(filename, ".js")) {
- const pkg = op_require_read_closest_package_json(filename);
- if (pkg?.type === "module") {
- format = "module";
- } else if (pkg?.type === "commonjs") {
- format = "commonjs";
- }
- }
-
- module._compile(content, filename, format);
-};
-
-Module._extensions[".ts"] =
+Module._extensions[".js"] =
+ Module._extensions[".ts"] =
Module._extensions[".jsx"] =
Module._extensions[".tsx"] =
function (module, filename) {
const content = op_require_read_file(filename);
-
- let format;
- const pkg = op_require_read_closest_package_json(filename);
- if (pkg?.type === "module") {
- format = "module";
- } else if (pkg?.type === "commonjs") {
- format = "commonjs";
- }
-
+ const format = op_require_is_maybe_cjs(filename) ? undefined : "module";
module._compile(content, filename, format);
};
diff --git a/ext/node/polyfills/process.ts b/ext/node/polyfills/process.ts
index bf626e410..647376d5c 100644
--- a/ext/node/polyfills/process.ts
+++ b/ext/node/polyfills/process.ts
@@ -919,7 +919,7 @@ Object.defineProperty(argv, "1", {
if (Deno.mainModule?.startsWith("file:")) {
return pathFromURL(new URL(Deno.mainModule));
} else {
- return join(Deno.cwd(), "$deno$node.js");
+ return join(Deno.cwd(), "$deno$node.mjs");
}
},
});