From 928808163816c69ac53ee4d56d2762a7f4c7312f Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 21 Nov 2024 11:37:10 -0500 Subject: [PATCH] fix(node): regression where ts files were sometimes resolved instead of js (#26971) --- ext/node/polyfills/01_require.js | 52 +++++++++++------- .../npm/pkg_index_ts_and_js/__test__.jsonc | 4 ++ tests/specs/npm/pkg_index_ts_and_js/index.js | 1 + .../node_modules/package/index.js | 3 + .../node_modules/package/json/index.js | 1 + .../node_modules/package/json/index.json | 1 + .../node_modules/package/package.json | 3 + .../node_modules/package/subdir/index.js | 1 + .../node_modules/package/subdir/index.ts | 1 + .../node_modules/package/wasm/index.js | 1 + .../node_modules/package/wasm/index.wasm | Bin 0 -> 318 bytes .../npm/pkg_index_ts_and_js/package.json | 5 ++ 12 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 tests/specs/npm/pkg_index_ts_and_js/__test__.jsonc create mode 100644 tests/specs/npm/pkg_index_ts_and_js/index.js create mode 100644 tests/specs/npm/pkg_index_ts_and_js/node_modules/package/index.js create mode 100644 tests/specs/npm/pkg_index_ts_and_js/node_modules/package/json/index.js create mode 100644 tests/specs/npm/pkg_index_ts_and_js/node_modules/package/json/index.json create mode 100644 tests/specs/npm/pkg_index_ts_and_js/node_modules/package/package.json create mode 100644 tests/specs/npm/pkg_index_ts_and_js/node_modules/package/subdir/index.js create mode 100644 tests/specs/npm/pkg_index_ts_and_js/node_modules/package/subdir/index.ts create mode 100644 tests/specs/npm/pkg_index_ts_and_js/node_modules/package/wasm/index.js create mode 100644 tests/specs/npm/pkg_index_ts_and_js/node_modules/package/wasm/index.wasm create mode 100644 tests/specs/npm/pkg_index_ts_and_js/package.json diff --git a/ext/node/polyfills/01_require.js b/ext/node/polyfills/01_require.js index db032014c2..df73cad6b7 100644 --- a/ext/node/polyfills/01_require.js +++ b/ext/node/polyfills/01_require.js @@ -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[".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"] = - Module._extensions[".cts"] = - function (module, filename) { - const content = op_require_read_file(filename); - module._compile(content, filename, "commonjs"); - }; +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( @@ -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); diff --git a/tests/specs/npm/pkg_index_ts_and_js/__test__.jsonc b/tests/specs/npm/pkg_index_ts_and_js/__test__.jsonc new file mode 100644 index 0000000000..689f6e1f41 --- /dev/null +++ b/tests/specs/npm/pkg_index_ts_and_js/__test__.jsonc @@ -0,0 +1,4 @@ +{ + "args": "run index.js", + "output": "Hi\nWasm\nJSON\n" +} diff --git a/tests/specs/npm/pkg_index_ts_and_js/index.js b/tests/specs/npm/pkg_index_ts_and_js/index.js new file mode 100644 index 0000000000..c6ef1486e6 --- /dev/null +++ b/tests/specs/npm/pkg_index_ts_and_js/index.js @@ -0,0 +1 @@ +import "package"; diff --git a/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/index.js b/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/index.js new file mode 100644 index 0000000000..4c83afcf50 --- /dev/null +++ b/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/index.js @@ -0,0 +1,3 @@ +require("./subdir"); +require("./wasm"); +require("./json"); diff --git a/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/json/index.js b/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/json/index.js new file mode 100644 index 0000000000..e998adfbdc --- /dev/null +++ b/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/json/index.js @@ -0,0 +1 @@ +console.log("JSON"); \ No newline at end of file diff --git a/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/json/index.json b/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/json/index.json new file mode 100644 index 0000000000..0967ef424b --- /dev/null +++ b/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/json/index.json @@ -0,0 +1 @@ +{} diff --git a/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/package.json b/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/package.json new file mode 100644 index 0000000000..028ddbb95b --- /dev/null +++ b/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/package.json @@ -0,0 +1,3 @@ +{ + "name": "package" +} \ No newline at end of file diff --git a/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/subdir/index.js b/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/subdir/index.js new file mode 100644 index 0000000000..1b8b5ee92b --- /dev/null +++ b/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/subdir/index.js @@ -0,0 +1 @@ +console.log("Hi") \ No newline at end of file diff --git a/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/subdir/index.ts b/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/subdir/index.ts new file mode 100644 index 0000000000..e9cffd2de0 --- /dev/null +++ b/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/subdir/index.ts @@ -0,0 +1 @@ +console.log("No"); \ No newline at end of file diff --git a/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/wasm/index.js b/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/wasm/index.js new file mode 100644 index 0000000000..9174e5fd65 --- /dev/null +++ b/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/wasm/index.js @@ -0,0 +1 @@ +console.log("Wasm"); \ No newline at end of file diff --git a/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/wasm/index.wasm b/tests/specs/npm/pkg_index_ts_and_js/node_modules/package/wasm/index.wasm new file mode 100644 index 0000000000000000000000000000000000000000..6b3950fcc5297b78e0cdfddc5b91ac72505c7c94 GIT binary patch literal 318 zcmY+8%}#?r6ov1dfkMHg)tw6ymYPaKn^x-52XNyHj2F1lTK+OKM7yGo=A(%*F>&>s zoRfSxK&D0jKo4KVFyJ7i0B~IF5Yd$g^U1Xw@acU1f^dNU^d8(v&2_6!0wfg$fN%QD zYRWBOpj1JY6gP#$P^mprr!q1uMQ|xRh%kcpaO9YdA*4=2HA671$?lgQKey(;R{WEC zR|qqujHUdjy7tgm*6=*-bX)W}Ya{%MnzE;o(Rt~Og^O{@*%&V3?1Zi!yZmOg+$GB- zx=psLO}cyBrqMcCZ{j44*Q-0YpIOo+TlL0dd(B2HW_52Vt2i#JHlxZcd~+z9m_O|| D`Uz8u literal 0 HcmV?d00001 diff --git a/tests/specs/npm/pkg_index_ts_and_js/package.json b/tests/specs/npm/pkg_index_ts_and_js/package.json new file mode 100644 index 0000000000..f0b3ee21dd --- /dev/null +++ b/tests/specs/npm/pkg_index_ts_and_js/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "package": "*" + } +}