Add rewriteRelativeImportExtension helper#270
Conversation
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"); |
There was a problem hiding this comment.
| 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?
There was a problem hiding this comment.
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.
| 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"); |
There was a problem hiding this comment.
Saving one character probably won't matter too much, but this is equivalent
| 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"); |
Counterpart to microsoft/TypeScript#59767