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 Compileroption to add Extensions to relative imports #47436

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/compiler/transformers/module/esnextAnd2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ namespace ts {
// To give easy access to a synchronous `require` in node-flavor esm. We do the transform even in scenarios where we error, but `import.meta.url`
// is available, just because the output is reasonable for a node-like runtime.
return getEmitScriptTarget(compilerOptions) >= ModuleKind.ES2020 ? visitImportEqualsDeclaration(node as ImportEqualsDeclaration) : undefined;
case SyntaxKind.ImportDeclaration:
return visitImportDeclaration(node as ImportDeclaration);
case SyntaxKind.CallExpression:
return visitCallExpression(node as CallExpression);
case SyntaxKind.ExportAssignment:
return visitExportAssignment(node as ExportAssignment);
case SyntaxKind.ExportDeclaration:
Expand Down Expand Up @@ -183,12 +187,85 @@ namespace ts {
return statements;
}

function appendModuleExtensionWhenNeeded(path: string) {
if (path.startsWith('.') || path.startsWith('/')) {
if (path.endsWith('.ts')) {
return path.slice(0, -2) + compilerOptions.appendModuleExtension;
} else if (!path.endsWith('.js') &&
!path.endsWith('.mjs') &&
!path.endsWith('.json') &&
!path.endsWith('.css') &&
!path.endsWith('.' + compilerOptions.appendModuleExtension)) {
return path + '.' + compilerOptions.appendModuleExtension;
}
}
return path;
}

function visitImportDeclaration(node: ImportDeclaration): VisitResult<ImportDeclaration> {
// append `.js` (or another extension specified in options) to relative imports
if (getAppendModuleExtension(compilerOptions))
{
if (isStringLiteral(node.moduleSpecifier)) {
const newPath = appendModuleExtensionWhenNeeded(node.moduleSpecifier.text);
if (node.moduleSpecifier.text !== newPath) {
return factory.createImportDeclaration(
node.decorators,
node.modifiers,
node.importClause,
factory.createStringLiteral(newPath, node.moduleSpecifier.singleQuote),
node.assertClause);
}
}
}
return node;
}

function visitCallExpression(node: CallExpression): VisitResult<CallExpression> {
// append `.js` (or another extension specified in options) to relative imports
if (getAppendModuleExtension(compilerOptions))
{
if (isImportKeyword(node.expression)) {
if (node.arguments.length && isStringLiteral(node.arguments[0])) {
const pathLiteral = node.arguments[0];
const newPath = appendModuleExtensionWhenNeeded(pathLiteral.text);
if (pathLiteral.text !== newPath) {
const newArgs = [factory.createStringLiteral(newPath, pathLiteral.singleQuote), ...node.arguments.slice(1)];
node = factory.createCallExpression(
node.expression,
node.typeArguments,
newArgs)

}
}
}
}
return node;
}

function visitExportAssignment(node: ExportAssignment): VisitResult<ExportAssignment> {
// Elide `export=` as it is not legal with --module ES6
return node.isExportEquals ? undefined : node;
}

function visitExportDeclaration(node: ExportDeclaration) {
// append `.js` (or another extension specified in options) to relative exports
if (getAppendModuleExtension(compilerOptions))
{
if (node.moduleSpecifier && isStringLiteral(node.moduleSpecifier)) {
const newPath = appendModuleExtensionWhenNeeded(node.moduleSpecifier.text);
if (node.moduleSpecifier.text !== newPath) {
node = factory.createExportDeclaration(
node.decorators,
node.modifiers,
node.isTypeOnly,
node.exportClause,
factory.createStringLiteral(newPath, node.moduleSpecifier.singleQuote),
node.assertClause);
}
}
}

// `export * as ns` only needs to be transformed in ES2015
if (compilerOptions.module !== undefined && compilerOptions.module > ModuleKind.ES2015) {
return node;
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6099,6 +6099,7 @@ namespace ts {
mapRoot?: string;
maxNodeModuleJsDepth?: number;
module?: ModuleKind;
appendModuleExtension ?: 'js' | 'mjs';
moduleResolution?: ModuleResolutionKind;
newLine?: NewLineKind;
noEmit?: boolean;
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6161,6 +6161,12 @@ namespace ts {
ScriptTarget.ES3;
}

export function getAppendModuleExtension(compilerOptions: {module?: CompilerOptions["module"], appendModuleExtension ?: CompilerOptions["appendModuleExtension"]}) {
return compilerOptions.appendModuleExtension &&
(compilerOptions.module === ModuleKind.Node12 ||
compilerOptions.module === ModuleKind.NodeNext);
}

export function getEmitModuleKind(compilerOptions: {module?: CompilerOptions["module"], target?: CompilerOptions["target"]}) {
return typeof compilerOptions.module === "number" ?
compilerOptions.module :
Expand Down