8000 [AssetMapper] Fix in-file imports to resolve via filesystem by weaverryan · Pull Request #52349 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[AssetMapper] Fix in-file imports to resolve via filesystem #52349

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

Conversation

weaverryan
Copy link
Member
@weaverryan weaverryan commented Oct 28, 2023
Q A
Branch? 6.4
Bug fix? yes
New feature? no
Deprecations? no
Issues None
License MIT

Hi!

Another fix from further testing in the complex world!

tl;dr Relative imports in CSS & JS files are now resolved via the filesystem path, not the AssetMapper "logical path".

Full Explanation

In AssetMapper, we configure different paths/directories, which exposes them to the public directory. If you need to fetch the info about an asset, you can use its "logical path".

However, when you're inside of a JavaScript or CSS file, these "asset mapper" paths or "logical asset paths" are correctly irrelevant. Instead, you should code as if all of your files are already public and won't be moved around:

// /path/to/project/assets/app.js

// (1️⃣) 👍 /path/to/project/assets/files/app.js
import './files/other.js'; 
// (2️⃣) 👍 /path/to/project/vendor/foo/bar/assets/baz.js
import '../vendor/foo/bar/assets/baz.js

// (3️⃣) 👎
// this is a valid asset mapper "logical path", but this is nonsense and doesn't work
// well, you could add this to the importmap. But "asset mapper" paths still have no relevancy
import 'files/other.js';

This PR fixes case 2️⃣ which previously did not work. Before, we used ../vendor/foo/bar/assets/baz.js and the "asset mapper logical path" of the importing file (app.js) and tried to construct a final, logical path from that. But because of the ../vendor, that went above the app.js root, and we panicked. Even if we didn't panic, logical paths should not be relevant here. Instead, it's simpler: the relative import in app.js uses the filesystem path. Of course, if the final file - /vendor/foo/bar/assets/baz.js is not, itself, in some AssetMapper path, an clear exception is thrown (if the file is not exposed publicly, we can't resolve a proper path to it).

Cheers!

@@ -38,23 +37,29 @@ public function compile(string $content, MappedAsset $asset, AssetMapperInterfac
{
return preg_replace_callback(self::ASSET_URL_PATTERN, function ($matches) use ($asset, $assetMapper) {
try {
$resolvedPath = $this->resolvePath(\dirname($asset->logicalPath), $matches[1]);
$resolvedSourcePath = Path::join(\dirname($asset->sourcePath), $matches[1]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit unrelated: let's let Path do the work

// avoid warning about vendor imports - these are often comments
if ($asset->isVendor) {
return null;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just represents a reorg so we can return earlier

@@ -38,23 +37,29 @@ public function compile(string $content, MappedAsset $asset, AssetMapperInterfac
{
return preg_replace_callback(self::ASSET_URL_PATTERN, function ($matches) use ($asset, $assetMapper) {
try {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we can ignore this. In practice, our preg_replace_callback() will return a string. But if there is some change we want to make to satisfy psalm, we can.

Copy link
Member
fabpot commented Oct 28, 2023

Thank you @weaverryan.

@fabpot fabpot force-pushed the asset-mapper-resolve-relative-paths-via-filesystem branch from f7f3574 to 659c635 Compare October 28, 2023 23:24
@fabpot fabpot merged commit fd3435a into symfony:6.4 Oct 28, 2023
@weaverryan weaverryan deleted the asset-mapper-resolve-relative-paths-via-filesystem branch October 29, 2023 01:54
xabbuh added a commit that referenced this pull request Oct 29, 2023
This PR was merged into the 6.4 branch.

Discussion
----------

[AssetMapper] Fixing merge

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | None
| License       | MIT

Fixing the merge of #52323, #52331 and #52349

Also tested on a real project locally to verify the moving pieces :).

Thanks!

Commits
-------

99d5cbb [AssetMapper] Fixing merge from multiple PR's
This was referenced Oct 29, 2023
nicolas-grekas added a commit that referenced this pull request Oct 31, 2023
…e importmap entry path (weaverryan)

This PR was squashed before being merged into the 6.4 branch.

Discussion
----------

[AssetMapper] Fixing bug where JSCompiler used non-absolute importmap entry path

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | None
| License       | MIT

Introduced over the weekend in #52349.

The `path` key inside `importmap.php` can be a full filesystem path or it can start with `./` to indicate a relative path to the root directory. That has been the case for weeks. In #52349, when start using the `path` of an importmap entry to find the `MappedAsset`. When doing that, I didn't consider the case where `path` is a relative path.

This PR fixes that and moves the code handling this into `ImportMapConfigReader` so that we're not repeating it in several places.

Cheers!

Commits
-------

31d04ef [AssetMapper] Fixing bug where JSCompiler used non-absolute importmap entry path
weaverryan added a commit to symfony/ux that referenced this pull request Nov 2, 2023
This PR was squashed before being merged into the 2.x branch.

Discussion
----------

Adding support for AssetMapper 6.4

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| Issues        | None
| License       | MIT

Change for AssetMapper 6.4 support. This is 2 main things:

A) Changes needed after symfony/symfony#52349 with removal of trait + absolute import paths
B) Outputting `autoimport` via `{{ importmap() }}` function / deprecate `ux_controller_link_tags()`.

TODO:
* I still need to the React/Svelte/Vue support to double-check it's all good for both 6.3 and 6.4.
* Finish lazy autoimport
* Make sure `controllers.json` is properly a config cache file resource for appropriate files

Cheers!

Commits
-------

05bcb98 Adding support for AssetMapper 6.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0