public function testLibraryWithDeps()
 {
     $lib1 = new Mpw\V8\JsLib('one', 'var a = b;');
     $lib2 = new Mpw\V8\JsLib('two', 'var b = true');
     $lib3 = new Mpw\V8\JsLib('three', 'var c = false');
     $lib1->addDep($lib2);
     $v8Wrapper = new Mpw\V8\V8Wrapper($lib1, $lib2, $lib3);
     $this->assertTrue($v8Wrapper->execJs($this->jsCodeFactory('a == b == true;')));
 }
Example #2
0
 public function testDep()
 {
     $lib = new Mpw\V8\JsLib('test', 'true;');
     $lib->addDep(new Mpw\V8\JsLib('test2', 'false;'));
     $this->assertEquals($lib->getDeps(), ['test2']);
 }
Example #3
0
<?php

declare (strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
// Get the bundle configuration shared with webpack
$bundleConfig = json_decode(file_get_contents(__DIR__ . '/entry-output.json'));
// Webpack uses `window` as a global
$browserizer = new Mpw\V8\JsLib('browserizer', 'var window = {};');
// Here's the ES5-ish bundle that works with browsers and V8
$componentBundle = new Mpw\V8\JsLib('components', file_get_contents(__DIR__ . '/components.build.js'));
// Ensure that the browserization happens before the component bundle execs
$componentBundle->addDep($browserizer);
// Wraps V8 for simplicity
$v8W = new Mpw\V8\V8Wrapper($componentBundle, $browserizer);
$componentFactory = new Mpw\V8\ReactComponentFactory($v8W, $bundleConfig->output->library);
// Create a new component
$personComponentHtml = $componentFactory->renderComponent('Person', ['firstName' => 'Matt']);
$peopleData = ['people' => [['firstName' => 'Matt'], ['firstName' => 'Paulie']], 'lastName' => 'Wells'];
$peopleComponentHtml = $componentFactory->renderComponent('Family', $peopleData);
// Add more data to people array to show frontend render
$peopleData['people'][] = ['firstName' => 'Zora'];
?>
<!DOCTYPE html>
<html>
<head>
    <title>Server-side rendering</title>
</head>
<body>
<script>
<?php 
echo $componentBundle->getSrc();