Skip to content

Commit ba79d06

Browse files
committed
Changing preload -> render_as_link_tag for CSS
1 parent 599c168 commit ba79d06

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

src/Symfony/Component/AssetMapper/ImportMap/ImportMapConfigReader.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function getEntries(): ImportMapEntries
3838

3939
$entries = new ImportMapEntries();
4040
foreach ($importMapConfig ?? [] as $importName => $data) {
41-
$validKeys = ['path', 'url', 'downloaded_to', 'type', 'preload', 'entrypoint'];
41+
$validKeys = ['path', 'url', 'downloaded_to', 'type', 'preload', 'entrypoint', 'render_link_tag'];
4242
if ($invalidKeys = array_diff(array_keys($data), $validKeys)) {
4343
throw new \InvalidArgumentException(sprintf('The following keys are not valid for the importmap entry "%s": "%s". Valid keys are: "%s".', $importName, implode('", "', $invalidKeys), implode('", "', $validKeys)));
4444
}
@@ -47,15 +47,33 @@ public function getEntries(): ImportMapEntries
4747
$isEntry = $data['entrypoint'] ?? false;
4848

4949
if ($isEntry && ImportMapType::JS !== $type) {
50-
throw new RuntimeException('The "entrypoint" option can only be used with the "js" type.');
50+
throw new RuntimeException(sprintf('The "entrypoint" option can only be used with the "js" type. Found in importmap.php for key "%s"', $importName));
51+
}
52+
53+
$preload = null;
54+
// render_link_tag is an alias for preload for CSS type
55+
if (isset($data['render_link_tag'])) {
56+
if (ImportMapType::CSS !== $type) {
57+
throw new RuntimeException(sprintf('The "render_link_tag" option can only be used with the "css" type. Found in importmap.php for key "%s"', $importName));
58+
}
59+
60+
$preload = $data['render_link_tag'];
61+
}
62+
63+
if (isset($data['preload'])) {
64+
if (ImportMapType::JS !== $type) {
65+
throw new RuntimeException(sprintf('The "preload" option can only be used with the "js" type. Found in importmap.php for key "%s"', $importName));
66+
}
67+
68+
$preload = $data['preload'];
5169
}
5270

5371
$entries->add(new ImportMapEntry(
5472
$importName,
5573
path: $data['path'] ?? $data['downloaded_to'] ?? null,
5674
url: $data['url'] ?? null,
5775
isDownloaded: isset($data['downloaded_to']),
58-
preload: $data['preload'] ?? null,
76+
preload: $preload,
5977
type: $type,
6078
isEntrypoint: $isEntry,
6179
));
@@ -79,7 +97,8 @@ public function writeEntries(ImportMapEntries $entries): void
7997
$config['url'] = $entry->url;
8098
}
8199
if ($entry->preload !== null) {
82-
$config['preload'] = $entry->preload;
100+
$key = ImportMapType::CSS === $entry->type ? 'render_link_tag' : 'preload';
101+
$config[$key] = $entry->preload;
83102
}
84103
if (ImportMapType::JS !== $entry->type) {
85104
$config['type'] = $entry->type->value;
@@ -100,9 +119,10 @@ public function writeEntries(ImportMapEntries $entries): void
100119
* - "path" is a path inside the asset mapper system. Use the
101120
* "debug:asset-map" command to see the full list of paths.
102121
*
103-
* - "preload"
104-
* For JavaScript: adds a modulepreload link to help the browser download it earlier.
105-
* For CSS: adds a <link rel="stylesheet"> tag for the CSS.
122+
* - "entrypoint" (JavaScript only) set to true for any module that will
123+
* be used as an the "entrypoint" (and passed to the importmap() Twig function).
124+
*
125+
* - "render_link_tag" (CSS only) set to true to render the CSS as a <link> tag.
106126
*
107127
* The "importmap:require" command can be used to add new entries to this file.
108128
*

src/Symfony/Component/AssetMapper/Tests/Command/AssetsMapperCompileCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function testAssetsAreCompiled()
8989
'/assets/file4.js', // imported by (preload: true) file5
9090
'file2', // in importmap (preload: null)
9191
'/assets/file1.css', // imported by (preload: null) file2.js
92-
'file3.css', // in importmap, (preload: true)
92+
'file3.css', // in importmap, (render_link_tag: true)
9393
// imported by file3.css: CSS imported by CSS does not need to be in the importmap
9494
// 'already-abcdefVWXYZ0123456789.digested.css',
9595
], array_keys($actualImportMap));

src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapConfigReaderTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public function testGetEntriesAndWriteEntries()
5353
'path' => 'styles/app.css',
5454
'type' => 'css',
5555
],
56+
'type_css_render_link_tag' => [
57+
'path' => 'styles/app.css',
58+
'render_link_tag' => true,
59+
'type' => 'css',
60+
],
5661
'entry_point' => [
5762
'path' => 'entry.js',
5863
'entrypoint' => true,
@@ -66,7 +71,7 @@ public function testGetEntriesAndWriteEntries()
6671
$this->assertInstanceOf(ImportMapEntries::class, $entries);
6772
/** @var ImportMapEntry[] $allEntries */
6873
$allEntries = iterator_to_array($entries);
69-
$this->assertCount(7, $allEntries);
74+
$this->assertCount(8, $allEntries);
7075

7176
$remotePackageEntry = $allEntries[0];
7277
$this->assertSame('remote_package', $remotePackageEntry->importName);
@@ -94,7 +99,10 @@ public function testGetEntriesAndWriteEntries()
9499
$typeCssEntry = $allEntries[5];
95100
$this->assertSame('css', $typeCssEntry->type->value);
96101

97-
$entryPointEntry = $allEntries[6];
102+
$typeCssRenderAsLinkTagEntry = $allEntries[6];
103+
$this->assertTrue($typeCssRenderAsLinkTagEntry->preload);
104+
105+
$entryPointEntry = $allEntries[7];
98106
$this->assertTrue($entryPointEntry->isEntrypoint);
99107

100108
// now save the original raw data from importmap.php and delete the file

src/Symfony/Component/AssetMapper/Tests/fixtures/importmap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
'file3.css' => [
3131
'path' => 'file3.css',
3232
'type' => 'css',
33-
'preload' => true,
33+
'render_link_tag' => true,
3434
],
3535
];

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy