From c2bfe21c7a8f240e8b10169cf01c0b61ca50a786 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 12 Sep 2018 22:00:18 -0500 Subject: [PATCH 1/2] Added ability to version assets for browser cache busting --- assets/templates/_header.phtml | 4 ++-- config.php | 11 +++++++++++ src/util/functions.php | 21 +++++++++++++++++++++ test/FunctionsTest.php | 30 ++++++++++++++++++++++++++++++ webpack.config.js | 3 ++- 5 files changed, 66 insertions(+), 3 deletions(-) diff --git a/assets/templates/_header.phtml b/assets/templates/_header.phtml index 9b8a2c8..c8a716d 100644 --- a/assets/templates/_header.phtml +++ b/assets/templates/_header.phtml @@ -8,9 +8,9 @@ - + - + diff --git a/config.php b/config.php index 8ae6801..4517e63 100644 --- a/config.php +++ b/config.php @@ -3,7 +3,18 @@ use Apprentice\Page; return [ + /* + * + * Directory for SVG icons that can be used in templates + * + */ 'icon_dir' => __DIR__ . '/assets/icons', + + /* + * + * Directory holding code files used in examples + * + */ 'code_dir' => __DIR__ . '/code', 'templates_dir' => __DIR__ . '/assets/templates', 'output_dir' => __DIR__ . '/.build', diff --git a/src/util/functions.php b/src/util/functions.php index 66d0559..ee93b74 100644 --- a/src/util/functions.php +++ b/src/util/functions.php @@ -147,3 +147,24 @@ function config(string $key) { return $config[$key] ?? null; } + +/** + * Returns path to asset based on manifes.json file + * + * @param string $name + * @return string + */ +function asset(string $name): string { + $outputDir = config('output_dir'); + + if (file_exists($outputDir . '/manifest.json')) { + $text = file_get_contents($outputDir . '/manifest.json'); + $paths = json_decode($text, true); + + if (isset($paths[$name])) { + return $paths[$name]; + } + } + + return '/' . $name; +} diff --git a/test/FunctionsTest.php b/test/FunctionsTest.php index e5aee24..de71240 100644 --- a/test/FunctionsTest.php +++ b/test/FunctionsTest.php @@ -4,9 +4,20 @@ namespace Test; class FunctionsTest extends BaseTestCase { + public function setUp() + { + mkdir('/tmp/apprentice_output'); + } + public function tearDown() { $GLOBALS['PARTIAL_TEST'] = null; + + $files = glob('/tmp/apprentice_output/*'); + foreach ($files as $file) { + unlink($file); + } + rmdir('/tmp/apprentice_output'); } public function test_load_config() @@ -56,4 +67,23 @@ class FunctionsTest extends BaseTestCase $this->assertEquals('test var', $GLOBALS['PARTIAL_TEST']); } + + public function test_asset_path_with_manifest() + { + file_put_contents( + '/tmp/apprentice_output/manifest.json', + json_encode(['js/app.js' => '/js/app-1234.js']) + ); + + $path = asset('js/app.js'); + + $this->assertEquals('/js/app-1234.js', $path); + } + + public function test_asset_path_without_manifest() + { + $path = asset('js/app.js'); + + $this->assertEquals('/js/app.js', $path); + } } diff --git a/webpack.config.js b/webpack.config.js index 54ce9e1..9e90541 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -11,7 +11,8 @@ Encore "languages": ["php"], "css": false, }]); - }); + }) + .enableVersioning(); ; module.exports = Encore.getWebpackConfig(); From 345d5f17bb5463f1cb6229b931dbb8e45719b3e1 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 12 Sep 2018 22:02:56 -0500 Subject: [PATCH 2/2] Finished commenting config file --- config.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/config.php b/config.php index 4517e63..39cc883 100644 --- a/config.php +++ b/config.php @@ -16,9 +16,33 @@ return [ * */ 'code_dir' => __DIR__ . '/code', + + /* + * + * Directory to PHP templates used by pages + * + */ 'templates_dir' => __DIR__ . '/assets/templates', + + /* + * + * Output directory for html files and assets + * + */ 'output_dir' => __DIR__ . '/.build', + + /* + * + * Static files that should be loaded into output directory + * + */ 'files_dir' => __DIR__ . '/assets/files', + + /* + * + * Configuration for all pages on the site + * + */ 'pages' => [ Page::create('index', 'index.phtml'), Page::create('installing-php', 'installing-php.phtml'),