Skip to content

Commit

Permalink
fix(node): regression where ts files were sometimes resolved instead …
Browse files Browse the repository at this point in the history
…of js (#26971)
  • Loading branch information
dsherret committed Nov 21, 2024
1 parent 1e03722 commit 77bcb97
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 21 deletions.
54 changes: 33 additions & 21 deletions ext/node/polyfills/01_require.js
Original file line number Diff line number Diff line change
Expand Up @@ -1060,22 +1060,39 @@ Module.prototype._compile = function (content, filename, format) {
return result;
};

Module._extensions[".js"] =
Module._extensions[".ts"] =
Module._extensions[".jsx"] =
Module._extensions[".tsx"] =
function (module, filename) {
const content = op_require_read_file(filename);
const format = op_require_is_maybe_cjs(filename) ? undefined : "module";
module._compile(content, filename, format);
};

Module._extensions[".cjs"] =
Module._extensions[".cts"] =
function (module, filename) {
const content = op_require_read_file(filename);
module._compile(content, filename, "commonjs");
};
Module._extensions[".js"] = function (module, filename) {
// We don't define everything on Module.extensions in
// order to prevent probing for these files
if (
StringPrototypeEndsWith(filename, ".js") ||
StringPrototypeEndsWith(filename, ".ts") ||
StringPrototypeEndsWith(filename, ".jsx") ||
StringPrototypeEndsWith(filename, ".tsx")
) {
return loadMaybeCjs(module, filename);
} else if (StringPrototypeEndsWith(filename, ".mts")) {
return loadESMFromCJS(module, filename);
} else if (StringPrototypeEndsWith(filename, ".cts")) {
return loadCjs(module, filename);
} else {
return loadMaybeCjs(module, filename);
}
};

Module._extensions[".cjs"] = loadCjs;
Module._extensions[".mjs"] = loadESMFromCJS;
Module._extensions[".wasm"] = loadESMFromCJS;

function loadMaybeCjs(module, filename) {
const content = op_require_read_file(filename);
const format = op_require_is_maybe_cjs(filename) ? undefined : "module";
module._compile(content, filename, format);
}

function loadCjs(module, filename) {
const content = op_require_read_file(filename);
module._compile(content, filename, "commonjs");
}

function loadESMFromCJS(module, filename, code) {
const namespace = op_import_sync(
Expand All @@ -1086,11 +1103,6 @@ function loadESMFromCJS(module, filename, code) {
module.exports = namespace;
}

Module._extensions[".mjs"] =
Module._extensions[".mts"] =
Module._extensions[".wasm"] =
loadESMFromCJS;

function stripBOM(content) {
if (StringPrototypeCharCodeAt(content, 0) === 0xfeff) {
content = StringPrototypeSlice(content, 1);
Expand Down
4 changes: 4 additions & 0 deletions tests/specs/npm/pkg_index_ts_and_js/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"args": "run index.js",
"output": "Hi\nWasm\nJSON\n"
}
1 change: 1 addition & 0 deletions tests/specs/npm/pkg_index_ts_and_js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "package";

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
5 changes: 5 additions & 0 deletions tests/specs/npm/pkg_index_ts_and_js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"package": "*"
}
}

0 comments on commit 77bcb97

Please sign in to comment.