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

use regex for detecting import.meta #1371

Merged

Conversation

Rich-Harris
Copy link
Contributor

Changes

The code that detects import.meta, in order to inject the HMR client etc, is using a naive code.includes('import.meta') check. This updates it to use a more robust regular expression, that can catch things like

import
  .meta
  .blah

which could be injected by tools like Prettier.

Testing

I changed one of the tests that uses import.meta.hot, but wasn't sure how to update the snapshot, so it's currently failing.

Docs

No docs, bug fix only

@vercel
Copy link

vercel bot commented Oct 20, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/pikapkg/snowpack/3ochcxrzx
✅ Preview: https://snowpack-git-robustify-import-meta-check.pikapkg.vercel.app

Copy link
Owner

@FredKSchott FredKSchott left a comment

Choose a reason for hiding this comment

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

LGTM with 1 comment. yarn build && yarn test -u should fix snapshots automatically.

@@ -30,7 +30,7 @@ export function wrapImportMeta({
env: boolean;
config: SnowpackConfig;
}) {
if (!code.includes('import.meta')) {
if (!/import\s*\.\s*meta/.test(code)) {
Copy link
Owner

Choose a reason for hiding this comment

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

this is a pretty hot code path, can you move the regex creation outside of the function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Owner

@FredKSchott FredKSchott left a comment

Choose a reason for hiding this comment

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

LGTM! Will merge once tests pass

@FredKSchott FredKSchott merged commit f14b23b into FredKSchott:master Oct 20, 2020
@Rich-Harris Rich-Harris deleted the robustify-import-meta-check branch October 20, 2020 17:23
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.

None yet

2 participants