-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bb797d2
Showing
12 changed files
with
499 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.composer | ||
vendor | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# v1.0.0 | ||
## 27-04-2020 | ||
|
||
1. [](#new) | ||
* First release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2020 Maarten de Boer <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Webpack Manifest Plugin | ||
|
||
The **Webpack Manifest** Plugin is an extension for [Grav CMS](http://github.com/getgrav/grav). It allows you to develop themes that use for example Webpack that generate versioned assets (css/js). | ||
|
||
As these generated files have a unique name every time they are generated, you can't reference them in your template directly. Webpack outputs a manifest file which contains a mapping between the unversioned filename and versioned filename, | ||
which is used by this plugin to replace assets with the correct file. | ||
|
||
## Installation | ||
|
||
Installing the Webpack Manifest plugin can be done in one of three ways: The GPM (Grav Package Manager) installation method lets you quickly install the plugin with a simple terminal command, the manual method lets you do so via a zip file, and the admin method lets you do so via the Admin Plugin. | ||
|
||
### GPM Installation (Preferred) | ||
|
||
To install the plugin via the [GPM](http://learn.getgrav.org/advanced/grav-gpm), through your system's terminal (also called the command line), navigate to the root of your Grav-installation, and enter: | ||
|
||
bin/gpm install webpack-manifest | ||
|
||
This will install the Webpack Manifest plugin into your `/user/plugins`-directory within Grav. Its files can be found under `/your/site/grav/user/plugins/webpack-manifest`. | ||
|
||
### Manual Installation | ||
|
||
To install the plugin manually, download the zip-version of this repository and unzip it under `/your/site/grav/user/plugins`. Then rename the folder to `webpack-manifest`. You can find these files on [GitHub](https://github.com/cloudstek/grav-plugin-webpack-manifest) or via [GetGrav.org](http://getgrav.org/downloads/plugins#extras). | ||
|
||
You should now have all the plugin files under | ||
|
||
/your/site/grav/user/plugins/webpack-manifest | ||
|
||
> NOTE: This plugin is a modular component for Grav which may require other plugins to operate, please see its [blueprints.yaml-file on GitHub](https://github.com/cloudstek/grav-plugin-webpack-manifest/blob/master/blueprints.yaml). | ||
### Admin Plugin | ||
|
||
If you use the Admin Plugin, you can install the plugin directly by browsing the `Plugins`-menu and clicking on the `Add` button. | ||
|
||
## Configuration | ||
|
||
Before configuring this plugin, you should copy the `user/plugins/webpack-manifest/webpack-manifest.yaml` to `user/config/plugins/webpack-manifest.yaml` and only edit that copy. | ||
|
||
Here is the default configuration and an explanation of available options: | ||
|
||
```yaml | ||
# Enable/disable the plugin | ||
enabled: true | ||
# Path to manifest.json relative to your active theme | ||
# For webpack use manifest.json. For Laravel Mix use mix-manifest.json. | ||
filepath: manifest.json | ||
``` | ||
Note that if you use the Admin Plugin, a file with your configuration named webpack-manifest.yaml will be saved in the `user/config/plugins/`-folder once the configuration is saved in the Admin. | ||
|
||
## Usage | ||
|
||
Usage is simple as in most cases you can directly replace `{% do asset.css( ...` with `{% do manifest.css( ...`, the API is mostly the same and serves as a wrapper around some of the asset manager functionality, see https://learn.getgrav.org/16/themes/asset-manager. | ||
|
||
Supported: **add()**, **addCss()**, **addJs()** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
name: Webpack Manifest | ||
version: 1.0.0 | ||
description: Reference assets from your webpack manifest in your theme | ||
icon: plug | ||
author: | ||
name: Maarten de Boer | ||
email: [email protected] | ||
homepage: https://github.com/Cloudstek/grav-plugin-webpack-manifest | ||
keywords: webpack, mix, encore, manifest | ||
bugs: https://github.com/Cloudstek/grav-plugin-webpack-manifest/issues | ||
docs: https://github.com/Cloudstek/grav-plugin-webpack-manifest/blob/master/README.md | ||
license: MIT | ||
|
||
dependencies: | ||
- { name: grav, version: '>=1.6.0' } | ||
|
||
form: | ||
validation: loose | ||
fields: | ||
enabled: | ||
type: toggle | ||
label: PLUGIN_ADMIN.PLUGIN_STATUS | ||
highlight: 1 | ||
default: 0 | ||
options: | ||
1: PLUGIN_ADMIN.ENABLED | ||
0: PLUGIN_ADMIN.DISABLED | ||
validate: | ||
type: bool | ||
filepath: | ||
type: text | ||
default: manifest.json | ||
label: PLUGIN_WEBPACK_MANIFEST.FILEPATH | ||
help: PLUGIN_WEBPACK_MANIFEST.FILEPATH_HELP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Grav\Plugin\WebpackManifest; | ||
|
||
use Grav\Common\Assets; | ||
use Grav\Common\Config; | ||
use Grav\Common\Grav; | ||
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator; | ||
|
||
/** | ||
* Webpack Manifest Assets class. | ||
*/ | ||
class WebpackManifestAssets | ||
{ | ||
/** | ||
* Assets. | ||
* | ||
* @var Assets | ||
*/ | ||
private $assets; | ||
|
||
/** | ||
* Plugin config. | ||
* | ||
* @var Config\Config | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* Locator. | ||
* | ||
* @var UniformResourceLocator | ||
*/ | ||
private $locator; | ||
|
||
/** | ||
* Webpack manifest.. | ||
* | ||
* @var array|null | ||
*/ | ||
private $manifest; | ||
|
||
public function __construct() | ||
{ | ||
$grav = Grav::instance(); | ||
$this->assets = $grav['assets']; | ||
$this->locator = $grav['locator']; | ||
$this->config = $grav['config']['plugins.webpack-manifest']; | ||
|
||
// Determine manifest file path. | ||
$manifestPath = 'theme://manifest.json'; | ||
|
||
if (empty($this->config['filepath']) === false) { | ||
$manifestPath = 'theme://' . $this->config['filepath']; | ||
} | ||
|
||
$manifestPath = $this->locator->findResource($manifestPath); | ||
|
||
// Load manifest | ||
if ($manifestPath !== false) { | ||
$this->manifest = json_decode(file_get_contents($manifestPath), true); | ||
|
||
if ($this->manifest === null) { | ||
throw new \Exception( | ||
sprintf( | ||
'Could not decode webpack manifest file at %s: %s', | ||
$manifestPath, | ||
json_last_error_msg() | ||
) | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Add an asset or a collection of assets. | ||
* | ||
* It automatically detects the asset type (JavaScript, CSS or collection). | ||
* You may add more than one asset passing an array as argument. | ||
* | ||
* @param array|string $asset | ||
* | ||
* @return $this | ||
*/ | ||
public function add($asset) | ||
{ | ||
// Skip processing if no manifest file could be found. | ||
if ($this->manifest === null) { | ||
$this->assets->add($asset); | ||
|
||
return $this; | ||
} | ||
|
||
// Function arguments. | ||
$args = func_get_args(); | ||
|
||
// Handle recursion for multiple assets. | ||
$handledMultiple = $this->handleMultipleAssets($asset, [$this, 'add'], $args); | ||
|
||
if ($handledMultiple === true) { | ||
return $this; | ||
} | ||
|
||
// Add asset. | ||
$extension = pathinfo(parse_url($asset, PHP_URL_PATH), PATHINFO_EXTENSION); | ||
|
||
if (empty($extension) === false) { | ||
switch (strtolower($extension)) { | ||
case 'css': | ||
call_user_func_array([$this, 'addCss'], $args); | ||
break; | ||
case 'js': | ||
call_user_func_array([$this, 'addJs'], $args); | ||
break; | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Add a CSS asset or a collection of assets. | ||
* | ||
* @param array|string $asset | ||
* | ||
* @see Assets::addCss() | ||
* | ||
* @return $this | ||
*/ | ||
public function addCss($asset) | ||
{ | ||
// Skip processing if no manifest file could be found. | ||
if ($this->manifest === null) { | ||
$this->assets->addCss($asset); | ||
|
||
return $this; | ||
} | ||
|
||
// Function arguments. | ||
$args = func_get_args(); | ||
|
||
// Handle recursion for multiple assets. | ||
$handledMultiple = $this->handleMultipleAssets($asset, [$this, 'addCss'], $args); | ||
|
||
if ($handledMultiple === true) { | ||
return $this; | ||
} | ||
|
||
// Handle single asset. | ||
$assetPath = $this->locator->findResource($asset, false); | ||
|
||
// Replace the original file path with the located file path from our manifest | ||
if ($assetPath !== false && isset($this->manifest[$assetPath])) { | ||
$args[0] = $this->manifest[$assetPath]; | ||
} | ||
|
||
call_user_func_array([$this->assets, 'addCss'], $args); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Add a JS asset or a collection of assets. | ||
* | ||
* @param array|string $asset | ||
* | ||
* @see Assets::addJs() | ||
* | ||
* @return $this | ||
*/ | ||
public function addJs($asset) | ||
{ | ||
// Skip processing if no manifest file could be found. | ||
if ($this->manifest === null) { | ||
$this->assets->addJs($asset); | ||
|
||
return $this; | ||
} | ||
|
||
// Function arguments. | ||
$args = func_get_args(); | ||
|
||
// Handle recursion for multiple assets. | ||
$handledMultiple = $this->handleMultipleAssets($asset, [$this, 'addJs'], $args); | ||
|
||
if ($handledMultiple === true) { | ||
return $this; | ||
} | ||
|
||
// Handle single asset. | ||
$assetPath = $this->locator->findResource($asset, false); | ||
|
||
// Replace the original file path with the located file path from our manifest | ||
if ($assetPath !== false && isset($this->manifest[$assetPath])) { | ||
$args[0] = $this->manifest[$assetPath]; | ||
} | ||
|
||
call_user_func_array([$this->assets, 'addJs'], $args); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Handle recursion for multiple assets. | ||
* | ||
* @param string|array $assets | ||
* @param callable $cb | ||
* @param array $args | ||
* | ||
* @return bool True on multiple assets, false on single asset. | ||
*/ | ||
private function handleMultipleAssets($assets, $cb, $args) | ||
{ | ||
// Handle multiple assets or a colleciton of assets | ||
if (is_array($assets)) { | ||
foreach ($assets as $asset) { | ||
array_shift($args); | ||
$args = array_merge([$asset], $args); | ||
call_user_func_array($cb, $args); | ||
} | ||
|
||
return true; | ||
} elseif (isset($this->assets->getCollections()[$assets])) { | ||
array_shift($args); | ||
$args = array_merge([$this->assets->getCollections()[$assets]], $args); | ||
call_user_func_array($cb, $args); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Grav\Plugin\WebpackManifest; | ||
|
||
use Twig; | ||
|
||
/** | ||
* Webpack Manifest Twig extension. | ||
*/ | ||
class WebpackManifestTwigExtension extends Twig\Extension\AbstractExtension | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getGlobals() | ||
{ | ||
return [ | ||
'manifest' => new WebpackManifestAssets() | ||
]; | ||
} | ||
} |
Oops, something went wrong.