-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
JsDirectoryCompiler.php
139 lines (110 loc) · 4.21 KB
/
JsDirectoryCompiler.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
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Frontend\Compiler;
use Flarum\Frontend\Compiler\Concerns\HasSources;
use Flarum\Frontend\Compiler\Source\DirectorySource;
use Flarum\Frontend\Compiler\Source\SourceCollector;
use Illuminate\Contracts\Filesystem\Cloud;
use Illuminate\Filesystem\FilesystemAdapter;
/**
* Used to copy JS files from a package directory to the assets' directory.
* Without concatenating them. Primarily used for lazy loading JS modules.
*/
class JsDirectoryCompiler implements CompilerInterface
{
use HasSources;
protected VersionerInterface $versioner;
public function __construct(
protected Cloud $assetsDir,
protected string $destinationPath
) {
$this->versioner = new FileVersioner($assetsDir);
}
public function getFilename(): ?string
{
return null;
}
public function setFilename(string $filename): void
{
//
}
public function commit(bool $force = false): void
{
foreach ($this->getSources() as $source) {
$this->compileSource($source, $force);
}
}
public function getUrl(): ?string
{
foreach ($this->getSources() as $source) {
$this->eachFile($source, fn (JsCompiler $compiler) => $compiler->getUrl());
}
return null;
}
public function flush(): void
{
foreach ($this->getSources() as $source) {
$this->flushSource($source);
}
// Delete the remaining empty directory.
$this->assetsDir->deleteDirectory($this->destinationPath);
}
protected function allowedSourceTypes(): array
{
return [DirectorySource::class];
}
protected function compileSource(DirectorySource $source, bool $force = false): void
{
$this->eachFile($source, fn (JsCompiler $compiler) => $compiler->commit($force));
}
protected function flushSource(DirectorySource $source): void
{
$this->eachFile($source, fn (JsCompiler $compiler) => $compiler->flush());
$destinationDir = $this->destinationFor($source);
// Destination can still contain stale chunks.
$this->assetsDir->deleteDirectory($destinationDir);
// Delete stale revisions.
$remainingRevisions = $this->versioner->allRevisions();
foreach ($remainingRevisions as $filename => $revision) {
if (str_starts_with($filename, $destinationDir)) {
$this->versioner->putRevision($filename, null);
}
}
}
protected function eachFile(DirectorySource $source, callable $callback): void
{
$filesystem = $source->getFilesystem();
foreach ($filesystem->allFiles() as $relativeFilePath) {
// Skip non-JS files.
if ($filesystem->mimeType($relativeFilePath) !== 'application/javascript') {
continue;
}
$jsCompiler = $this->compilerFor($source, $filesystem, $relativeFilePath);
$callback($jsCompiler);
}
}
protected function compilerFor(DirectorySource $source, FilesystemAdapter $filesystem, string $relativeFilePath): JsCompiler
{
// Filesystem's root is the actual directory we want to copy.
// The destination path is relative to the assets' filesystem.
$jsCompiler = resolve(JsCompiler::class, [
'assetsDir' => $this->assetsDir,
// We put each file in `js/extensionId/frontend` (path provided) `/relativeFilePath` (such as `components/LogInModal.js`).
'filename' => $this->destinationFor($source, $relativeFilePath),
]);
$jsCompiler->addSources(
fn (SourceCollector $sources) => $sources->addFile($filesystem->path($relativeFilePath), $source->getExtensionId())
);
return $jsCompiler;
}
protected function destinationFor(DirectorySource $source, ?string $relativeFilePath = null): string
{
$extensionId = $source->getExtensionId() ?? 'core';
return str_replace('{ext}', $extensionId, $this->destinationPath).DIRECTORY_SEPARATOR.$relativeFilePath;
}
}