-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[AssetMapper] Put importmap in polyfill so it can be hosted locally easily #51828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fabpot
merged 1 commit into
symfony:6.4
from
weaverryan:asset-mapper-importmap-in-polyfill
Oct 16, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ | |
*/ | ||
class ImportMapManager | ||
{ | ||
public const POLYFILL_URL = 'https://ga.jspm.io/npm:[email protected]/dist/es-module-shims.js'; | ||
public const IMPORT_MAP_CACHE_FILENAME = 'importmap.json'; | ||
public const ENTRYPOINT_CACHE_FILENAME_PATTERN = 'entrypoint.%s.json'; | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,11 +27,13 @@ | |
*/ | ||
class ImportMapRenderer | ||
{ | ||
private const DEFAULT_ES_MODULE_SHIMS_POLYFILL_URL = 'https://ga.jspm.io/npm:[email protected]/dist/es-module-shims.js'; | ||
|
||
public function __construct( | ||
private readonly ImportMapManager $importMapManager, | ||
private readonly ?Packages $assetPackages = null, | ||
private readonly string $charset = 'UTF-8', | ||
private readonly string|false $polyfillUrl = ImportMapManager::POLYFILL_URL, | ||
private readonly string|false $polyfillImportName = false, | ||
private readonly array $scriptAttributes = [], | ||
private readonly ?RequestStack $requestStack = null, | ||
) { | ||
|
@@ -45,6 +47,7 @@ public function render(string|array $entryPoint, array $attributes = []): string | |
$importMap = []; | ||
$modulePreloads = []; | ||
$cssLinks = []; | ||
$polyFillPath = null; | ||
foreach ($importMapData as $importName => $data) { | ||
$path = $data['path']; | ||
|
||
|
@@ -53,6 +56,12 @@ public function render(string|array $entryPoint, array $attributes = []): string | |
$path = $this->assetPackages->getUrl(ltrim($path, '/')); | ||
} | ||
|
||
// if this represents the polyfill, hide it from the import map | ||
if ($importName === $this->polyfillImportName) { | ||
$polyFillPath = $path; | ||
continue; | ||
} | ||
|
||
$preload = $data['preload'] ?? false; | ||
if ('css' !== $data['type']) { | ||
$importMap[$importName] = $path; | ||
|
@@ -87,8 +96,17 @@ public function render(string|array $entryPoint, array $attributes = []): string | |
</script> | ||
HTML; | ||
|
||
if ($this->polyfillUrl) { | ||
$url = $this->escapeAttributeValue($this->polyfillUrl); | ||
if (false !== $this->polyfillImportName && null === $polyFillPath) { | ||
if ('es-module-shims' !== $this->polyfillImportName) { | ||
throw new \InvalidArgumentException(sprintf('The JavaScript module polyfill was not found in your import map. Either disable the polyfill or run "php bin/console importmap:require "%s"" to install it.', $this->polyfillImportName)); | ||
} | ||
|
||
// a fallback for the default polyfill in case it's not in the importmap | ||
$polyFillPath = self::DEFAULT_ES_MODULE_SHIMS_POLYFILL_URL; | ||
} | ||
|
||
if ($polyFillPath) { | ||
$url = $this->escapeAttributeValue($polyFillPath); | ||
|
||
$output .= <<<HTML | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.