Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node): regression where ts files were sometimes resolved instead of js #26971

Merged
merged 4 commits into from
Nov 21, 2024
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
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": "*"
}
}