Skip to content
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

[MAJOR] Expand relative imports in external maps #81

Merged

Conversation

robmosca
Copy link
Contributor

@robmosca robmosca commented Jun 18, 2023

Fixes issues #80.

In order to fix the issue, I propose expanding relative URLs in external maps when they are loaded. In this way those mappings:

  • will be displayed with a correct "domain" value when shown in the UI
  • will be treated correctly when introducing devLibs overrides (otherwise they would become relative to the baseUrl of index.html, instead of the URL of the import map, as specified in the import-map specs, here)

This is potentially a breaking change so it would need a major release update.

@robmosca robmosca force-pushed the fix-remote-relative-dev-libs-imports branch from acf8be5 to 9ed3f66 Compare June 18, 2023 19:57
package.json Outdated
@@ -61,5 +61,6 @@
},
"dependencies": {
"cookie": "^0.4.1"
}
},
"packageManager": "[email protected]"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the pnpm-lock.yaml file to a more recent version of pnpm. I suggest adding this line to make explicit which version of pnpm goes well with the existing lock file.

@@ -63,3 +63,6 @@ dist

# jetbrain IDE files
/.idea

# VSCode IDE files
.vscode
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To prevent accidentally adding VSCode settings

Comment on lines +213 to +215
return Promise.all([promise, nextPromise]).then(
([originalMap, newMap]) =>
imo.mergeImportMap(originalMap, newMap)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Prettier seems to be reformatting some lines...

Copy link
Contributor Author

@robmosca robmosca Jul 1, 2023

Choose a reason for hiding this comment

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

I cannot explain this change from prettier. I verified I am using the version of prettier pinned in the dependencies.

Comment on lines -484 to +488
console.warn(
Error(`Unable to download external import map at url '${url}'`)
);
imo.invalidExternalMaps.push(new URL(url, document.baseURI).href);
return createEmptyImportMap();
}
);
)
.then((importMap) => expandRelativeUrlsInImportMap(importMap, url));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the actual change

@robmosca robmosca force-pushed the fix-remote-relative-dev-libs-imports branch from 9ed3f66 to 609fd2c Compare June 18, 2023 20:08
package.json Outdated
@@ -1,6 +1,6 @@
{
"name": "import-map-overrides",
"version": "3.0.0",
"version": "4.0.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is potentially a breaking change. What do you think @joeldenning?

@robmosca robmosca force-pushed the fix-remote-relative-dev-libs-imports branch from 609fd2c to 23b1ed8 Compare June 29, 2023 16:04
@robmosca robmosca changed the title Expand relative imports in external maps [MAJOR] Expand relative imports in external maps Jun 29, 2023
Copy link
Member

@joeldenning joeldenning left a comment

Choose a reason for hiding this comment

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

This looks good. Were you able to test it very much? I'm thinking this might not need to be a major/breaking change to import-map-overrides, since it only impacts how the URLs are displayed in the UI. Do you agree?

const outUrl = new URL(url, baseUrl);
return outUrl.href;
} catch (err) {
return url;
Copy link
Member

Choose a reason for hiding this comment

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

Is returning the relative url the correct thing to do when there is an error? The error would only happen if baseUrl is wrong, right? I'm not sure what the best UX is for when the baseUrl is invalid

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, me neither. I wasn't sure so I thought that returning the relative url would still be a reasonable thing to do. I am open to change it as you see fit, if there is a better way of handling this. I don't think this should ever happen, actually.

);
imo.invalidExternalMaps.push(r.url);
imo.invalidExternalMaps.push(new URL(url, document.baseURI).href);
Copy link
Member

Choose a reason for hiding this comment

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

👍 this is a good change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, this line was already there (line 487 in the older version). It looks like a change because of the modifications from prettier. See this comment about it.

@robmosca robmosca force-pushed the fix-remote-relative-dev-libs-imports branch from b07d322 to e93d8ff Compare July 1, 2023 16:56
Comment on lines +120 to +123
externalOverrideModules.push({
...mod,
overrideUrl: this.state.currentPageMap.imports[moduleName],
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When including an external override the domain is displayed from the defaultMap, not from the overridden entry.

@robmosca
Copy link
Contributor Author

robmosca commented Jul 1, 2023

This looks good. Were you able to test it very much? I'm thinking this might not need to be a major/breaking change to import-map-overrides, since it only impacts how the URLs are displayed in the UI. Do you agree?

I tested it in the following scenarios:

  1. Importing dependencies from an external import map (including both normal dependencies and dependencies that can be submit to dev overrides)
  2. Adding an override from an external import map.

In this second scenario, I realised there was still a problem with the visualisation of the domain, which I tried to fix here).
Also, in our company we are using a version corresponding to this PR and, at the moment, it seems to be working correctly.

As for the version change. This PR will change the way modified dev dependencies will be loaded. The import-map-overrides changes automatically the production version of some dependencies to the development counterpart. When it does that, the overridden dependencies are injected in an import map after all the ones already existing in the document. If those dependencies were specified with a relative path and they were set in an external import map, they were resolved based to the URL of the import map file and when the override happened, they would instead be resolved to the base URL of the index.html file. With this PR, they continue to be resolved w.r.t. the URL of the import map, which might change the behaviour for some existing instances.
It is quite an edge case and the change actually aligns the current implementation to the import map standard, so... yes, it could also be fair to use a patch update (even if, in principle it could break some existing use cases).

As you prefer, Joel, both major or patch are OK for me.

@@ -10,6 +10,7 @@
"build": "pnpm run clean && cross-env NODE_ENV=production rollup -c",
"build:dev": "pnpm run clean && cross-env NODE_ENV=development rollup -c",
"clean": "rimraf dist",
"check-format": "prettier --check src",
Copy link
Contributor Author

@robmosca robmosca Jul 1, 2023

Choose a reason for hiding this comment

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

I added this command to check the format with prettier. I am not sure why so many changes from prettier. I am using the version from the node_modules folder as you can see in this image:

Screenshot 2023-07-02 at 12 02 43

🤷🏽‍♂️

@TheMcMurder TheMcMurder merged commit 4e90b5d into single-spa:main Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants