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

Add rewriteRelativeImportExtension helper #270

Merged
merged 9 commits into from
Oct 14, 2024

Conversation

andrewbranch
Copy link
Member

tslib.es6.js Outdated Show resolved Hide resolved
tslib.es6.js Outdated Show resolved Hide resolved
tslib.es6.js Outdated Show resolved Hide resolved
tslib.es6.js Outdated Show resolved Hide resolved
tslib.es6.js Outdated Show resolved Hide resolved
tslib.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");
Copy link
Member

@rbuckton rbuckton Sep 24, 2024

Choose a reason for hiding this comment

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

Suggested change
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (ext && !cm || !ext) ? m : ((d || "") + (ext || "") + "." + (cm || "").toLowerCase() + "js");
return tsx ? 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
Member Author

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.

tslib.js Show resolved Hide resolved
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
Member

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
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
return tsx ? preserveJsx ? ".jsx" : ".js" : d && !(ext && cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");

Sign up for free to 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