diff --git a/package.json b/package.json index edd9e344..7a14db54 100644 --- a/package.json +++ b/package.json @@ -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 ", "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", diff --git a/src/bin/ssr.ts b/src/bin/ssr.ts index 53313ae8..93577619 100755 --- a/src/bin/ssr.ts +++ b/src/bin/ssr.ts @@ -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' @@ -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`) diff --git a/src/compilers/to-js-compiler.ts b/src/compilers/to-js-compiler.ts index f428d9cb..3257eaf0 100644 --- a/src/compilers/to-js-compiler.ts +++ b/src/compilers/to-js-compiler.ts @@ -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') @@ -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'], () => { @@ -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'], () => { @@ -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`) @@ -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 }) diff --git a/src/compilers/to-php-compiler.ts b/src/compilers/to-php-compiler.ts index 42ccd935..b7679f24 100644 --- a/src/compilers/to-php-compiler.ts +++ b/src/compilers/to-php-compiler.ts @@ -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', diff --git a/src/utils/case.ts b/src/utils/case.ts index 560fdab0..3b6f80fb 100644 --- a/src/utils/case.ts +++ b/src/utils/case.ts @@ -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) }