Skip to content

Commit

Permalink
feat: Compiler#compile auto detect file format
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Oct 12, 2019
1 parent 37ee37f commit dc2c83d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 45 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/harttle/san-ssr-php.git"
"url": "git+https://github.com/harttle/san-ssr.git"
},
"author": "harttle <yangjvn@126.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/harttle/san-ssr-php/issues"
"url": "https://github.com/harttle/san-ssr/issues"
},
"homepage": "https://github.com/harttle/san-ssr-php#readme",
"homepage": "https://github.com/harttle/san-ssr#readme",
"devDependencies": {
"@babel/core": "^7.6.0",
"@babel/preset-env": "^7.6.0",
Expand Down
36 changes: 9 additions & 27 deletions src/bin/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import chalk from 'chalk'
import { ToPHPCompiler } from '../compilers/to-php-compiler'
import { ToJSCompiler } from '../compilers/to-js-compiler'
import { writeFileSync } from 'fs'
import { extname, resolve } from 'path'
import { resolve } from 'path'
import * as yargs from 'yargs'
import { byteCount } from '../utils/buffer'

Expand Down Expand Up @@ -39,40 +39,22 @@ const outputFile = yargs.argv.output as OptionValue
const componentFile = resolve(yargs.argv._[0])
console.error(chalk.gray('compiling'), componentFile, 'to', target)

let targetCode = ''
if (target === 'php') {
const toPHPCompiler = new ToPHPCompiler({
tsConfigFilePath,
externalModules: [{ name: '../../..', required: true }],
nsPrefix: 'san\\components\\test\\'
})
const ext = extname(componentFile)
const options = {
ns: `san\\renderer`
}
if (ext === '.ts') {
print(toPHPCompiler.compileFromTS(componentFile, options))
} else if (ext === '.js') {
print(toPHPCompiler.compileFromJS(componentFile, options))
} else {
throw new Error(`not recognized file extension: ${ext}`)
}
targetCode = toPHPCompiler.compile(componentFile, { ns: `san\\renderer` })
} else {
const toJSCompiler = new ToJSCompiler(tsConfigFilePath)
const ext = extname(componentFile)
if (ext === '.ts') {
print(toJSCompiler.compileFromTS(componentFile))
} else if (ext === '.js') {
print(toJSCompiler.compileFromJS(componentFile))
} else {
throw new Error(`not recognized file extension: ${ext}`)
}
targetCode = toJSCompiler.compile(componentFile)
}

function print (targetCode) {
if (outputFile !== undefined) {
writeFileSync(outputFile, targetCode)
} else {
process.stdout.write(targetCode)
}
console.error(chalk.green('success'), `${byteCount(targetCode)} bytes written`)
if (outputFile !== undefined) {
writeFileSync(outputFile, targetCode)
} else {
process.stdout.write(targetCode)
}
console.error(chalk.green('success'), `${byteCount(targetCode)} bytes written`)
20 changes: 15 additions & 5 deletions src/compilers/to-js-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Project } from 'ts-morph'
import { SanSourceFile } from '../parsers/san-sourcefile'
import { getDefaultConfigPath } from '../parsers/tsconfig'
import { Compiler } from './compiler'
import { sep } from 'path'
import { sep, extname } from 'path'
import debugFactory from 'debug'

const debug = debugFactory('to-js-compiler')
Expand All @@ -30,7 +30,17 @@ export class ToJSCompiler extends Compiler {
})
}

compileFromJS (filepath: string) {
public compile (filepath: string) {
const ext = extname(filepath)
if (ext === '.ts') {
return this.compileFromTS(filepath)
} else if (ext === '.js') {
return this.compileFromJS(filepath)
}
throw new Error(`not recognized file extension: ${ext}`)
}

public compileFromJS (filepath: string) {
const emitter = new JSEmitter()
emitter.write('module.exports = ')
emitter.writeAnonymousFunction(['data', 'noDataOutput'], () => {
Expand All @@ -42,7 +52,7 @@ export class ToJSCompiler extends Compiler {
return emitter.fullText()
}

compileFromTS (filepath: string) {
public compileFromTS (filepath: string) {
const emitter = new JSEmitter()
emitter.write('module.exports = ')
emitter.writeAnonymousFunction(['data', 'noDataOutput'], () => {
Expand All @@ -55,7 +65,7 @@ export class ToJSCompiler extends Compiler {
return emitter.fullText()
}

evalComponentClass (component: Component) {
public evalComponentClass (component: Component) {
const commonJS = new CommonJS(filepath => {
const sourceFile = component.getModule(filepath)
if (!sourceFile) throw new Error(`file ${filepath} not found`)
Expand All @@ -65,7 +75,7 @@ export class ToJSCompiler extends Compiler {
return commonJS.require(component.getComponentFilepath()).default
}

compileToJS (source: SanSourceFile) {
public compileToJS (source: SanSourceFile) {
const compilerOptions = this.tsConfigFilePath['compilerOptions']
const { diagnostics, outputText } =
transpileModule(source.getFullText(), { compilerOptions })
Expand Down
10 changes: 10 additions & 0 deletions src/compilers/to-php-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ export class ToPHPCompiler extends Compiler {
this.toJSCompiler = new ToJSCompiler(tsConfigFilePath)
}

public compile (filepath: string, options) {
const ext = extname(filepath)
if (ext === '.ts') {
return this.compileFromTS(filepath, options)
} else if (ext === '.js') {
return this.compileFromJS(filepath, options)
}
throw new Error(`not recognized file extension: ${ext}`)
}

public compileFromTS (filepath: string, {
funcName = 'render',
ns = 'san\\renderer',
Expand Down
16 changes: 6 additions & 10 deletions src/utils/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,19 @@ export function compileToJS (caseName) {
debug('compileToJS', caseName)
const ts = join(caseRoot, caseName, 'component.ts')
const js = resolve(caseRoot, caseName, 'component.js')
const targetCode = toJSCompiler.compile(existsSync(js) ? js : ts)

const fn = existsSync(js)
? toJSCompiler.compileFromJS(js)
: toJSCompiler.compileFromTS(ts)
writeFileSync(join(caseRoot, caseName, 'ssr.js'), `${fn}`)
writeFileSync(join(caseRoot, caseName, 'ssr.js'), targetCode)
}

export function compileToPHP (caseName) {
const ts = join(caseRoot, caseName, 'component.ts')
const js = resolve(caseRoot, caseName, 'component.js')
const options = {
ns: `san\\renderer\\${camelCase(caseName)}`
}
const targetCode = toPHPCompiler.compile(
existsSync(ts) ? ts : js,
{ ns: `san\\renderer\\${camelCase(caseName)}` }
)

const targetCode = existsSync(ts)
? toPHPCompiler.compileFromTS(ts, options)
: toPHPCompiler.compileFromJS(js, options)
writeFileSync(join(caseRoot, caseName, 'ssr.php'), targetCode)
}

Expand Down

0 comments on commit dc2c83d

Please sign in to comment.