зеркало из https://github.com/microsoft/tslib.git
Add rewriteRelativeImportExtension helper
This commit is contained in:
Родитель
497872a54e
Коммит
689efe3cd2
|
@ -33,5 +33,6 @@ export {
|
|||
__createBinding,
|
||||
__addDisposableResource,
|
||||
__disposeResources,
|
||||
__rewriteRelativeImportExtension,
|
||||
} from '../tslib.js';
|
||||
export * as default from '../tslib.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;
|
||||
|
|
|
@ -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");
|
||||
});
|
||||
});
|
|
@ -451,3 +451,10 @@ export declare function __addDisposableResource<T>(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;
|
18
tslib.es6.js
18
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(/(?<!\.d)\.[cm]ts$/, function (ext) { return ext === ".mts" ? ".mjs" : ".cjs"; });
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
export default {
|
||||
__extends: __extends,
|
||||
__assign: __assign,
|
||||
|
@ -380,4 +397,5 @@ export default {
|
|||
__classPrivateFieldIn: __classPrivateFieldIn,
|
||||
__addDisposableResource: __addDisposableResource,
|
||||
__disposeResources: __disposeResources,
|
||||
__rewriteRelativeImportExtension: __rewriteRelativeImportExtension,
|
||||
};
|
||||
|
|
|
@ -347,6 +347,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(/(?<!\.d)\.[cm]ts$/, function (ext) { return ext === ".mts" ? ".mjs" : ".cjs"; });
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
export default {
|
||||
__extends,
|
||||
__assign,
|
||||
|
@ -379,4 +396,5 @@ export default {
|
|||
__classPrivateFieldIn,
|
||||
__addDisposableResource,
|
||||
__disposeResources,
|
||||
__rewriteRelativeImportExtension,
|
||||
};
|
||||
|
|
20
tslib.js
20
tslib.js
|
@ -44,6 +44,7 @@ var __classPrivateFieldIn;
|
|||
var __createBinding;
|
||||
var __addDisposableResource;
|
||||
var __disposeResources;
|
||||
var __rewriteRelativeImportExtension;
|
||||
(function (factory) {
|
||||
var root = typeof global === "object" ? global : typeof self === "object" ? self : typeof this === "object" ? this : {};
|
||||
if (typeof define === "function" && define.amd) {
|
||||
|
@ -395,6 +396,23 @@ var __disposeResources;
|
|||
return next();
|
||||
};
|
||||
|
||||
__rewriteRelativeImportExtension = function (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(/(?<!\.d)\.[cm]ts$/, function (ext) { return ext === ".mts" ? ".mjs" : ".cjs"; });
|
||||
}
|
||||
return path;
|
||||
};
|
||||
|
||||
exporter("__extends", __extends);
|
||||
exporter("__assign", __assign);
|
||||
exporter("__rest", __rest);
|
||||
|
@ -426,6 +444,7 @@ var __disposeResources;
|
|||
exporter("__classPrivateFieldIn", __classPrivateFieldIn);
|
||||
exporter("__addDisposableResource", __addDisposableResource);
|
||||
exporter("__disposeResources", __disposeResources);
|
||||
exporter("__rewriteRelativeImportExtension", __rewriteRelativeImportExtension);
|
||||
});
|
||||
|
||||
0 && (module.exports = {
|
||||
|
@ -460,4 +479,5 @@ var __disposeResources;
|
|||
__classPrivateFieldIn,
|
||||
__addDisposableResource,
|
||||
__disposeResources,
|
||||
__rewriteRelativeImportExtension,
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче