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

Fix imports with a query part #603

Merged
merged 2 commits into from
Dec 12, 2023
Merged

Fix imports with a query part #603

merged 2 commits into from
Dec 12, 2023

Conversation

simonihmig
Copy link
Contributor

With the new allowAppImports feature, apps can import resources that are handled with custom webpack loaders. Some can allow you to customize their behavior by passing query params, like import image from './images/image.jpg?size=1024.

This PR is fixing two related issues:

  • make sure that an allowAppImports glob like 'images/**/*.jpg' would also match for imports that include query params, by stripping the query part before trying to match the import to the glob
  • query params could technically also include quotes. These already got escaped when building the app-chunk that has the AMD-shims, but the regex to replace EAI_DISCOVERED_EXTERNALS was not taking their possible existence into account correctly, leaving EAI_DISCOVERED_EXTERNALS around in the final build.

With the new `allowAppImports` feature, apps can import resources that are handledwith custom webpack loaders. Some can allow you to customize their behavior by passing query params, like `import image from './images/image.jpg?size=1024`.

This PR is fixing two related issues:
* make sure that an `allowAppImports` glob like `'images/**/*.jpg'` would also match for imports that include query params, by stripping the query part before trying to match the import to the glob
* query params could technically also include quotes. These already got escaped when building the app-chunk that has the AMD-shims, but the regex to replace `EAI_DISCOVERED_EXTERNALS` was not taking their possible existance into account correctly.
@simonihmig simonihmig force-pushed the fix-imports-with-query branch from d14ae9a to b51bc86 Compare December 3, 2023 20:01
function moduleToId(moduleSpecifier: string) {
let id = moduleSpecifier;

// if the module contains characters that need to be escaped, then map this to a hash instead, so we can easily replace this later
Copy link
Contributor Author

@simonihmig simonihmig Dec 3, 2023

Choose a reason for hiding this comment

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

This is used for the argument to EAI_DISCOVERED_EXTERNALS(). When we have quotes in the query part of an import, mapping this part back to a module inside addDiscoveredExternals can become a bit more involved, because not only would we have to unescape the changes from js-string-escape, but in the case of devtool: 'eval' we would even end up with double-escaped quotes (' --js-string-escape--> \' --webpack-eval--> \\\').

So instead of trying to map this back to the original module specifier, I was just using something here that can serve as an identifier to the module, and which we can map back to it. In this case a md5 hash. Which has the benefit that we won't run into this kind of escaping/unescaping hell...

@simonihmig
Copy link
Contributor Author

I haven't yet looked into adding tests for this. I guess for covering imports of non-JS files, including query params, we would need to add a simple webpack loader in a scenario test? Like on that returns the query as the generated module's export, so we can assert that the loader is correctly invoked and the query stuff works? Should that be a separate scenario? Or any other suggestions?

@simonihmig
Copy link
Contributor Author

Btw, I worked on these changes as part of making simonihmig/responsive-image#442 work with ember-auto-import, and it seems this is indeed the final missing piece, as the things that worked under Embroider are now also passing (locally) under eai! 🎉

@simonihmig simonihmig requested review from mansona and ef4 December 3, 2023 20:17
@ef4
Copy link
Collaborator

ef4 commented Dec 4, 2023

This is looking reasonable. For tests, I think you can extend these where we test allowAppImports in general.

Adding a loader would be OK, but it also might be enough to use built-in webpack features like Asset Modules.

@simonihmig simonihmig marked this pull request as ready for review December 6, 2023 19:06
@simonihmig simonihmig added the bug Something isn't working label Dec 6, 2023
@simonihmig
Copy link
Contributor Author

Added tests, this should be good now for another review! @ef4 @mansona

@ef4 ef4 merged commit 5204edf into main Dec 12, 2023
119 checks passed
@github-actions github-actions bot mentioned this pull request Dec 12, 2023
@simonihmig simonihmig deleted the fix-imports-with-query branch December 12, 2023 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants