From cb2329da1c0dc091f57cbef53300a55bc851c34e Mon Sep 17 00:00:00 2001 From: l5oo00 Date: Sat, 9 Nov 2019 16:57:37 +0800 Subject: [PATCH] fix: format compilerOptions for ts2php, fixes #19 --- src/loaders/common-js.ts | 6 ++++++ src/target-php/index.ts | 13 +++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/loaders/common-js.ts b/src/loaders/common-js.ts index 3a3f4fe3..4e821b58 100644 --- a/src/loaders/common-js.ts +++ b/src/loaders/common-js.ts @@ -52,6 +52,12 @@ export class CommonJS { if (isRelativePath(path)) { return this.require(resolve(dirname(filepath), path), path) } + + // 兼容配置的模块名为 绝对路径或 node module 模块名 + if (this.modules[path] !== undefined) { + return this.require(path) + } + return require(path) }) return mod.exports diff --git a/src/target-php/index.ts b/src/target-php/index.ts index ee20c168..46b4a7b7 100644 --- a/src/target-php/index.ts +++ b/src/target-php/index.ts @@ -10,7 +10,7 @@ import { SanApp } from '../models/san-app' import camelCase from 'camelcase' import { SanSourceFile } from '../models/san-sourcefile' import { getDefaultConfigPath } from '../parsers/tsconfig' -import { sep, extname } from 'path' +import { sep, extname, isAbsolute, resolve } from 'path' import debugFactory from 'debug' import { Compiler } from '..' import { emitRuntime } from './emitters/runtime' @@ -90,6 +90,15 @@ export class ToPHPCompiler implements Compiler { emitter.endNamespace() } + private formatCompilerOptions (compilerOptions = { baseUrl: '' }) { + let baseUrl = compilerOptions.baseUrl + if (baseUrl && !isAbsolute(baseUrl)) { + baseUrl = resolve(this.root, baseUrl) + compilerOptions.baseUrl = baseUrl + } + return compilerOptions + } + public compileComponent (sourceFile: SanSourceFile, nsPrefix: string, modules: Modules) { if (!sourceFile.tsSourceFile) return '' @@ -115,7 +124,7 @@ export class ToPHPCompiler implements Compiler { return generatePHPCode( sourceFile.tsSourceFile, modules, - tsconfig['compilerOptions'], + this.formatCompilerOptions(tsconfig['compilerOptions']), nsPrefix ) }