From 689efe3cd26a664a85afefbcfaa098d3bbab7984 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Mon, 23 Sep 2024 10:09:50 -0700 Subject: [PATCH 1/9] Add rewriteRelativeImportExtension helper --- modules/index.d.ts | 1 + modules/index.js | 2 + .../rewriteRelativeImportExtension.test.js | 49 +++++++++++++++++++ tslib.d.ts | 7 +++ tslib.es6.js | 18 +++++++ tslib.es6.mjs | 18 +++++++ tslib.js | 20 ++++++++ 7 files changed, 115 insertions(+) create mode 100644 test/node/rewriteRelativeImportExtension.test.js diff --git a/modules/index.d.ts b/modules/index.d.ts index 0fedae8..3244fab 100644 --- a/modules/index.d.ts +++ b/modules/index.d.ts @@ -33,5 +33,6 @@ export { __createBinding, __addDisposableResource, __disposeResources, + __rewriteRelativeImportExtension, } from '../tslib.js'; export * as default from '../tslib.js'; diff --git a/modules/index.js b/modules/index.js index af9f5ac..c91f618 100644 --- a/modules/index.js +++ b/modules/index.js @@ -31,6 +31,7 @@ const { __classPrivateFieldIn, __addDisposableResource, __disposeResources, + __rewriteRelativeImportExtension, } = tslib; export { __extends, @@ -64,5 +65,6 @@ export { __classPrivateFieldIn, __addDisposableResource, __disposeResources, + __rewriteRelativeImportExtension, }; export default tslib; diff --git a/test/node/rewriteRelativeImportExtension.test.js b/test/node/rewriteRelativeImportExtension.test.js new file mode 100644 index 0000000..772d173 --- /dev/null +++ b/test/node/rewriteRelativeImportExtension.test.js @@ -0,0 +1,49 @@ +import assert from "node:assert"; +import { test } from "node:test"; +import { testHelper } from "./testHelper.js"; + +testHelper("__rewriteRelativeImportExtension", __rewriteRelativeImportExtension => { + test("rewrites relative .ts to .js", () => { + assert.equal(__rewriteRelativeImportExtension("./foo.ts"), "./foo.js"); + assert.equal(__rewriteRelativeImportExtension("../foo.ts"), "../foo.js"); + assert.equal(__rewriteRelativeImportExtension("../../foo.ts"), "../../foo.js"); + }); + + test("rewrites other TypeScript extensions", () => { + assert.equal(__rewriteRelativeImportExtension("./foo.mts"), "./foo.mjs"); + assert.equal(__rewriteRelativeImportExtension("./foo.cts"), "./foo.cjs"); + assert.equal(__rewriteRelativeImportExtension("./foo.tsx"), "./foo.js"); + assert.equal(__rewriteRelativeImportExtension("./foo.tsx", true), "./foo.jsx"); + }); + + test("does not rewrite other extensions", () => { + assert.equal(__rewriteRelativeImportExtension("./foo.js"), "./foo.js"); + assert.equal(__rewriteRelativeImportExtension("./foo.mjs"), "./foo.mjs"); + assert.equal(__rewriteRelativeImportExtension("./foo.cjs"), "./foo.cjs"); + assert.equal(__rewriteRelativeImportExtension("./foo.jsx"), "./foo.jsx"); + assert.equal(__rewriteRelativeImportExtension("./foo.json"), "./foo.json"); + assert.equal(__rewriteRelativeImportExtension("./foo.css"), "./foo.css"); + assert.equal(__rewriteRelativeImportExtension("./foo"), "./foo"); + }); + + test("does not rewrite non-relative imports", () => { + assert.equal(__rewriteRelativeImportExtension("foo.ts"), "foo.ts"); + assert.equal(__rewriteRelativeImportExtension("foo.mts"), "foo.mts"); + assert.equal(__rewriteRelativeImportExtension("foo.cts"), "foo.cts"); + assert.equal(__rewriteRelativeImportExtension("foo.tsx"), "foo.tsx"); + assert.equal(__rewriteRelativeImportExtension("foo.js"), "foo.js"); + assert.equal(__rewriteRelativeImportExtension("foo.mjs"), "foo.mjs"); + assert.equal(__rewriteRelativeImportExtension("foo.cjs"), "foo.cjs"); + assert.equal(__rewriteRelativeImportExtension("foo.jsx"), "foo.jsx"); + assert.equal(__rewriteRelativeImportExtension("foo.json"), "foo.json"); + assert.equal(__rewriteRelativeImportExtension("foo.css"), "foo.css"); + assert.equal(__rewriteRelativeImportExtension("foo"), "foo"); + }); + + test("does not rewrite declaration file extensions", () => { + assert.equal(__rewriteRelativeImportExtension("./foo.d.ts"), "./foo.d.ts"); + assert.equal(__rewriteRelativeImportExtension("./foo.d.mts"), "./foo.d.mts"); + assert.equal(__rewriteRelativeImportExtension("./foo.d.cts"), "./foo.d.cts"); + assert.equal(__rewriteRelativeImportExtension("./foo.d.css.ts"), "./foo.d.css.ts"); + }); +}); diff --git a/tslib.d.ts b/tslib.d.ts index 4576221..f23df55 100644 --- a/tslib.d.ts +++ b/tslib.d.ts @@ -451,3 +451,10 @@ export declare function __addDisposableResource(env: { stack: { value?: unkno * @seealso {@link __addDisposableResource} */ export declare function __disposeResources(env: { stack: { value?: unknown, dispose?: Function, async: boolean }[]; error: unknown; hasError: boolean; }): any; + +/** + * Transforms a relative import specifier ending in a non-declaration TypeScript file extension to its JavaScript file extension counterpart. + * @param path The import specifier. + * @param preserveJsx Causes '*.tsx' to transform to '*.jsx' instead of '*.js'. Should be true when `--jsx` is set to `preserve`. + */ +export declare function __rewriteRelativeImportExtension(path: string, preserveJsx?: boolean): string; \ No newline at end of file diff --git a/tslib.es6.js b/tslib.es6.js index 9365006..a1323a6 100644 --- a/tslib.es6.js +++ b/tslib.es6.js @@ -348,6 +348,23 @@ export function __disposeResources(env) { return next(); } +export function __rewriteRelativeImportExtension(path, preserveJsx) { + if (typeof path === "string" && path[0] === "." && (path[1] === "/" || path[1] === "." && path[2] === "/")) { + if (path.substring(path.length - 4) === ".tsx") { + return path.substring(0, path.length - 4) + (preserveJsx ? ".jsx" : ".js"); + } + if (path.substring(path.length - 3) === ".ts") { + var dot = path.lastIndexOf(".", path.length - 4); + if (dot >= 0 && (path.substring(dot - 2, dot) === ".d" || path.substring(dot, dot + 2) === ".d")) { + return path; + } + return path.substring(0, path.length - 3) + ".js"; + } + return path.replace(/(?= 0 && (path.substring(dot - 2, dot) === ".d" || path.substring(dot, dot + 2) === ".d")) { + return path; + } + return path.substring(0, path.length - 3) + ".js"; + } + return path.replace(/(?= 0 && (path.substring(dot - 2, dot) === ".d" || path.substring(dot, dot + 2) === ".d")) { + return path; + } + return path.substring(0, path.length - 3) + ".js"; + } + return path.replace(/(? Date: Tue, 24 Sep 2024 09:32:46 -0700 Subject: [PATCH 2/9] Case insensitivity, remove lookbehind --- .../rewriteRelativeImportExtension.test.js | 4 ++++ tslib.es6.js | 13 ++++++------ tslib.es6.mjs | 21 ++++++++++--------- tslib.js | 13 ++++++------ 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/test/node/rewriteRelativeImportExtension.test.js b/test/node/rewriteRelativeImportExtension.test.js index 772d173..25158e1 100644 --- a/test/node/rewriteRelativeImportExtension.test.js +++ b/test/node/rewriteRelativeImportExtension.test.js @@ -7,6 +7,8 @@ testHelper("__rewriteRelativeImportExtension", __rewriteRelativeImportExtension assert.equal(__rewriteRelativeImportExtension("./foo.ts"), "./foo.js"); assert.equal(__rewriteRelativeImportExtension("../foo.ts"), "../foo.js"); assert.equal(__rewriteRelativeImportExtension("../../foo.ts"), "../../foo.js"); + assert.equal(__rewriteRelativeImportExtension("./foo.TS"), "./foo.js"); + assert.equal(__rewriteRelativeImportExtension("./foo.Ts"), "./foo.js"); }); test("rewrites other TypeScript extensions", () => { @@ -14,6 +16,7 @@ testHelper("__rewriteRelativeImportExtension", __rewriteRelativeImportExtension assert.equal(__rewriteRelativeImportExtension("./foo.cts"), "./foo.cjs"); assert.equal(__rewriteRelativeImportExtension("./foo.tsx"), "./foo.js"); assert.equal(__rewriteRelativeImportExtension("./foo.tsx", true), "./foo.jsx"); + assert.equal(__rewriteRelativeImportExtension("./foo.Tsx", true), "./foo.jsx"); }); test("does not rewrite other extensions", () => { @@ -45,5 +48,6 @@ testHelper("__rewriteRelativeImportExtension", __rewriteRelativeImportExtension assert.equal(__rewriteRelativeImportExtension("./foo.d.mts"), "./foo.d.mts"); assert.equal(__rewriteRelativeImportExtension("./foo.d.cts"), "./foo.d.cts"); assert.equal(__rewriteRelativeImportExtension("./foo.d.css.ts"), "./foo.d.css.ts"); + assert.equal(__rewriteRelativeImportExtension("./foo.D.ts"), "./foo.D.ts"); }); }); diff --git a/tslib.es6.js b/tslib.es6.js index a1323a6..36b58a5 100644 --- a/tslib.es6.js +++ b/tslib.es6.js @@ -350,17 +350,18 @@ export function __disposeResources(env) { export function __rewriteRelativeImportExtension(path, preserveJsx) { if (typeof path === "string" && path[0] === "." && (path[1] === "/" || path[1] === "." && path[2] === "/")) { - if (path.substring(path.length - 4) === ".tsx") { + if (path.substring(path.length - 4).toLowerCase() === ".tsx") { return path.substring(0, path.length - 4) + (preserveJsx ? ".jsx" : ".js"); } - if (path.substring(path.length - 3) === ".ts") { - var dot = path.lastIndexOf(".", path.length - 4); - if (dot >= 0 && (path.substring(dot - 2, dot) === ".d" || path.substring(dot, dot + 2) === ".d")) { + var extMatch = path.match(/\.[cm]?ts$/i); + if (extMatch) { + var ext = extMatch[0].toLowerCase(); + var dot = path.lastIndexOf(".", path.length - (ext.length + 1)); + if (dot >= 0 && (path.substring(dot - 2, dot).toLowerCase() === ".d" || path.substring(dot, dot + 2).toLowerCase() === ".d")) { return path; } - return path.substring(0, path.length - 3) + ".js"; + return path.substring(0, path.length - ext.length) + (ext === ".mts" ? ".mjs" : ext === ".cts" ? ".cjs" : ".js"); } - return path.replace(/(?= 0 && (path.substring(dot - 2, dot) === ".d" || path.substring(dot, dot + 2) === ".d")) { - return path; + if (path.substring(path.length - 4).toLowerCase() === ".tsx") { + return path.substring(0, path.length - 4) + (preserveJsx ? ".jsx" : ".js"); + } + var extMatch = path.match(/\.[cm]?ts$/i); + if (extMatch) { + var ext = extMatch[0].toLowerCase(); + var dot = path.lastIndexOf(".", path.length - (ext.length + 1)); + if (dot >= 0 && (path.substring(dot - 2, dot).toLowerCase() === ".d" || path.substring(dot, dot + 2).toLowerCase() === ".d")) { + return path; + } + return path.substring(0, path.length - ext.length) + (ext === ".mts" ? ".mjs" : ext === ".cts" ? ".cjs" : ".js"); } - return path.substring(0, path.length - 3) + ".js"; - } - return path.replace(/(?= 0 && (path.substring(dot - 2, dot) === ".d" || path.substring(dot, dot + 2) === ".d")) { + var extMatch = path.match(/\.[cm]?ts$/i); + if (extMatch) { + var ext = extMatch[0].toLowerCase(); + var dot = path.lastIndexOf(".", path.length - (ext.length + 1)); + if (dot >= 0 && (path.substring(dot - 2, dot).toLowerCase() === ".d" || path.substring(dot, dot + 2).toLowerCase() === ".d")) { return path; } - return path.substring(0, path.length - 3) + ".js"; + return path.substring(0, path.length - ext.length) + (ext === ".mts" ? ".mjs" : ext === ".cts" ? ".cjs" : ".js"); } - return path.replace(/(? Date: Tue, 24 Sep 2024 09:38:27 -0700 Subject: [PATCH 3/9] Shorten by one line --- tslib.es6.js | 5 ++--- tslib.es6.mjs | 5 ++--- tslib.js | 5 ++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/tslib.es6.js b/tslib.es6.js index 36b58a5..5b36110 100644 --- a/tslib.es6.js +++ b/tslib.es6.js @@ -357,10 +357,9 @@ export function __rewriteRelativeImportExtension(path, preserveJsx) { if (extMatch) { var ext = extMatch[0].toLowerCase(); var dot = path.lastIndexOf(".", path.length - (ext.length + 1)); - if (dot >= 0 && (path.substring(dot - 2, dot).toLowerCase() === ".d" || path.substring(dot, dot + 2).toLowerCase() === ".d")) { - return path; + if (dot < 0 || !(path.substring(dot - 2, dot).toLowerCase() === ".d" || path.substring(dot, dot + 2).toLowerCase() === ".d")) { + return path.substring(0, path.length - ext.length) + (ext === ".mts" ? ".mjs" : ext === ".cts" ? ".cjs" : ".js"); } - return path.substring(0, path.length - ext.length) + (ext === ".mts" ? ".mjs" : ext === ".cts" ? ".cjs" : ".js"); } } return path; diff --git a/tslib.es6.mjs b/tslib.es6.mjs index ccffef3..7fed569 100644 --- a/tslib.es6.mjs +++ b/tslib.es6.mjs @@ -356,10 +356,9 @@ export function __rewriteRelativeImportExtension(path, preserveJsx) { if (extMatch) { var ext = extMatch[0].toLowerCase(); var dot = path.lastIndexOf(".", path.length - (ext.length + 1)); - if (dot >= 0 && (path.substring(dot - 2, dot).toLowerCase() === ".d" || path.substring(dot, dot + 2).toLowerCase() === ".d")) { - return path; + if (dot < 0 || !(path.substring(dot - 2, dot).toLowerCase() === ".d" || path.substring(dot, dot + 2).toLowerCase() === ".d")) { + return path.substring(0, path.length - ext.length) + (ext === ".mts" ? ".mjs" : ext === ".cts" ? ".cjs" : ".js"); } - return path.substring(0, path.length - ext.length) + (ext === ".mts" ? ".mjs" : ext === ".cts" ? ".cjs" : ".js"); } } return path; diff --git a/tslib.js b/tslib.js index 64090f0..d22e885 100644 --- a/tslib.js +++ b/tslib.js @@ -405,10 +405,9 @@ var __rewriteRelativeImportExtension; if (extMatch) { var ext = extMatch[0].toLowerCase(); var dot = path.lastIndexOf(".", path.length - (ext.length + 1)); - if (dot >= 0 && (path.substring(dot - 2, dot).toLowerCase() === ".d" || path.substring(dot, dot + 2).toLowerCase() === ".d")) { - return path; + if (dot < 0 || !(path.substring(dot - 2, dot).toLowerCase() === ".d" || path.substring(dot, dot + 2).toLowerCase() === ".d")) { + return path.substring(0, path.length - ext.length) + (ext === ".mts" ? ".mjs" : ext === ".cts" ? ".cjs" : ".js"); } - return path.substring(0, path.length - ext.length) + (ext === ".mts" ? ".mjs" : ext === ".cts" ? ".cjs" : ".js"); } } return path; From a55e9f7a805d9aa489fe29f7acb59fdc19f09472 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 24 Sep 2024 13:57:49 -0700 Subject: [PATCH 4/9] Do more with a regex --- tslib.es6.js | 11 +++-------- tslib.es6.mjs | 11 +++-------- tslib.js | 11 +++-------- 3 files changed, 9 insertions(+), 24 deletions(-) diff --git a/tslib.es6.js b/tslib.es6.js index 5b36110..fc6481a 100644 --- a/tslib.es6.js +++ b/tslib.es6.js @@ -353,14 +353,9 @@ export function __rewriteRelativeImportExtension(path, preserveJsx) { if (path.substring(path.length - 4).toLowerCase() === ".tsx") { return path.substring(0, path.length - 4) + (preserveJsx ? ".jsx" : ".js"); } - var extMatch = path.match(/\.[cm]?ts$/i); - if (extMatch) { - var ext = extMatch[0].toLowerCase(); - var dot = path.lastIndexOf(".", path.length - (ext.length + 1)); - if (dot < 0 || !(path.substring(dot - 2, dot).toLowerCase() === ".d" || path.substring(dot, dot + 2).toLowerCase() === ".d")) { - return path.substring(0, path.length - ext.length) + (ext === ".mts" ? ".mjs" : ext === ".cts" ? ".cjs" : ".js"); - } - } + return path.replace(/(\.d)?(\.[^./]+?)?\.([cm])?ts$/i, function (m, d, ext, cm) { + return d && (ext && !cm || !ext) ? m : ("." + (cm || "").toLowerCase() + "js"); + }); } return path; } diff --git a/tslib.es6.mjs b/tslib.es6.mjs index 7fed569..bd46885 100644 --- a/tslib.es6.mjs +++ b/tslib.es6.mjs @@ -352,14 +352,9 @@ export function __rewriteRelativeImportExtension(path, preserveJsx) { if (path.substring(path.length - 4).toLowerCase() === ".tsx") { return path.substring(0, path.length - 4) + (preserveJsx ? ".jsx" : ".js"); } - var extMatch = path.match(/\.[cm]?ts$/i); - if (extMatch) { - var ext = extMatch[0].toLowerCase(); - var dot = path.lastIndexOf(".", path.length - (ext.length + 1)); - if (dot < 0 || !(path.substring(dot - 2, dot).toLowerCase() === ".d" || path.substring(dot, dot + 2).toLowerCase() === ".d")) { - return path.substring(0, path.length - ext.length) + (ext === ".mts" ? ".mjs" : ext === ".cts" ? ".cjs" : ".js"); - } - } + return path.replace(/(\.d)?(\.[^./]+?)?\.([cm])?ts$/i, function (m, d, ext, cm) { + return d && (ext && !cm || !ext) ? m : ("." + (cm || "").toLowerCase() + "js"); + }); } return path; } diff --git a/tslib.js b/tslib.js index d22e885..7b627f0 100644 --- a/tslib.js +++ b/tslib.js @@ -401,14 +401,9 @@ var __rewriteRelativeImportExtension; if (path.substring(path.length - 4).toLowerCase() === ".tsx") { return path.substring(0, path.length - 4) + (preserveJsx ? ".jsx" : ".js"); } - var extMatch = path.match(/\.[cm]?ts$/i); - if (extMatch) { - var ext = extMatch[0].toLowerCase(); - var dot = path.lastIndexOf(".", path.length - (ext.length + 1)); - if (dot < 0 || !(path.substring(dot - 2, dot).toLowerCase() === ".d" || path.substring(dot, dot + 2).toLowerCase() === ".d")) { - return path.substring(0, path.length - ext.length) + (ext === ".mts" ? ".mjs" : ext === ".cts" ? ".cjs" : ".js"); - } - } + return path.replace(/(\.d)?(\.[^./]+?)?\.([cm])?ts$/i, function (m, d, ext, cm) { + return d && (ext && !cm || !ext) ? m : ("." + (cm || "").toLowerCase() + "js"); + }); } return path; }; From 1c03288ab27b787dc4cc3bc9e002c7d3d1a27815 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 24 Sep 2024 14:05:56 -0700 Subject: [PATCH 5/9] Test and fix invalid declaration-looking extensions --- test/node/rewriteRelativeImportExtension.test.js | 3 +++ tslib.es6.js | 2 +- tslib.es6.mjs | 2 +- tslib.js | 2 +- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/test/node/rewriteRelativeImportExtension.test.js b/test/node/rewriteRelativeImportExtension.test.js index 25158e1..af69458 100644 --- a/test/node/rewriteRelativeImportExtension.test.js +++ b/test/node/rewriteRelativeImportExtension.test.js @@ -9,6 +9,7 @@ testHelper("__rewriteRelativeImportExtension", __rewriteRelativeImportExtension assert.equal(__rewriteRelativeImportExtension("../../foo.ts"), "../../foo.js"); assert.equal(__rewriteRelativeImportExtension("./foo.TS"), "./foo.js"); assert.equal(__rewriteRelativeImportExtension("./foo.Ts"), "./foo.js"); + assert.equal(__rewriteRelativeImportExtension("./foo/.hidden/foo.ts"), "./foo/.hidden/foo.js"); }); test("rewrites other TypeScript extensions", () => { @@ -17,6 +18,8 @@ testHelper("__rewriteRelativeImportExtension", __rewriteRelativeImportExtension assert.equal(__rewriteRelativeImportExtension("./foo.tsx"), "./foo.js"); assert.equal(__rewriteRelativeImportExtension("./foo.tsx", true), "./foo.jsx"); assert.equal(__rewriteRelativeImportExtension("./foo.Tsx", true), "./foo.jsx"); + assert.equal(__rewriteRelativeImportExtension("./foo.d.css.mts"), "./foo.d.css.mjs"); + assert.equal(__rewriteRelativeImportExtension("./foo.d.tsx"), "./foo.d.js"); }); test("does not rewrite other extensions", () => { diff --git a/tslib.es6.js b/tslib.es6.js index fc6481a..545a8ef 100644 --- a/tslib.es6.js +++ b/tslib.es6.js @@ -354,7 +354,7 @@ export function __rewriteRelativeImportExtension(path, preserveJsx) { return path.substring(0, path.length - 4) + (preserveJsx ? ".jsx" : ".js"); } return path.replace(/(\.d)?(\.[^./]+?)?\.([cm])?ts$/i, function (m, d, ext, cm) { - return d && (ext && !cm || !ext) ? m : ("." + (cm || "").toLowerCase() + "js"); + return d && (ext && !cm || !ext) ? m : ((d || "") + (ext || "") + "." + (cm || "").toLowerCase() + "js"); }); } return path; diff --git a/tslib.es6.mjs b/tslib.es6.mjs index bd46885..3f7c843 100644 --- a/tslib.es6.mjs +++ b/tslib.es6.mjs @@ -353,7 +353,7 @@ export function __rewriteRelativeImportExtension(path, preserveJsx) { return path.substring(0, path.length - 4) + (preserveJsx ? ".jsx" : ".js"); } return path.replace(/(\.d)?(\.[^./]+?)?\.([cm])?ts$/i, function (m, d, ext, cm) { - return d && (ext && !cm || !ext) ? m : ("." + (cm || "").toLowerCase() + "js"); + return d && (ext && !cm || !ext) ? m : ((d || "") + (ext || "") + "." + (cm || "").toLowerCase() + "js"); }); } return path; diff --git a/tslib.js b/tslib.js index 7b627f0..88c717f 100644 --- a/tslib.js +++ b/tslib.js @@ -402,7 +402,7 @@ var __rewriteRelativeImportExtension; return path.substring(0, path.length - 4) + (preserveJsx ? ".jsx" : ".js"); } return path.replace(/(\.d)?(\.[^./]+?)?\.([cm])?ts$/i, function (m, d, ext, cm) { - return d && (ext && !cm || !ext) ? m : ("." + (cm || "").toLowerCase() + "js"); + return d && (ext && !cm || !ext) ? m : ((d || "") + (ext || "") + "." + (cm || "").toLowerCase() + "js"); }); } return path; From 9c731e9f325b1e8341c8fbc3a7ca572cc792f15b Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 24 Sep 2024 15:18:20 -0700 Subject: [PATCH 6/9] Combine tsx case into regex --- tslib.es6.js | 9 +++------ tslib.es6.mjs | 9 +++------ tslib.js | 9 +++------ 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/tslib.es6.js b/tslib.es6.js index 545a8ef..2c7eaad 100644 --- a/tslib.es6.js +++ b/tslib.es6.js @@ -349,12 +349,9 @@ export function __disposeResources(env) { } export function __rewriteRelativeImportExtension(path, preserveJsx) { - if (typeof path === "string" && path[0] === "." && (path[1] === "/" || path[1] === "." && path[2] === "/")) { - if (path.substring(path.length - 4).toLowerCase() === ".tsx") { - return path.substring(0, path.length - 4) + (preserveJsx ? ".jsx" : ".js"); - } - return path.replace(/(\.d)?(\.[^./]+?)?\.([cm])?ts$/i, function (m, d, ext, cm) { - return d && (ext && !cm || !ext) ? m : ((d || "") + (ext || "") + "." + (cm || "").toLowerCase() + "js"); + 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"); }); } return path; diff --git a/tslib.es6.mjs b/tslib.es6.mjs index 3f7c843..9c2c3f3 100644 --- a/tslib.es6.mjs +++ b/tslib.es6.mjs @@ -348,12 +348,9 @@ export function __disposeResources(env) { } export function __rewriteRelativeImportExtension(path, preserveJsx) { - if (typeof path === "string" && path[0] === "." && (path[1] === "/" || path[1] === "." && path[2] === "/")) { - if (path.substring(path.length - 4).toLowerCase() === ".tsx") { - return path.substring(0, path.length - 4) + (preserveJsx ? ".jsx" : ".js"); - } - return path.replace(/(\.d)?(\.[^./]+?)?\.([cm])?ts$/i, function (m, d, ext, cm) { - return d && (ext && !cm || !ext) ? m : ((d || "") + (ext || "") + "." + (cm || "").toLowerCase() + "js"); + 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"); }); } return path; diff --git a/tslib.js b/tslib.js index 88c717f..53455fd 100644 --- a/tslib.js +++ b/tslib.js @@ -397,12 +397,9 @@ var __rewriteRelativeImportExtension; }; __rewriteRelativeImportExtension = function (path, preserveJsx) { - if (typeof path === "string" && path[0] === "." && (path[1] === "/" || path[1] === "." && path[2] === "/")) { - if (path.substring(path.length - 4).toLowerCase() === ".tsx") { - return path.substring(0, path.length - 4) + (preserveJsx ? ".jsx" : ".js"); - } - return path.replace(/(\.d)?(\.[^./]+?)?\.([cm])?ts$/i, function (m, d, ext, cm) { - return d && (ext && !cm || !ext) ? m : ((d || "") + (ext || "") + "." + (cm || "").toLowerCase() + "js"); + 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"); }); } return path; From fece22a6b42e378abcf7dfbefaa89b62e0b814f8 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 24 Sep 2024 15:33:34 -0700 Subject: [PATCH 7/9] Add URL-ish test --- test/node/rewriteRelativeImportExtension.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/node/rewriteRelativeImportExtension.test.js b/test/node/rewriteRelativeImportExtension.test.js index af69458..6eb83d5 100644 --- a/test/node/rewriteRelativeImportExtension.test.js +++ b/test/node/rewriteRelativeImportExtension.test.js @@ -30,6 +30,7 @@ testHelper("__rewriteRelativeImportExtension", __rewriteRelativeImportExtension assert.equal(__rewriteRelativeImportExtension("./foo.json"), "./foo.json"); assert.equal(__rewriteRelativeImportExtension("./foo.css"), "./foo.css"); assert.equal(__rewriteRelativeImportExtension("./foo"), "./foo"); + assert.equal(__rewriteRelativeImportExtension("./foo.d.php?q=1.ts"), "./foo.d.php?q=1.ts"); }); test("does not rewrite non-relative imports", () => { From 27b219209a4d35308e9dd6a533e5169a981d2614 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 24 Sep 2024 16:04:07 -0700 Subject: [PATCH 8/9] Little optimizations --- tslib.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tslib.js b/tslib.js index 53455fd..95beb91 100644 --- a/tslib.js +++ b/tslib.js @@ -398,8 +398,8 @@ var __rewriteRelativeImportExtension; __rewriteRelativeImportExtension = function (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"); + 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"); }); } return path; From c4cde745896b4d845a11dc5aeb72405074c9a0c5 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 24 Sep 2024 16:52:24 -0700 Subject: [PATCH 9/9] Missed update --- tslib.es6.js | 4 ++-- tslib.es6.mjs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tslib.es6.js b/tslib.es6.js index 2c7eaad..ddd87f6 100644 --- a/tslib.es6.js +++ b/tslib.es6.js @@ -350,8 +350,8 @@ export function __disposeResources(env) { 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"); + 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"); }); } return path; diff --git a/tslib.es6.mjs b/tslib.es6.mjs index 9c2c3f3..a1d9ef5 100644 --- a/tslib.es6.mjs +++ b/tslib.es6.mjs @@ -349,8 +349,8 @@ export function __disposeResources(env) { 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"); + 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"); }); } return path;