|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class WebpackPageControllerExtension extends Extension |
|
|
|
|
|
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* you only need to set this if you have some themes that are enabled and some themes |
|
9
|
|
|
* that do not run webpack |
|
10
|
|
|
* @var {Array} |
|
11
|
|
|
*/ |
|
12
|
|
|
private static $webpack_enabled_themes = []; |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* override webpack server for custom set ups |
|
16
|
|
|
* set to true to make this class believe you are always running |
|
17
|
|
|
* the webpack server |
|
18
|
|
|
* @see IsWebpackDevServer |
|
19
|
|
|
* @var bool |
|
20
|
|
|
*/ |
|
21
|
|
|
private static $is_webpack_server = false; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* override webpack server for custom set ups |
|
25
|
|
|
* this is the server used for checking if the webpack server is running |
|
26
|
|
|
* @see IsWebpackDevServer |
|
27
|
|
|
* @var bool |
|
28
|
|
|
*/ |
|
29
|
|
|
private static $webpack_socket_server = 'localhost'; |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* usually this is set to current domain |
|
33
|
|
|
* only set if you need an alternative |
|
34
|
|
|
* @see: WebpackBaseURL |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private static $webpack_server = ''; |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* |
|
41
|
|
|
* @var int |
|
42
|
|
|
*/ |
|
43
|
|
|
private static $webpack_port = 3000; |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* this is the folder where the distilled files are placed. |
|
47
|
|
|
* If your theme is foo then you will find the distilled files in themes/foo_dist |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
private static $webpack_distribution_folder_extension = 'dist'; |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
|
|
public function IsNotWebpackDevServer() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->IsWebpackDevServer() ? false : true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
|
|
public function IsWebpackDevServer() |
|
67
|
|
|
{ |
|
68
|
|
|
$override = $this->owner->Config()->get('is_webpack_server'); |
|
69
|
|
|
if ($override) { |
|
70
|
|
|
return $override; |
|
71
|
|
|
} |
|
72
|
|
|
if (Director::isDev()) { |
|
73
|
|
|
$socket = @fsockopen( |
|
74
|
|
|
$this->owner->Config()->get('webpack_socket_server'), |
|
75
|
|
|
$this->owner->Config()->get('webpack_port'), |
|
76
|
|
|
$errno, |
|
77
|
|
|
$errstr, |
|
78
|
|
|
1 |
|
79
|
|
|
); |
|
80
|
|
|
return ! $socket ? false : true; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function WebpackBaseURL() |
|
90
|
|
|
{ |
|
91
|
|
|
$webpackServer = $this->owner->Config()->get('webpack_server'); |
|
92
|
|
|
if (! $webpackServer) { |
|
93
|
|
|
$webpackServer = Director::AbsoluteBaseURL('/'); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
if ($this->IsWebpackDevServer()) { |
|
96
|
|
|
$webpackServer = rtrim($webpackServer, '/') .':'.$this->owner->Config()->get('webpack_port').'/'; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $webpackServer; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function WebpackDistributionFolderExtension() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->owner->Config()->get('webpack_distribution_folder_extension'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $type should be set to JS or CSS |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
public function WebpackFileHash($type = 'JS') |
|
116
|
|
|
{ |
|
117
|
|
|
$base = Director::baseFolder(); |
|
118
|
|
|
if ($type === 'JS') { |
|
119
|
|
|
$file = 'bundle.js'; |
|
120
|
|
|
} elseif ($type === 'CSS') { |
|
121
|
|
|
$file = 'style.css'; |
|
122
|
|
|
} else { |
|
123
|
|
|
user_error('Please specify JS or CSS, '.$type.' specified.'); |
|
124
|
|
|
} |
|
125
|
|
|
$fullFile = $base.'/'.THEMES_DIR . "/" . Config::inst()->get('SSViewer', 'theme').'_'.$this->WebpackDistributionFolderExtension().'/'.$file; |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
return @filemtime($fullFile); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.