From f2bf40d157879cf05a9334d9a4676562bdf1b1c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 15 Nov 2022 13:58:04 +0100 Subject: [PATCH] fix(npm): probing for files that have a file stem (#16641) --- cli/node/mod.rs | 9 ++++----- .../registry/@denotest/cjs-with-file-stem/1.0.0/index.js | 5 +++++ .../@denotest/cjs-with-file-stem/1.0.0/other.service.js | 4 ++++ .../@denotest/cjs-with-file-stem/1.0.0/package.json | 4 ++++ .../registry/@denotest/cjs-with-file-stem/1.0.0/tslib.js | 3 +++ cli/tests/testdata/npm/translate_cjs_to_esm/main.js | 1 + cli/tests/testdata/npm/translate_cjs_to_esm/main.out | 2 ++ 7 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 cli/tests/testdata/npm/registry/@denotest/cjs-with-file-stem/1.0.0/index.js create mode 100644 cli/tests/testdata/npm/registry/@denotest/cjs-with-file-stem/1.0.0/other.service.js create mode 100644 cli/tests/testdata/npm/registry/@denotest/cjs-with-file-stem/1.0.0/package.json create mode 100644 cli/tests/testdata/npm/registry/@denotest/cjs-with-file-stem/1.0.0/tslib.js diff --git a/cli/node/mod.rs b/cli/node/mod.rs index 56f228d3f7..f2a645b5f6 100644 --- a/cli/node/mod.rs +++ b/cli/node/mod.rs @@ -1178,8 +1178,8 @@ fn file_extension_probe( ) -> Result { let p = p.clean(); if p.exists() { - let mut p_js = p.clone(); - p_js.set_extension("js"); + let file_name = p.file_name().unwrap(); + let p_js = p.with_file_name(format!("{}.js", file_name.to_str().unwrap())); if p_js.exists() && p_js.is_file() { return Ok(p_js); } else if p.is_dir() { @@ -1187,9 +1187,8 @@ fn file_extension_probe( } else { return Ok(p); } - } else { - let mut p_js = p.clone(); - p_js.set_extension("js"); + } else if let Some(file_name) = p.file_name() { + let p_js = p.with_file_name(format!("{}.js", file_name.to_str().unwrap())); if p_js.exists() && p_js.is_file() { return Ok(p_js); } diff --git a/cli/tests/testdata/npm/registry/@denotest/cjs-with-file-stem/1.0.0/index.js b/cli/tests/testdata/npm/registry/@denotest/cjs-with-file-stem/1.0.0/index.js new file mode 100644 index 0000000000..f7164182ad --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/cjs-with-file-stem/1.0.0/index.js @@ -0,0 +1,5 @@ +"use strict"; +console.log("hello"); +Object.defineProperty(exports, "__esModule", { value: true }); +const tslib_1 = require("./tslib.js"); +tslib_1.__exportStar(require("./other.service"), exports); diff --git a/cli/tests/testdata/npm/registry/@denotest/cjs-with-file-stem/1.0.0/other.service.js b/cli/tests/testdata/npm/registry/@denotest/cjs-with-file-stem/1.0.0/other.service.js new file mode 100644 index 0000000000..ba702a9af3 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/cjs-with-file-stem/1.0.0/other.service.js @@ -0,0 +1,4 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.foo = void 0; +console.log("world"); \ No newline at end of file diff --git a/cli/tests/testdata/npm/registry/@denotest/cjs-with-file-stem/1.0.0/package.json b/cli/tests/testdata/npm/registry/@denotest/cjs-with-file-stem/1.0.0/package.json new file mode 100644 index 0000000000..c72a8ae5a9 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/cjs-with-file-stem/1.0.0/package.json @@ -0,0 +1,4 @@ +{ + "name": "@denotest/cjs-with-file-stem", + "version": "1.0.0" +} diff --git a/cli/tests/testdata/npm/registry/@denotest/cjs-with-file-stem/1.0.0/tslib.js b/cli/tests/testdata/npm/registry/@denotest/cjs-with-file-stem/1.0.0/tslib.js new file mode 100644 index 0000000000..e5c2d6bc00 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/cjs-with-file-stem/1.0.0/tslib.js @@ -0,0 +1,3 @@ +module.exports = { + __exportStar: function(mod) {} +}; \ No newline at end of file diff --git a/cli/tests/testdata/npm/translate_cjs_to_esm/main.js b/cli/tests/testdata/npm/translate_cjs_to_esm/main.js index a7b5960ca4..04a86fbf17 100644 --- a/cli/tests/testdata/npm/translate_cjs_to_esm/main.js +++ b/cli/tests/testdata/npm/translate_cjs_to_esm/main.js @@ -1,5 +1,6 @@ import fsx from "npm:fs-extra@10.1.0"; import { createApp } from "npm:vue"; +import "npm:@denotest/cjs-with-file-stem"; console.log(fsx.access); console.log(createApp); diff --git a/cli/tests/testdata/npm/translate_cjs_to_esm/main.out b/cli/tests/testdata/npm/translate_cjs_to_esm/main.out index c472472f39..c21a965b88 100644 --- a/cli/tests/testdata/npm/translate_cjs_to_esm/main.out +++ b/cli/tests/testdata/npm/translate_cjs_to_esm/main.out @@ -1,2 +1,4 @@ +hello +world [Function: access] [Function: createApp]