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

fix: adapt imports replacement with tsconfig outDir depth #2707

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 22 additions & 1 deletion lib/plugin/utils/plugin-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { head } from 'lodash';
import { isAbsolute, posix } from 'path';
import {isAbsolute, join, posix} from 'path';
import * as ts from 'typescript';
import { PluginOptions } from '../merge-options';
import {
Expand All @@ -16,6 +16,8 @@ import {
isStringLiteral,
isStringMapping
} from './ast-utils';
import {readFileSync} from "node:fs";
import {normalize, sep} from "node:path"

export function getDecoratorOrUndefinedByNames(
names: string[],
Expand Down Expand Up @@ -166,6 +168,7 @@ export function replaceImportPath(

let relativePath = posix.relative(from, importPath);
relativePath = relativePath[0] !== '.' ? './' + relativePath : relativePath;
relativePath = adaptPathWithConfig(relativePath);

const nodeModulesText = 'node_modules';
const nodeModulePos = relativePath.indexOf(nodeModulesText);
Expand Down Expand Up @@ -331,6 +334,24 @@ function isOptionalBoolean(text: string) {
return typeof text === 'string' && text === 'boolean | undefined';
}

function adaptPathWithConfig(inputPath: string) {
try {
const rawConfig = readFileSync('tsconfig.json', 'utf8');
Copy link
Member

@micalevisk micalevisk Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned with this hard code "tsconfig.json"

As a dev, we can name our tsconfig file as we wish, so this readFile will always fail.

Not sure how to handle this properly

const parsedConfig = JSON.parse(rawConfig);
const outDir = parsedConfig.compilerOptions.outDir;

const outDirDepth = normalize(outDir)
.split(sep)
.map(() => '..')
.join(sep);

return join(outDirDepth, inputPath);
} catch (error) {
console.error(error);
return inputPath;
}
}

/**
* Converts Windows specific file paths to posix
* @param windowsPath
Expand Down