From 7b5e2c5994ba9374d734821d55c738b74d3554cf Mon Sep 17 00:00:00 2001 From: Chris Kankiewicz Date: Tue, 7 Jan 2020 11:54:23 -0700 Subject: [PATCH] Asset compilation improvements --- app/Providers/TwigProvider.php | 2 +- app/ViewFunctions/Asset.php | 22 +++++++++++++++++++++- app/ViewFunctions/ViewFunction.php | 7 ++++++- package-lock.json | 14 ++++---------- package.json | 2 +- webpack.mix.js | 15 ++++++++++----- 6 files changed, 43 insertions(+), 19 deletions(-) diff --git a/app/Providers/TwigProvider.php b/app/Providers/TwigProvider.php index d046e35..7870209 100644 --- a/app/Providers/TwigProvider.php +++ b/app/Providers/TwigProvider.php @@ -56,7 +56,7 @@ class TwigProvider ); foreach (self::VIEW_FUNCTIONS as $function) { - $function = new $function($this->config); + $function = new $function($this->container, $this->config); $twig->getEnvironment()->addFunction( new TwigFunction($function->name(), $function) diff --git a/app/ViewFunctions/Asset.php b/app/ViewFunctions/Asset.php index d095233..ac9d5ef 100644 --- a/app/ViewFunctions/Asset.php +++ b/app/ViewFunctions/Asset.php @@ -2,6 +2,8 @@ namespace App\ViewFunctions; +use Tightenco\Collect\Support\Collection; + class Asset extends ViewFunction { /** @const Constant description */ @@ -19,6 +21,24 @@ class Asset extends ViewFunction */ public function __invoke(string $path): string { - return self::ASSET_PATH . $path; + $assetPath = self::ASSET_PATH . $path; + + if ($this->mixManifest()->has($assetPath)) { + return $this->mixManifest()->get($assetPath); + } + + return $assetPath; + } + + /** + * Return the mix manifest collection. + * + * @return \Tightenco\Collect\Support\Collection + */ + protected function mixManifest(): Collection + { + return Collection::make(json_decode(file_get_contents( + $this->container->get('base_path') . '/mix-manifest.json' + ), true)); } } diff --git a/app/ViewFunctions/ViewFunction.php b/app/ViewFunctions/ViewFunction.php index c8b37cc..0ade628 100644 --- a/app/ViewFunctions/ViewFunction.php +++ b/app/ViewFunctions/ViewFunction.php @@ -2,6 +2,7 @@ namespace App\ViewFunctions; +use DI\Container; use PHLAK\Config\Config; abstract class ViewFunction @@ -9,6 +10,9 @@ abstract class ViewFunction /** @var string The function name */ protected $name = ''; + /** @var Container The application container */ + protected $container; + /** @var Config App configuration component */ protected $config; @@ -17,8 +21,9 @@ abstract class ViewFunction * * @param \PHLAK\Config\Config $config */ - public function __construct(Config $config) + public function __construct(Container $container, Config $config) { + $this->container = $container; $this->config = $config; } diff --git a/package-lock.json b/package-lock.json index 2f7654d..9dcbdf7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1464,12 +1464,11 @@ } }, "axios": { - "version": "0.19.0", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.0.tgz", - "integrity": "sha512-1uvKqKQta3KBxIz14F2v06AEHZ/dIoeKfbTRkK1E5oqjDnuEerLmYTgJB5AiQZHJcljpg1TuRzdjDR06qNk0DQ==", + "version": "0.19.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.1.tgz", + "integrity": "sha512-Yl+7nfreYKaLRvAvjNPkvfjnQHJM1yLBY3zhqAwcJSwR/6ETkanUgylgtIvkvz0xJ+p/vZuNw8X7Hnb7Whsbpw==", "requires": { - "follow-redirects": "1.5.10", - "is-buffer": "^2.0.2" + "follow-redirects": "1.5.10" }, "dependencies": { "debug": { @@ -1488,11 +1487,6 @@ "debug": "=3.1.0" } }, - "is-buffer": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.4.tgz", - "integrity": "sha512-Kq1rokWXOPXWuaMAqZiJW4XxsmD9zGx9q4aePabbn3qCRGedtH7Cm+zV8WETitMfu1wdh+Rvd6w5egwSngUX2A==" - }, "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", diff --git a/package.json b/package.json index 55e7e92..af6c62e 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js" }, "dependencies": { - "axios": "^0.19.0", + "axios": "^0.19.1", "vue": "^2.6.11" }, "devDependencies": { diff --git a/webpack.mix.js b/webpack.mix.js index fb8aa18..6e6870d 100644 --- a/webpack.mix.js +++ b/webpack.mix.js @@ -2,12 +2,14 @@ let mix = require('laravel-mix'); let tailwindcss = require('tailwindcss'); require('laravel-mix-purgecss'); -mix.sass('app/resources/sass/app.scss', 'app/dist').options({ +mix.setPublicPath(path.resolve('.')); + +mix.sass('app/resources/sass/app.scss', 'app/dist/app.css').options({ processCssUrls: false, postCss: [tailwindcss('tailwind.config.js')] }); -mix.js('app/resources/js/app.js', 'app/dist'); +mix.js('app/resources/js/app.js', 'app/dist/app.js'); mix.copyDirectory( 'node_modules/@fortawesome/fontawesome-free/webfonts', @@ -16,8 +18,11 @@ mix.copyDirectory( mix.purgeCss({ extensions: ["html", "js", "php", "scss", "twig", "vue"], - globs: ["**/*.php", "**/*.scss", "**/*.twig"], - folders: ["src"], - whitelist: ["html", "body", "main", "fab", "far", "fas", "fal", "fad"], + folders: ["app"], + whitelist: ["html", "body", "main", "fab", "fad", "fal", "far", "fas"], whitelistPatterns: [/^fa\-/] }); + +if (mix.inProduction()) { + mix.version(); +}