Skip to content

Commit

Permalink
fix: adapt imports replacement with tsconfig outDir depth
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrisole committed Nov 14, 2023
1 parent c4cbca6 commit c5b171a
Showing 1 changed file with 22 additions and 1 deletion.
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');
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

0 comments on commit c5b171a

Please sign in to comment.