-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScss.module.php
176 lines (157 loc) · 4.72 KB
/
Scss.module.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace ProcessWire;
use ScssPhp\ScssPhp\Compiler;
use ScssPhp\ScssPhp\OutputStyle;
/**
* @author Bernhard Baumrock, 20.10.2022
* @license Licensed under MIT
* @link https://www.baumrock.com
*/
class Scss extends WireData implements Module
{
/** @var Compiler */
private $compiler;
/**
* Compile input file to output file
*
* $output can be left empty, then it will take the input file and replace
* the .scss ending with .css ending!
*
* $style can either be 'compressed' or 'expanded'
*/
public function compile(
string $input,
string $output = null,
string $style = 'compressed',
): void {
// auto-create output path if no custom one is set
if (!$output) $output = substr($input, 0, -4) . "css";
// get scss content of input file
$content = $this->wire->files->fileGetContents($input);
// get compiler and set output style
$compiler = $this->compiler();
$compiler->setOutputStyle($style);
// write CSS to file
$css = $compiler->compileString($content)->getCss();
$this->wire->files->filePutContents($output, $css);
}
/**
* Compile input file to output file if any watched file is newer than output
*
*
* Returns TRUE if file has been compiled
* and FALSE if nothing was done
*/
public function compileIfChanged(
string $input,
array $watch,
string $output = null,
string $style = 'compressed',
): bool {
if (!is_file($input)) throw new WireException("$input not found");
// auto-create output path if no custom one is set
if (!$output) $output = substr($input, 0, -4) . "css";
// get modified timestamp of output file
$mCSS = 0;
if (is_file($output)) $mCSS = filemtime($output);
// loop all watched files
// as soon if we find one that is newer than the output we compile new
foreach ($watch as $file) {
if (filemtime($file) <= $mCSS) continue;
$this->compile($input, $output, $style);
return true;
}
return false;
}
/**
* Get the compiler instance
*/
public function compiler(): Compiler
{
if ($this->compiler) return $this->compiler;
require_once __DIR__ . "/vendor/autoload.php";
$this->compiler = new Compiler();
return $this->compiler;
}
/**
* Compile function to use with RockFrontend
*
* $output can be left empty, then it will take the input file and replace
* the .scss ending with .css ending!
*
* @throws \ScssPhp\ScssPhp\Exception\SassException
*/
public function compileRF(
string $input,
string $output,
string $cssPath,
string $import,
string $style = 'compressed',
string $SourcemapFile = '',
): void {
// auto-create output path if no custom one is set
if (!$output) $output = substr($input, 0, -4) . "css";
// get scss content of input file
$content = '@import "' . $input . '"';
// get compiler and set output style
$compiler = $this->compiler();
$compiler->setImportPaths($import);
if ($SourcemapFile) {
$compiler->setSourceMap(Compiler::SOURCE_MAP_FILE);
$compiler->setSourceMapOptions([
'sourceMapWriteTo' => $SourcemapFile,
'sourceMapURL' => $SourcemapFile,
'sourceMapFilename' => $output,
'sourceMapBasepath' => $cssPath,
]);
}
$compiler->setOutputStyle($style);
// write CSS to file
try {
$css = $compiler->compileString($content)->getCss();
$this->wire->files->filePutContents($output, $css);
} catch (\Exception $e) {
$this->log($e->getMessage());
if (wire()->modules->isInstalled('TracyDebugger')) {
bd($e);
}
}
// write SourceMap to file
if ($SourcemapFile) {
try {
$sourcemap = $compiler->compileString($content)->getSourceMap();
$this->wire->files->filePutContents($SourcemapFile, $sourcemap);
} catch (\Exception $e) {
$this->log($e->getMessage());
if (wire()->modules->isInstalled('TracyDebugger')) {
bd($e);
}
}
}
}
/**
* Watch core .scss files and auto-compile .css files
*/
public function watchCore()
{
$path = $this->wire->config->paths->wire;
$opt = ['extensions' => ['scss']];
$this->compiler()->setOutputStyle(OutputStyle::COMPRESSED);
foreach ($this->wire->files->find($path, $opt) as $scss) {
$css = substr($scss, 0, -4) . "css";
if (!is_file($css)) continue;
if (filemtime($css) >= filemtime($scss)) continue;
$content = $this->wire->files->fileGetContents($scss);
$this->wire->files->filePutContents(
$css,
$this->compiler()->compileString($content)->getCss()
);
}
}
public function __debugInfo()
{
return [
'compiler' => $this->compiler(),
];
}
}