Skip to content

Add rewriteRelativeImportExtension helper - #270

Merged
Andrew Branch (andrewbranch) merged 9 commits into
mainfrom
rewriteRelativeImportExtension
Oct 14, 2024
Merged

Add rewriteRelativeImportExtension helper#270
Andrew Branch (andrewbranch) merged 9 commits into
mainfrom
rewriteRelativeImportExtension

Conversation

@andrewbranch

Copy link
Copy Markdown
Member

Comment threadtslib.es6.js Outdated
Comment threadtslib.es6.js Outdated
Comment threadtslib.es6.js Outdated
Comment threadtslib.es6.js Outdated
Comment threadtslib.es6.js Outdated
Comment threadtslib.es6.js Outdated
export function __rewriteRelativeImportExtension(path, preserveJsx) {
if (typeof path === "string" && /^\.\.?\//.test(path)) {
return path.replace(/\.(tsx)$|(\.d)?(\.[^./]+?)?\.([cm])?ts$/i, function (m, tsx, d, ext, cm) {
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (ext && !cm || !ext) ? m : ((d || "") + (ext || "") + "." + (cm || "").toLowerCase() + "js");

@rbucktonRon Buckton (rbuckton)Sep 24, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
returntsx ? preserveJsx ? ".jsx" : ".js" : d&&(ext&&!cm||!ext) ? m : ((d||"")+(ext||"")+"."+(cm||"").toLowerCase()+"js");
returntsx ? preserveJsx ? ".jsx" : ".js" : d&&!(ext&&cm) ? m : ((d||"")+(ext||"")+"."+(cm||"").toLowerCase()+"js");

To reduce the amount of || "" that you're doing, you could move the quantifiers inside of the capture groups so that they capture empty strings instead of undefined:

/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i

Also, to clarify the d && (ext && !cm || !ext) case: is this saying that d.ts (no ext and no cm) remains .d.ts, .d.cts (no ext but has cm) remains .d.cts, but .d.css.cts (has ext and cm) becomes .d.css.cjs?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Yes, that’s right. .d.css.cts is not a recognized declaration file extension (even though it looks suspiciously like one), so we would have to treat it as any other *.cts file name. A real fishy edge case, but correct for today’s compiler, anyway.

Comment threadtslib.js
Comment threadtslib.es6.js
export function __rewriteRelativeImportExtension(path, preserveJsx) {
if (typeof path === "string" && /^\.\.?\//.test(path)) {
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Saving one character probably won't matter too much, but this is equivalent

Suggested change
returntsx ? preserveJsx ? ".jsx" : ".js" : d&&(!ext||!cm) ? m : (d+ext+"."+cm.toLowerCase()+"js");
returntsx ? preserveJsx ? ".jsx" : ".js" : d&&!(ext&&cm) ? m : (d+ext+"."+cm.toLowerCase()+"js");

@andrewbranch
Andrew Branch (andrewbranch) deleted the rewriteRelativeImportExtension branch October 14, 2024 17:27
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@andrewbranch@rbuckton