-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebpackAssets.php
81 lines (68 loc) · 2.59 KB
/
WebpackAssets.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
// namespace CIC\WebpackAssets\Plugin;
/**
* Class WebpackAssets
* @package CIC\WebpackAssets\Plugin
*/
class WebpackAssets {
// Class defaults
protected $assetDir = '/build/';
protected $fileName = 'assets-manifest';
protected $phpClassName = 'WebpackBuiltFiles';
protected $entry;
public function __construct($options = []) {
$this->assetDir = get_template_directory().$this->assetDir;
$this->assetDir = array_key_exists('dir', $options) ?
$options['dir'] : $this->assetDir;
$this->fileName = array_key_exists('fileName', $options) ?
$options['fileName'] : $this->fileName;
$this->phpClassName = array_key_exists('phpClassName', $options) ?
$options['phpClassName'] : $this->phpClassName;
$this->entry = array_key_exists('entry', $options) ? $options['entry'] : null;
}
protected function assetManifestPath() {
// Append subdir, even if it's a blank string
// This is deprecated.
// $path = $this->assetDir.$this->subDir;
// Conditionally append a slash if the user didn't provide one in their subdir
// $path = (substr($path, -1) == '/') ? $path : $path.'/';
return $this->assetDir.$this->fileName.'.php';
}
protected function loadAssetsManifest() {
require_once($this->assetManifestPath());
}
public function getAssets($type) {
$manifestClass = $this->phpClassName;
// Require assets manifest once before outputting assets
$this->loadAssetsManifest();
// Check for php classname in document and return the stuff!
if(!class_exists($manifestClass)) {
throw new Exception('Could not load class ' . $manifestClass . ' from asset manifest file ' .
$this->assetManifestPath());
}
$assetListVar = $type.'Files';
$all = $manifestClass::$$assetListVar;
if ($this->entry) {
return array_filter($all, function($entry) {
if ($entry === $this->entry) return true;
if ($entry === "webpack-dev-server") return true;
return false;
}, ARRAY_FILTER_USE_KEY);
}
return $all;
}
protected function css($files) {
return implode("\n", array_map(function ($file) {
return '<link rel="stylesheet" href="'.$file.'">';
}, $files));
}
protected function js($files) {
return implode("\n", array_map(function ($file) {
return '<script src="'.$file.'"></script>';
}, $files));
}
public function outputAssets($type) {
$files = $this->getAssets($type);
return $this->$type($files);
}
}