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(es/modules): Preserve extensions #6339

Merged
merged 8 commits into from Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/deno/paths/cjs-001/output/index.ts
Expand Up @@ -2,5 +2,5 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
require("./src/dep");
require("./src2/dep-2");
require("./src/dep.ts");
require("./src2/dep-2.ts");
@@ -1,2 +1,2 @@
import styles from "./foo.ts/index";
import styles from "./foo.ts/index.js";
console.log(styles);
Expand Up @@ -2,5 +2,5 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
const _a = require("../packages/a/src/index");
const _a = require("../packages/a/src/index.ts");
console.log(`${(0, _a.displayA)()}`);
Expand Up @@ -4,6 +4,6 @@ Object.defineProperty(exports, "__esModule", {
});
const _interopRequireWildcard = require("@swc/helpers/lib/_interop_require_wildcard.js").default;
(async function() {
const { displayA } = await Promise.resolve().then(()=>/*#__PURE__*/ _interopRequireWildcard(require("../packages/a/src/index")));
const { displayA } = await Promise.resolve().then(()=>/*#__PURE__*/ _interopRequireWildcard(require("../packages/a/src/index.ts")));
console.log(displayA());
})();
Expand Up @@ -2,7 +2,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
require("./core/module/moduleFile");
require("./core/module/moduleFile.ts");
require("./core/utils");
require("./core/utilFile");
require("./utils");
Expand Down
@@ -1,3 +1,3 @@
import { sum } from "./modules/01-moduleA/utils";
import { sum } from "./modules/01-moduleA/utils.ts";
export { multiply } from "./modules/03-moduleB/utils";
console.log(sum(1, 2));
Expand Up @@ -3,5 +3,5 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
const _interopRequireDefault = require("@swc/helpers/lib/_interop_require_default.js").default;
const _a = /*#__PURE__*/ _interopRequireDefault(require("./subfolder/A"));
const _a = /*#__PURE__*/ _interopRequireDefault(require("./subfolder/A.ts"));
console.log(_a.default);
@@ -1,2 +1,2 @@
import styles from "./src/foo.bar.baz";
import styles from "./src/foo.bar.baz.js";
console.log(styles);
17 changes: 17 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6209/1/input/.swcrc
@@ -0,0 +1,17 @@
{
"jsc": {
"baseUrl": "./src",
"paths": {
"@modules/*": [
"./modules/*"
]
},
"target": "es2020",
"parser": {
"syntax": "typescript"
}
},
"module": {
"type": "es6"
}
}
5 changes: 5 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6209/1/input/index.js
@@ -0,0 +1,5 @@
import { modulo } from '@modules/style.module.less'
import { sum } from './sum.js'

console.log(modulo)
console.log(sum)
@@ -0,0 +1,5 @@


