-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Rewrite relative import extensions with flag #59767
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
Changes from all commits
2439577
2b06e62
a398ad1
921f9ae
ac8a2fa
6e62350
c8ba95d
fce0a2d
1558c7f
0d95ce2
b88dddc
4867001
62dab48
d87beef
3a52370
84fa9ff
32b3be0
094c03c
b1974e0
a125bfd
5a1d785
5506655
5548868
b43ea23
d7b7e62
4f01fc1
35c6438
4c9557c
d2a7841
9f677d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import { | |
isCallExpression, | ||
isComputedPropertyName, | ||
isIdentifier, | ||
JsxEmit, | ||
memoize, | ||
ObjectLiteralElementLike, | ||
ParameterDeclaration, | ||
|
@@ -139,6 +140,8 @@ export interface EmitHelperFactory { | |
// 'using' helpers | ||
createAddDisposableResourceHelper(envBinding: Expression, value: Expression, async: boolean): Expression; | ||
createDisposeResourcesHelper(envBinding: Expression): Expression; | ||
// --rewriteRelativeImportExtensions helpers | ||
createRewriteRelativeImportExtensionsHelper(expression: Expression): Expression; | ||
} | ||
|
||
/** @internal */ | ||
|
@@ -189,6 +192,8 @@ export function createEmitHelperFactory(context: TransformationContext): EmitHel | |
// 'using' helpers | ||
createAddDisposableResourceHelper, | ||
createDisposeResourcesHelper, | ||
// --rewriteRelativeImportExtensions helpers | ||
createRewriteRelativeImportExtensionsHelper, | ||
}; | ||
|
||
/** | ||
|
@@ -682,6 +687,17 @@ export function createEmitHelperFactory(context: TransformationContext): EmitHel | |
context.requestEmitHelper(disposeResourcesHelper); | ||
return factory.createCallExpression(getUnscopedHelperName("__disposeResources"), /*typeArguments*/ undefined, [envBinding]); | ||
} | ||
|
||
function createRewriteRelativeImportExtensionsHelper(expression: Expression) { | ||
context.requestEmitHelper(rewriteRelativeImportExtensionsHelper); | ||
return factory.createCallExpression( | ||
getUnscopedHelperName("__rewriteRelativeImportExtension"), | ||
/*typeArguments*/ undefined, | ||
context.getCompilerOptions().jsx === JsxEmit.Preserve | ||
? [expression, factory.createTrue()] | ||
: [expression], | ||
); | ||
} | ||
} | ||
|
||
/** @internal */ | ||
|
@@ -1422,6 +1438,21 @@ const disposeResourcesHelper: UnscopedEmitHelper = { | |
});`, | ||
}; | ||
|
||
const rewriteRelativeImportExtensionsHelper: UnscopedEmitHelper = { | ||
name: "typescript:rewriteRelativeImportExtensions", | ||
importName: "__rewriteRelativeImportExtension", | ||
scoped: false, | ||
text: ` | ||
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) { | ||
if (typeof path === "string" && /^\\.\\.?\\//.test(path)) { | ||
return path.replace(/\\.(tsx)$|((?:\\.d)?)((?:\\.[^./]+?)?)\\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a path contains Should the regex be This now works: console.log(rewriteRelativeImportExtension("./main.tsx"));
console.log(rewriteRelativeImportExtension("./main.ts"));
console.log(rewriteRelativeImportExtension("./main.jsx"));
console.log(rewriteRelativeImportExtension("./main.jsx", true));
console.log(rewriteRelativeImportExtension("./main.js"));
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you clarify your actual behavior and expected behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nevermind, I'm writing a plugin that is based on |
||
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js"); | ||
}); | ||
} | ||
return path; | ||
};`, | ||
}; | ||
|
||
/** @internal */ | ||
export const asyncSuperHelper: EmitHelper = { | ||
name: "typescript:async-super", | ||
|
Uh oh!
There was an error while loading. Please reload this page.