Repository navigation
fix(cli): rewrite import extensions in transpile output #36403
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
Open
rathodkunj2005
wants to merge
6
commits into
denoland:main
Choose a base branch
from
rathodkunj2005:fix/transpile-import-extensions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c4a2f33
fix(cli): rewrite import extensions in transpile output
rathodkunj2005 4aadb71
fix: only rewrite extensions for relative specifiers
bartlomieju 182fbcf
fix(transpile): cover jsx and import-equals rewrites
rathodkunj2005 d9a6834
chore: format transpile fixture
rathodkunj2005 58031e7
test(transpile): keep import-equals binding so the rewrite is observable
rathodkunj2005 1deb41f
ci: retrigger checks
rathodkunj2005 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright 2018-2026 the Deno authors. MIT license. | ||
|
|
||
| pub fn rewrite(specifier: &str) -> Option<String> { | ||
| // Only relative specifiers can refer to a file we're emitting. Remote URLs, | ||
| // `npm:`/`jsr:` and bare specifiers all point at modules we don't rewrite, | ||
| // so changing their extension would just break the import. | ||
| if !specifier.starts_with("./") && !specifier.starts_with("../") { | ||
| return None; | ||
| } | ||
| let path_end = specifier.find(['?', '#']).unwrap_or(specifier.len()); | ||
| let (path, suffix) = specifier.split_at(path_end); | ||
| if path.ends_with(".d.ts") | ||
| || path.ends_with(".d.mts") | ||
| || path.ends_with(".d.cts") | ||
| { | ||
| None | ||
| } else if let Some(path) = path.strip_suffix(".tsx") { | ||
| Some(format!("{path}.js{suffix}")) | ||
| } else if let Some(path) = path.strip_suffix(".jsx") { | ||
| Some(format!("{path}.js{suffix}")) | ||
| } else if let Some(path) = path.strip_suffix(".ts") { | ||
| Some(format!("{path}.js{suffix}")) | ||
| } else if let Some(path) = path.strip_suffix(".mts") { | ||
| Some(format!("{path}.mjs{suffix}")) | ||
| } else { | ||
|
rathodkunj2005 marked this conversation as resolved.
Resolved
|
||
| path | ||
| .strip_suffix(".cts") | ||
| .map(|path| format!("{path}.cjs{suffix}")) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn rewrites_typescript_extensions() { | ||
| assert_eq!(rewrite("./mod.ts").as_deref(), Some("./mod.js")); | ||
| assert_eq!(rewrite("../mod.tsx").as_deref(), Some("../mod.js")); | ||
| assert_eq!(rewrite("./mod.jsx").as_deref(), Some("./mod.js")); | ||
| assert_eq!(rewrite("./mod.mts").as_deref(), Some("./mod.mjs")); | ||
| assert_eq!(rewrite("./mod.cts").as_deref(), Some("./mod.cjs")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn preserves_suffixes_and_non_typescript_extensions() { | ||
| assert_eq!( | ||
| rewrite("./mod.ts?raw#fragment").as_deref(), | ||
| Some("./mod.js?raw#fragment") | ||
| ); | ||
| assert_eq!(rewrite("./mod.d.ts"), None); | ||
| assert_eq!(rewrite("./mod.js"), None); | ||
| assert_eq!(rewrite("./literal.ts.txt"), None); | ||
| } | ||
|
|
||
| #[test] | ||
| fn preserves_non_relative_specifiers() { | ||
| // None of these resolve to a file `deno transpile` emits, so rewriting | ||
| // their extension would break the import. | ||
| assert_eq!(rewrite("https://deno.land/x/foo/mod.ts"), None); | ||
| assert_eq!(rewrite("http://localhost:8000/mod.ts"), None); | ||
| assert_eq!(rewrite("file:///abs/mod.ts"), None); | ||
| assert_eq!(rewrite("npm:some-pkg/mod.ts"), None); | ||
| assert_eq!(rewrite("jsr:@scope/pkg/mod.ts"), None); | ||
| assert_eq!(rewrite("some-pkg/mod.ts"), None); | ||
| assert_eq!(rewrite("/abs/mod.ts"), None); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| const output = await Deno.readTextFile("dist/import_extensions.js"); | ||
|
|
||
| export {}; | ||
|
|
||
| console.log( | ||
| output.includes('import "./helpers.js";') && | ||
| output.includes('export { add } from "./helpers.js";') && | ||
| output.includes('export * from "./helper.mjs";') && | ||
| output.includes('await import("./helper.cjs");') && | ||
| output.includes('import helper = require("./helper.cjs");') && | ||
| output.includes('import "./view.js";') && | ||
| output.includes('console.log("./literal.ts");'), | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import "./helpers.ts"; | ||
| import "./view.jsx"; | ||
| import helper = require("./helper.cts"); | ||
| console.log(helper); | ||
| export { add } from "./helpers.ts"; | ||
| export * from "./helper.mts"; | ||
|
|
||
| await import("./helper.cts"); | ||
|
|
||
| console.log("./literal.ts"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| [WILDCARD]deno transpile is experimental and subject to changes | ||
| Emit [WILDCARD]dist[WILDCARD]import_extensions.js | ||
| Emit [WILDCARD]dist[WILDCARD]helpers.js | ||
| Emit [WILDCARD]dist[WILDCARD]helper.mjs | ||
| Emit [WILDCARD]dist[WILDCARD]helper.cjs | ||
| Emit [WILDCARD]dist[WILDCARD]view.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export function View() { | ||
| return "view"; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.