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

Dynamic filename issue #36

Closed
samyoungnyc opened this issue Jul 1, 2020 · 5 comments · Fixed by #37
Closed

Dynamic filename issue #36

samyoungnyc opened this issue Jul 1, 2020 · 5 comments · Fixed by #37
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@samyoungnyc
Copy link

samyoungnyc commented Jul 1, 2020

I am trying this library because I have not been able to dynamically import markdown files successfully using create-react-app. I am trying to avoid ejecting.

I saw a snippet on this thead: facebook/create-react-app#3722

I am trying to load markdown files dynamically based on a prop lang like so:

const markdown = raw(`./${lang}About.md`);
setView(markdown);

I get the following error:

raw.macro: There was a problem evaluating the value of the argument for the code: raw(./${lang}About.md). If the value is dynamic, please make sure that its value is statically deterministic. Learn more:

What this error is saying is puzzling to me. If setting a path like this is not possible I understand, however if this is something that should be possible another way, would love to know how.

@pveyes
Copy link
Owner

pveyes commented Jul 1, 2020

Yeah this is the limitation of macro in general because it runs at build time. In your case it's not possible to resolve lang value in build time.

Maybe we can improve the error message. This one is actually copied from import-all.macro

@samyoungnyc
Copy link
Author

Ok gotcha. Thanks for clarifying!

@ibarapascal
Copy link

Actually, if you write

const lang = 'xxxx';
const markdown = raw(`./${lang}About.md`);

It would pass, and the below not

const lang = ['xxx', 'yyy'];
const markdown = raw(`./${lang[0]}About.md`);
const lang = {x: 'xxx'};
const markdown = raw(`./${lang.x}About.md`);

By the way, is there any not run at build time solution to import markdown file?

@pveyes
Copy link
Owner

pveyes commented Jul 17, 2020

Ok good news: turns out it's possible to do with some work. It's basically similar with how webpack dynamic require works

The bad news is there's a lot of cases to cover.

I've made a proof of concept in #37, feel free to send alternative solution or improve my PR.

@pveyes pveyes reopened this Jul 17, 2020
@pveyes pveyes added good first issue Good for newcomers help wanted Extra attention is needed labels Jul 17, 2020
@pveyes
Copy link
Owner

pveyes commented Jul 18, 2020

Ok done, the dynamic value inside template literal is limited up to 2 variables (1 for directory name, and another one for filename). I think this should be more than enough and cover > 99% of use case when using dynamic import. I also added some notes in README regarding this limitation.

You can try it out it #37. I plan to merge it on Monday and release it as v0.4.0 if all goes well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants