Skip to content

Commit

Permalink
fix: format compilerOptions for ts2php, fixes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
l5oo00 authored and harttle committed Nov 14, 2019
1 parent f72f7af commit cb2329d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/loaders/common-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions src/target-php/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 ''

Expand All @@ -115,7 +124,7 @@ export class ToPHPCompiler implements Compiler {
return generatePHPCode(
sourceFile.tsSourceFile,
modules,
tsconfig['compilerOptions'],
this.formatCompilerOptions(tsconfig['compilerOptions']),
nsPrefix
)
}
Expand Down

0 comments on commit cb2329d

Please sign in to comment.