export function sum(a: number, b: number) {
return a + b;
}
4 changes: 4 additions & 0 deletions crates/swc/tests/fixture/issues-6xxx/6209/1/output/index.js
@@ -0,0 +1,4 @@
import { modulo } from "./src/modules/style.module.less";
import { sum } from "./sum.js";
console.log(modulo);
console.log(sum);
@@ -0,0 +1,3 @@
export function sum(a, b) {
return a + b;
}
58 changes: 6 additions & 52 deletions crates/swc_ecma_transforms_module/src/path.rs
Expand Up @@ -112,51 +112,13 @@ where
R: Resolve,
{
fn resolve_import(&self, base: &FileName, module_specifier: &str) -> Result<JsWord, Error> {
fn to_specifier(
target_path: &str,
is_file: Option<bool>,
orig_ext: Option<&str>,
) -> JsWord {
let mut p = PathBuf::from(target_path);
fn to_specifier(target_path: &str, orig_ext: Option<&str>) -> JsWord {
let p = PathBuf::from(target_path);

if cfg!(debug_assertions) {
trace!("to_specifier: orig_ext={:?}", orig_ext);
}

let dot_count = p
.file_name()
.map(|s| s.to_string_lossy())
.map(|v| v.as_bytes().iter().filter(|&&c| c == b'.').count())
.unwrap_or(0);

match orig_ext {
Some(orig_ext) => {
if is_file.unwrap_or_else(|| p.is_file()) {
if let Some(..) = p.extension() {
if orig_ext == "ts"
|| orig_ext == "tsx"
|| orig_ext == "js"
|| orig_ext == "jsx"
|| dot_count == 1
{
p.set_extension(orig_ext);
} else {
p.set_extension("");
}
}
}
}
_ => {
if is_file.unwrap_or_else(|| p.is_file()) {
if let Some(v) = p.extension() {
if v == "ts" || v == "tsx" || v == "js" || v == "jsx" {
p.set_extension("");
}
}
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was workaround for ts, i.e.

import foo from "./test.ts"

to

import foo from "./test"

So test.js can be resolved, not sure why it was implemented in this way 😕

babel keeps exetensions too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤣 I made lots of mistakes


p.display().to_string().into()
}

Expand Down Expand Up @@ -197,7 +159,7 @@ where

let mut target = match target {
FileName::Real(v) => v,
FileName::Custom(s) => return Ok(to_specifier(&s, None, orig_ext)),
FileName::Custom(s) => return Ok(to_specifier(&s, orig_ext)),
_ => {
unreachable!(
"Node path provider does not support using `{:?}` as a target file name",
Expand All @@ -222,8 +184,6 @@ where
}
};

let is_file = target.is_file();

if base.is_absolute() != target.is_absolute() {
base = Cow::Owned(absolute_path(&base)?);
target = absolute_path(&target)?;
Expand All @@ -239,13 +199,7 @@ where

let rel_path = match rel_path {
Some(v) => v,
None => {
return Ok(to_specifier(
&target.display().to_string(),
Some(is_file),
orig_ext,
))
}
None => return Ok(to_specifier(&target.display().to_string(), orig_ext)),
};

{
Expand Down Expand Up @@ -273,9 +227,9 @@ where
Cow::Owned(format!("./{}", s))
};
if cfg!(target_os = "windows") {
Ok(to_specifier(&s.replace('\\', "/"), Some(is_file), orig_ext))
Ok(to_specifier(&s.replace('\\', "/"), orig_ext))
} else {
Ok(to_specifier(&s, Some(is_file), orig_ext))
Ok(to_specifier(&s, orig_ext))
}
}
}
Expand Down
@@ -1,6 +1,6 @@
import { displayB } from "../packages/b/src/index";
import { displayB } from "../packages/b/src/index.ts";
async function display() {
const displayA = await import("../packages/a/src/index").then((c)=>c.displayA);
const displayA = await import("../packages/a/src/index.ts").then((c)=>c.displayA);
console.log(displayA());
console.log(displayB());
}
Expand Down
@@ -1 +1 @@
import "./rel.js";
import "./rel.ts";
@@ -1 +1 @@
import "./src/feat.js";
import "./src/feat.ts";
@@ -1 +1 @@
import "./src/rel.decorator.js";
import "./src/rel.decorator.ts";
@@ -1 +1 @@
import "./src/rel.decorator";
import "./src/rel.decorator.ts";
@@ -1 +1 @@
import "./src/rel.decorator.js";
import "./src/rel.decorator.ts";
@@ -1 +1 @@
import "./src/rel.decorator";
import "./src/rel.decorator.ts";
2 changes: 1 addition & 1 deletion packages/swc-helpers/package.json
Expand Up @@ -30,7 +30,7 @@
"homepage": "https://swc.rs",
"devDependencies": {
"@swc/cli": "^0.1.36",
"@swc/core": "^1.2.196"
"@swc/core": "=1.3.3"
},
"dependencies": {
"tslib": "^2.4.0"
Expand Down