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: dont replace define in json #7294

Merged
merged 1 commit into from Mar 12, 2022
Merged

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Mar 12, 2022

Description

Closes #4173

Don't run regex define replacements in JSON files as it only contains strings/booleans/numbers, to give slight perf improvements and prevent accidental replacements.

Additional context

Not sure if there's more non-js file extensions to add, but JSON seems to be the safest since Vite handles that natively.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@bluwy bluwy added the p2-nice-to-have Not breaking anything but nice to have (priority) label Mar 12, 2022
Comment on lines 102 to 106
// exclude html, css and static assets for performance
isHTMLRequest(id) ||
isCSSRequest(id) ||
isNonJsRequest(id) ||
config.assetsInclude(id)
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should go the other way around and use the isJSRequest(id) utility here. This isn't only for performance, if we want to avoid inconsistencies between dev and build, we shouldn't be doing replacements for define in non-js sources.
I think we can merge your PR now to close the issue, but let's explore this further.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've thought about it too but it feels like a bold move that could cause a breaking change if there are more extensions that compile to JS (Vite currently keeps a hardcoded list for it). And I can see some cases for this, like .svelte.md which is compiled by Svelte but is written in markdown.

Maybe the more robust way is to somehow identify the code is JS and then do the replacements. Or otherwise we can proceed with #3828 to make things more flexible.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, #3828 got stuck because we thought there was a chance of removing this list altogether but there hasn't been progress in that direction and this is another valid use case for being able to answer the "isJSRequest" question. Maybe we should push for #3828 again.

@patak-dev patak-dev merged commit fc5c937 into vitejs:main Mar 12, 2022
@bluwy bluwy deleted the fix-define-json branch March 12, 2022 23:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

process.env.NODE_ENV in package.json is incorrectly replaced
2 participants