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

[FEATURE] Transform complex dynamic imports #2861

Open
FredKSchott opened this issue Mar 15, 2021 · 4 comments
Open

[FEATURE] Transform complex dynamic imports #2861

FredKSchott opened this issue Mar 15, 2021 · 4 comments

Comments

@FredKSchott
Copy link
Owner

The problem that you want to solve.

Issue reported here: #2840
We should be able to transform:

import(`../locales/${langCode}.json`);

to:

import(`../locales/${langCode}.json.proxy.js`);

Your take on the correct solution to problem.

We could add some advanced handling to our internal transform utility to parse the expression inside of a dynamic import into an AST (with something like acorn) or even something simpler like Regex. Then, we could transform the template string based on its raw parts.

Are you willing to submit a pull request to implement this change?

This is a little tricky, but fairly self contained / a good place to start if you're looking to contribute to Snowpack!

@FredKSchott FredKSchott added this to Ready to Implement in Triage Mar 15, 2021
@natemoo-re
Copy link
Contributor

I wonder if we could combine #2900 and #2881, expand the import.meta.glob lexer to collect all import statements, then handle import.meta.glob and proxy import transforms all in one go? That sounds like it would be the most performant solution.

We could also speed up the lexer by only scanning forward from import tokens rather than the whole file.

@natemoo-re
Copy link
Contributor

Also this particular use case might be solved by our upcoming import.meta.glob feature? I'll have to test this one!

// writing the following
const langs = import.meta.glob(`../locales/*.json`);

// should produce
const langs = {
  '../locales/en.json': () => import('../locales/es.json.proxy.js'),
  '../locales/es.json': () => import('../locales/es.json.proxy.js')
}

@FredKSchott
Copy link
Owner Author

Oh interesting! What if we just added a warning when we saw a complex dynamic import, telling you to use glob instead?

@Airkro
Copy link

Airkro commented Jul 7, 2021

import.meta.glob can't be found in the document.

@FredKSchott FredKSchott removed this from Ready to Implement in Triage Apr 20, 2022
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

No branches or pull requests

3 participants