From 24925ba946c36b0d916b6942e295bf97d2cf8d1c Mon Sep 17 00:00:00 2001 From: LorexIQ Date: Fri, 15 Nov 2024 12:12:05 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D0=BC=D1=8B=D1=85?= =?UTF-8?q?=20=D0=B8=D0=BC=D0=BF=D0=BE=D1=80=D1=82=D0=BE=D0=B2=20=D0=B2=20?= =?UTF-8?q?cjs=20=D0=B8=20=D1=83=D1=81=D0=BA=D0=BE=D1=80=D0=B5=D0=BD=20ts-?= =?UTF-8?q?morph?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/runtime/autoImport.ts | 7 ++-- src/runtime/helpers/getTsMorphProject.ts | 10 ------ src/runtime/helpers/loadTsModule.ts | 41 +++++++++++++++++++++--- src/runtime/helpers/tsMorphProject.ts | 10 ++++++ src/runtime/helpers/typeGenerator.ts | 5 ++- 5 files changed, 52 insertions(+), 21 deletions(-) delete mode 100644 src/runtime/helpers/getTsMorphProject.ts create mode 100644 src/runtime/helpers/tsMorphProject.ts diff --git a/src/runtime/autoImport.ts b/src/runtime/autoImport.ts index f9ea10d..718775e 100644 --- a/src/runtime/autoImport.ts +++ b/src/runtime/autoImport.ts @@ -14,14 +14,13 @@ import type { } from './types'; import defineConnector from './composables/defineConnector'; import loadTsModule from './helpers/loadTsModule'; -import getTsMorphProject from './helpers/getTsMorphProject'; +import tsMorphProject from './helpers/tsMorphProject'; import pathRelativeMove from './helpers/pathRelativeMove'; import logger from './helpers/logger'; export class Module { private readonly rootDir: string; private readonly debugEnabled: boolean; - private readonly project = getTsMorphProject(); private readonly config: ModuleOptionsExtend; private readonly typeGeneratorListFunc: ModuleConnectorTypeGenerator[] = []; @@ -56,8 +55,8 @@ export class Module { if (!fs.existsSync(definesDir)) fs.mkdirSync(definesDir); - const parsedConnector = this.project.createSourceFile(`${name}.${Date.now()}.temp.ts`, content); - const sourceFile = this.project.createSourceFile(definePath, '', { overwrite: true }); + const parsedConnector = tsMorphProject.createSourceFile(`${name}.temp.ts`, content, { overwrite: true }); + const sourceFile = tsMorphProject.createSourceFile(definePath, '', { overwrite: true }); sourceFile.addImportDeclaration({ moduleSpecifier: '../types', diff --git a/src/runtime/helpers/getTsMorphProject.ts b/src/runtime/helpers/getTsMorphProject.ts deleted file mode 100644 index 7fece74..0000000 --- a/src/runtime/helpers/getTsMorphProject.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { IndentationText, Project } from 'ts-morph'; - -export default function () { - return new Project({ - tsConfigFilePath: './tsconfig.json', - manipulationSettings: { - indentationText: IndentationText.TwoSpaces - } - }); -} diff --git a/src/runtime/helpers/loadTsModule.ts b/src/runtime/helpers/loadTsModule.ts index 1b9e1e5..89d577a 100644 --- a/src/runtime/helpers/loadTsModule.ts +++ b/src/runtime/helpers/loadTsModule.ts @@ -2,22 +2,55 @@ import path from 'node:path'; import fs from 'node:fs'; import { pathToFileURL } from 'node:url'; import esbuild from 'esbuild'; +import { resolveAlias } from '@nuxt/kit'; +import type { SourceFile } from 'ts-morph'; +import tsMorphProject from './tsMorphProject'; -export default async function (modulePath: string) { +function clearTemp(tempPath: string, file?: SourceFile) { + if (fs.existsSync(tempPath)) fs.unlinkSync(tempPath); + file && tsMorphProject.removeSourceFile(file); +} + +function getImportPath(ctxPath: string, p: string) { + const asAliasResolve = resolveAlias(p); + const asPathResolve = path.resolve(ctxPath, p); + const isAliasPath = asAliasResolve !== p; + const isRelativePath = p.startsWith('.') || p.startsWith('/'); + const importPath = isRelativePath ? asPathResolve : asAliasResolve; + + if (isAliasPath || isRelativePath) { + if (fs.existsSync(importPath + '.ts')) return importPath + '.ts'; + else if (fs.existsSync(importPath + '\\index.ts')) return importPath + '\\index.ts'; + else return undefined; + } else { + return undefined; + } +} + +export default async function loadTsModule(modulePath: string) { const fullPath = path.resolve(modulePath); + const parsedPath = path.parse(fullPath); + const tempTsFilePath = fullPath.replace(/\.ts$/, `.${Date.now()}.temp.ts`); const tempJsFilePath = fullPath.replace(/\.ts$/, `.${Date.now()}.temp.js`); + let sourceFile: SourceFile | undefined = undefined; try { const tsCode = fs.readFileSync(fullPath, 'utf8'); - const { code } = await esbuild.transform(tsCode, { loader: 'ts' }); + sourceFile = tsMorphProject.createSourceFile(tempTsFilePath, tsCode, { overwrite: true }); + + await Promise.all(sourceFile.getImportDeclarations().map(async (imp) => { + const importPath = getImportPath(parsedPath.dir, imp.getStructure().moduleSpecifier); + if (importPath) imp.remove(); + })); + const { code } = await esbuild.transform(sourceFile.getFullText(), { loader: 'ts' }); fs.writeFileSync(tempJsFilePath, code, 'utf8'); const module = await import(pathToFileURL(tempJsFilePath).href); - fs.unlinkSync(tempJsFilePath); + clearTemp(tempJsFilePath, sourceFile); return module; } catch { - if (fs.existsSync(tempJsFilePath)) fs.unlinkSync(tempJsFilePath); + clearTemp(tempJsFilePath, sourceFile); return undefined; } } diff --git a/src/runtime/helpers/tsMorphProject.ts b/src/runtime/helpers/tsMorphProject.ts new file mode 100644 index 0000000..f8782b6 --- /dev/null +++ b/src/runtime/helpers/tsMorphProject.ts @@ -0,0 +1,10 @@ +import { IndentationText, Project } from 'ts-morph'; + +const tsMorphProject = new Project({ + tsConfigFilePath: './tsconfig.json', + manipulationSettings: { + indentationText: IndentationText.TwoSpaces + } +}); + +export default tsMorphProject; diff --git a/src/runtime/helpers/typeGenerator.ts b/src/runtime/helpers/typeGenerator.ts index 5c8bb4a..561e4f1 100644 --- a/src/runtime/helpers/typeGenerator.ts +++ b/src/runtime/helpers/typeGenerator.ts @@ -1,7 +1,7 @@ import path from 'node:path'; import fs from 'node:fs'; import type { WriterFunction } from 'ts-morph'; -import getTsMorphProject from './getTsMorphProject'; +import tsMorphProject from './tsMorphProject'; export default function (typesDir: string, typeName: string, content: string | WriterFunction, tryRead = false) { if (typeName.length === 0) return; @@ -12,8 +12,7 @@ export default function (typesDir: string, typeName: string, content: string | W if (tryRead && fs.existsSync(filePath)) return filePath; - const project = getTsMorphProject(); - const file = project.createSourceFile(filePath, '', { overwrite: true }); + const file = tsMorphProject.createSourceFile(filePath, '', { overwrite: true }); file.addStatements((writer) => { writer.write('declare global').block(() => {