Skip to content

Commit

Permalink
feat: wireup ssr.php and component.php
Browse files Browse the repository at this point in the history
[-] wireup ssr.php and component.php
[-] apply co-compile to debug scripts
[-] apply co-compile to integration test
  • Loading branch information
harttle committed Sep 17, 2019
1 parent e391cef commit 06e1ba4
Show file tree
Hide file tree
Showing 49 changed files with 318 additions and 1,159 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules/
npm-debug.log
/test/e2e.spec.js
coverage
vendor/
vendor/
dist/
3 changes: 2 additions & 1 deletion bin/auto-complete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#
# 3. Input `./bin/test.ts` and press <Tab>

compdef _test test.ts
compdef _test test-php.ts
compdef _test test-js.ts
compdef _test render.ts
compdef _test compile-to-js.ts
compdef _test compile-to-php.ts
Expand Down
18 changes: 0 additions & 18 deletions bin/compile-to-js.ts

This file was deleted.

18 changes: 0 additions & 18 deletions bin/compile-to-php.ts

This file was deleted.

4 changes: 2 additions & 2 deletions bin/render.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/usr/bin/env node

const { readFileSync } = require('fs')
const { resolve, join } = require('path')
const { read } = require('../test/data')

const caseName = process.argv[2]
const caseRoot = resolve(__dirname, '../test/cases')
const caseDir = resolve(caseRoot, caseName)
const jsSSRPath = join(caseDir, 'ssr.js')
const dataPath = join(caseDir, 'data.json')

const data = read(dataPath)
const data = JSON.parse(readFileSync(dataPath, 'utf8'))
const noDataOutput = /-ndo$/.test(caseDir)
const jsRendered = require(jsSSRPath)(data, noDataOutput)
process.stdout.write(jsRendered)
12 changes: 11 additions & 1 deletion bin/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

$dataStr = file_get_contents($caseDir . "/data.json");
$data = json_decode($dataStr);

$noDataOutput = preg_match('/-ndo$/', $caseName);
$renderFunc = '\\san\\renderer\\' . dashesToCamelCase($caseName) . '\\render';

echo $renderFunc($data, $noDataOutput);

echo $render($data, $noDataOutput);
function dashesToCamelCase($string, $capitalizeFirstCharacter = false) {
$str = str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));
if (!$capitalizeFirstCharacter) {
$str[0] = strtolower($str[0]);
}
return $str;
}
28 changes: 0 additions & 28 deletions bin/test.ts

This file was deleted.

