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

RequireFallback function for requiring userland code #1800

Closed
wants to merge 1 commit into from

Conversation

nickmaltsev
Copy link
Contributor

} catch (e) {
Html = require(`./default-html`)
}
const Html = requireWithFallback(`../src/html`, `./default-html`)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you can abstract this actually. Webpack statically analyzes requires, if the require path is completely dynamic the chances of stuff going wrong are high. like it including ALL files in the root in the bundle

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't know about these webpack features. So, seems like the only way here is to add e.code !== MODULE_NOT_FOUND checking for each try/catch block?

Copy link
Contributor

Choose a reason for hiding this comment

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

for any require in cache-dir yes, you can rescope the helper to contain the check and throw if you want though. but you need to do the require with string literals

@KyleAMathews
Copy link
Contributor

Deploy preview failed.

Built with commit e9bd386

https://app.netlify.com/sites/using-glamor/deploys/5991f8156f4c5071551d7008

@KyleAMathews
Copy link
Contributor

Working on a PR for this exact issue right now actually :-)

And yeah, as @jquense said, you need to require the fallback module in the catch.

Also checking for MODULE_NOT_FOUND won't work I've learned as the module you're requiring requires a non-existent module then the code will still be MODULE_NOT_FOUND. Turns out the only foolproof way is do a regex check of the error string 🤷‍♂️

@KyleAMathews
Copy link
Contributor

Thanks for looking into this! Will be reviewing your other PRs soon!

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