Skip to content

Commit

Permalink
[GEST-712] Implementando Webpack Encore
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoapaes committed Jun 25, 2021
1 parent 8e67601 commit 1fff281
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ desktop.ini
node_modules/
public/
assets/
webpack.config.js
webpack.config.js
tests/template-compile/
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"like/json": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^5.0 || ^9.0"
"phpunit/phpunit": "^5.0 || ^9.0",
"smarty/smarty": "^3.1"
},
"autoload": {
"psr-4": {
Expand Down
65 changes: 64 additions & 1 deletion composer.lock

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

14 changes: 13 additions & 1 deletion src/ReadEntrypoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ReadEntrypoints {
/**
* @var array
*/
private $entrypoints;
private $entrypoints = [];

const PUBLIC_ASSETS = 'public/build';
const ENTRYPOINTS = 'entrypoints.json';
Expand All @@ -27,6 +27,10 @@ private function __construct($path) {

public function read() {
$file = $this->getRaiz() . '/../' . $this->path . '/' . self::ENTRYPOINTS;
if (! file_exists($file)) {
return;
}

$json = file_get_contents($file);
$this->entrypoints = Decode::decode($json);
}
Expand All @@ -37,6 +41,14 @@ private function getRaiz() {
}

public function getEntrypoint($type, $name='app') {
if (! isset($this->entrypoints['entrypoints'])) {
return [];
}

if (! isset($this->entrypoints['entrypoints'][$name])) {
return [];
}

return $this->entrypoints['entrypoints'][$name][$type];
}

Expand Down
16 changes: 16 additions & 0 deletions src/SmartyTemplateIntegration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace WebpackEncore;

use Smarty;

class SmartyTemplateIntegration {
public static function addFunctions(Smarty $smarty, ReadEntrypoints $readEntrypoints) {
$smarty->registerPlugin('function', 'webpack_encore_css', function ($params) use ($readEntrypoints) {
return $readEntrypoints->getCssTags($params['name'] ?: 'app');
});
$smarty->registerPlugin('function', 'webpack_encore_js', function ($params) use ($readEntrypoints) {
return $readEntrypoints->getJsTags($params['name'] ?: 'app');
});
}
}
26 changes: 26 additions & 0 deletions tests/ReadEntrypointsErrorsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Like\NomeDaLib\Tests;

use WebpackEncore\ReadEntrypoints;
use PHPUnit\Framework\TestCase;

class ReadEntrypointsErrorsTest extends TestCase {
public function testFileNotFound() {
$readEntrypoints = ReadEntrypoints::get('inexistent-folder');
$this->assertEquals([], $readEntrypoints->getCss());
$this->assertEquals([], $readEntrypoints->getJs());
}

public function testEmpty() {
$readEntrypoints = ReadEntrypoints::get('tests/empty');
$this->assertEquals([], $readEntrypoints->getCss());
$this->assertEquals([], $readEntrypoints->getJs());
}

public function testEmpty2() {
$readEntrypoints = ReadEntrypoints::get('tests/empty2');
$this->assertEquals([], $readEntrypoints->getCss());
$this->assertEquals([], $readEntrypoints->getJs());
}
}
2 changes: 1 addition & 1 deletion tests/ReadEntrypointsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ReadEntrypointsTest extends TestCase {
private $instance;

public function setUp() {
$this->instance = ReadEntrypoints::get('tests');
$this->instance = ReadEntrypoints::get('tests/valid');
}

public function testInstance() {
Expand Down
28 changes: 28 additions & 0 deletions tests/SmartyTemplateIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Like\NomeDaLib\Tests;

use PHPUnit\Framework\TestCase;
use Smarty;
use WebpackEncore\ReadEntrypoints;
use WebpackEncore\SmartyTemplateIntegration;

class SmartyTemplateIntegrationTest extends TestCase {
public function testUseTemplate() {
$readEntrypoints = ReadEntrypoints::get('tests/valid');
$smarty = new Smarty();
SmartyTemplateIntegration::addFunctions($smarty, $readEntrypoints);

$smarty->addTemplateDir(__DIR__ . '/./template/');
$smarty->compile_dir = __DIR__ . '/./template-compile/';
$this->assertEquals('<html>
<head>
<title>Teste</title>
<link href="/build/app.css" rel="stylesheet">
</head>
<body>
<script type="text/javascript" src="/build/runtime.js"></script><script type="text/javascript" src="/build/app.js"></script>
</body>
</html>', $smarty->fetch('index.tpl'));
}
}
8 changes: 8 additions & 0 deletions tests/empty/entrypoints.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"entrypoints": {
"app": {
"js": [],
"css": []
}
}
}
5 changes: 5 additions & 0 deletions tests/empty2/entrypoints.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"entrypoints": {
"app": {}
}
}
9 changes: 9 additions & 0 deletions tests/template/index.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<title>Teste</title>
{webpack_encore_css}
</head>
<body>
{webpack_encore_js}
</body>
</html>
File renamed without changes.

0 comments on commit 1fff281

Please sign in to comment.