3 changes: 1 addition & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module.exports = {
roots: [
'<rootDir>/src',
'<rootDir>/test'
],
testMatch: [
'<rootDir>/test/unit/**.ts',
'<rootDir>/test/integration.spec.ts'
'<rootDir>/test/integration.spec.js'
],
globals: {
tsConfig: {
Expand Down
27 changes: 24 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
},
"scripts": {
"lint": "eslint src test",
"build": "tsc && cp runtime/* dist/runtime",
"build": "tsc",
"build-watch": "tsc",
"unit-php": "./vendor/bin/phpunit --bootstrap vendor/autoload.php test/unit/*.spec.php",
"unit-ts": "jest test/unit",
"unit": "npm run unit-ts && npm run unit-php",
"integration": "jest test/integration.spec.ts",
"integration": "jest test/integration.spec.js",
"coverage": "jest --coverage",
"test": "jest && npm run e2e",
"render": "./bin/render.js",
Expand Down Expand Up @@ -62,7 +63,8 @@
"dependencies": {
"camelcase": "^5.3.1",
"chalk": "^2.4.2",
"ts-morph": "^4.0.0",
"snake-case": "^2.1.0",
"ts-morph": "^4.0.1",
"ts-node": "^8.3.0",
"ts2php": "^0.12.1",
"typescript": "^3.4.5"
Expand Down
9 changes: 5 additions & 4 deletions runtime/underscore.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ private static function boolAttrTruthy($value) {

public static function callFilter($ctx, $name, $args)
{
$filter = $ctx["proto"]["filters"][$name];
// TODO this is
if (is_callable($filter)) {
return call_user_func_array($filter, $args);
$cid = $ctx["spsrId"];
$cls = \san\runtime\ComponentRegistry::get($cid);
$func = $cls::$filters[$name];
if (is_callable($func)) {
return call_user_func_array($func, $args);
}
}

Expand Down
70 changes: 70 additions & 0 deletions src/bin/case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { exec } from './exec'
import { ComponentParser } from '../transpilers/component-parser'
import { readdirSync, writeFileSync, existsSync } from 'fs'
import { resolve, join } from 'path'
import { compileToSource as compileToJSSource } from '../js-ssr'
import { compileToSource as compileToPHPSource } from '../php-ssr'
import { Compiler as ToPHPCompiler } from '../transpilers/ts2php'
import { Compiler as ToJSCompiler } from '../transpilers/ts2js'
import camelCase from 'camelcase'

const caseRoot = resolve(__dirname, '../../test/cases')
const tsconfigPath = resolve(__dirname, '../../test/tsconfig.json')
const cases = readdirSync(caseRoot)

export function compileToJS (caseName) {
const caseDir = join(caseRoot, caseName)
const ts = join(caseDir, 'component.ts')
const js = join(caseDir, 'component.js')
const componentClass = existsSync(ts) ? require(ts).default : require(js)
const fn = compileToJSSource(componentClass)
writeFileSync(join(caseDir, 'ssr.js'), `module.exports = ${fn}`)
}

export function compileAllToJS () {
for (const caseName of cases) compileToJS(caseName)
}

export function compileToPHP (caseName) {
const caseDir = join(caseRoot, caseName)
const ts = join(caseDir, 'component.ts')
let componentClass
let code = ''

if (existsSync(ts)) {
const ccp = new ToPHPCompiler({
tsconfigPath,
nsPrefix: 'san\\components\\test\\'
})
const component = new ComponentParser(ts, tsconfigPath).parseComponent()
const ccj = new ToJSCompiler(tsconfigPath)
componentClass = ccj.compileAndRun(component.get(ts))['default']
code += ccp.compileComponent(component)
} else {
const js = join(caseDir, 'component.js')
componentClass = require(js)
}

const renderCode = compileToPHPSource(
componentClass,
{ funcName: 'render' }
)

code +=
`namespace san\\renderer\\${camelCase(caseName)} {\n` +
` ${renderCode}` +
`}\n`
writeFileSync(join(caseDir, 'ssr.php'), `<?php ${code} ?>`)
}

export function compileAllToPHP () {
for (const caseName of cases) compileToPHP(caseName)
}

export function renderByJS (caseName) {
return exec(resolve(__dirname, `../../bin/render.js`), [caseName])
}

export function renderByPHP (caseName) {
return exec(resolve(__dirname, `../../bin/render.php`), [caseName])
}
5 changes: 5 additions & 0 deletions src/bin/compile-to-js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { compileAllToJS, compileToJS } from './case'

const caseName = process.argv[2]
if (caseName === '--all') compileAllToJS()
else compileToJS(caseName)
6 changes: 6 additions & 0 deletions src/bin/compile-to-php.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { compileToPHP, compileAllToPHP } from './case'

const caseName = process.argv[2]

if (caseName === '--all') compileAllToPHP()
else compileToPHP(caseName)
File renamed without changes.
23 changes: 23 additions & 0 deletions src/bin/js-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import chalk from 'chalk'
import { readFileSync } from 'fs'
import { resolve } from 'path'
import { renderByJS, compileToJS } from './case'
import { measure, startMeasure } from '../utils/timing'

const caseName = process.argv[2]
const htmlPath = resolve(__dirname, '../test/cases', caseName, 'result.html')
const expected = readFileSync(htmlPath, 'utf8')

console.log(chalk.green('[COMP JS]'), measure(() => compileToJS(caseName)))

check(`[EXPECTED] ${caseName}`, () => expected)
check(`[SSR JS] ${caseName}`, () => renderByJS(caseName))

function check (title, render) {
const renderJSMeasure = startMeasure()
const html = render()
const color = html === expected ? 'green' : 'red'
console.log(chalk[color](title), renderJSMeasure.duration())
console.log(html + '\n')
if (html !== expected) process.exit(1)
}
23 changes: 23 additions & 0 deletions src/bin/php-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import chalk from 'chalk'
import { readFileSync } from 'fs'
import { resolve } from 'path'
import { renderByPHP, compileToPHP } from './case'
import { measure, startMeasure } from '../utils/timing'

const caseName = process.argv[2]
const htmlPath = resolve(__dirname, '../test/cases', caseName, 'result.html')
const expected = readFileSync(htmlPath, 'utf8')

console.log(chalk.green('[COMP PHP]'), measure(() => compileToPHP(caseName)))

check(`[EXPECTED] ${caseName}`, () => expected)
check(`[SSR PHP] ${caseName}`, () => renderByPHP(caseName))

function check (title, render) {
const renderJSMeasure = startMeasure()
const html = render()
const color = html === expected ? 'green' : 'red'
console.log(chalk[color](title), renderJSMeasure.duration())
console.log(html + '\n')
if (html !== expected) process.exit(1)
}
2 changes: 1 addition & 1 deletion src/js-ssr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { each, contains, empty, extend, bind, inherits } from './utils'
import { each, contains, empty, extend, bind, inherits } from './utils/underscore'
import * as fs from 'fs'
import * as path from 'path'

Expand Down
Loading

0 comments on commit 06e1ba4

Please sign in to comment.