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

feat(html): support env replacement #12202

Merged
merged 6 commits into from
Mar 7, 2023
Merged

feat(html): support env replacement #12202

merged 6 commits into from
Mar 7, 2023

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Feb 26, 2023

Description

Support %ENV_NAME% syntax in HTML files. Close #3105

Additional context

  1. I made it a pre-hook for transformIndexHtml as otherwise normal-hook transforms like vite-plugin-html may get a parse error.
  2. It also supports %MODE%, %PROD%, etc, which are a little short, but maybe that's fine.
  3. It doesn't support %SSR% as I don't think it makes sense in HTML files.

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 PR Title 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 feat: html p2-nice-to-have Not breaking anything but nice to have (priority) labels Feb 26, 2023
@Tal500
Copy link
Contributor

Tal500 commented Feb 28, 2023

Isn't this a breaking change?

@bluwy
Copy link
Member Author

bluwy commented Feb 28, 2023

It would break if someone happens to use %something% and has an env named "something". If not %something% would be left as-is. I think it's quite unlikely that this would happen and break things, we also used to merge features like this in the past.

@patak-dev
Copy link
Member

It would break if someone happens to use %something% and has an env named "something". If not %something% would be left as-is.

What do you think if we also parse the remaining %VITE_...% placeholders and emit a warning about this env being missed? I think it will be a common mistake and we could still be safe if we only check for the env prefixed ones.

@Tal500
Copy link
Contributor

Tal500 commented Feb 28, 2023

Additionally, SvelteKit uses %% in a different manner, but it might not matter since they have their own HTML template parsing and HTML generation.

@bluwy
Copy link
Member Author

bluwy commented Mar 1, 2023

What do you think if we also parse the remaining %VITE_...% placeholders and emit a warning about this env being missed? I think it will be a common mistake and we could still be safe if we only check for the env prefixed ones.

Yeah I think this is good to have too. I'll add a quick warning for this, and maybe we could improved later if needed.

@PunkFleet
Copy link

Is this feature available in 4.1.4?

@bluwy
Copy link
Member Author

bluwy commented Mar 7, 2023

It's not merged yet, so no.

patak-dev
patak-dev previously approved these changes Mar 7, 2023
sapphi-red
sapphi-red previously approved these changes Mar 7, 2023
Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

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

One nit pick but LGTM 👍

docs/guide/env-and-mode.md Show resolved Hide resolved
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
patak-dev
patak-dev previously approved these changes Mar 7, 2023
@patak-dev patak-dev enabled auto-merge (squash) March 7, 2023 12:50
@patak-dev patak-dev merged commit 4f2c49f into main Mar 7, 2023
@patak-dev patak-dev deleted the html-env branch March 7, 2023 13:07
@lucassonsinlima
Copy link

hi! I know this PR was just merged like 2 days ago, but since it's in main branch, we should expect some release for this? ty!

@patak-dev
Copy link
Member

@lucassonsinlima it is already released as vite@4.2.0-beta.1. Next week 4.2 stable will go out if there aren't regressions and the current issues in ecosystem-ci are resolved

@lucassonsinlima
Copy link

@lucassonsinlima it is already released as vite@4.2.0-beta.1. Next week 4.2 stable will go out if there aren't regressions and the current issues in ecosystem-ci are resolved

thank you!!

@aardvarkk
Copy link

aardvarkk commented Mar 13, 2023

@bluwy @patak-dev Is there anything incorrect about trying to use it in the following way:

    <link rel="icon" href="%VITE_FAVICON_URL%" />

It works well in the development environment, but when I try to run build I get the following:

error during build:
URIError: URI malformed

For this to work, should this transform be performed as a preHook rather than a normalHook?

@patak-dev
Copy link
Member

Thanks for the report @aardvarkk, #12404 should fix your issue.

@Eduruiz
Copy link

Eduruiz commented May 25, 2023

That's so cool! updated vite to use it here.
there is some way to check an env variable (an if statement) to dynamically ad some script on it?

@el-frontend
Copy link

Thank you, guys. 🎉

@fritzfelix
Copy link

fritzfelix commented Jan 2, 2024

@bluwy Does this enable conditionals too, i.e. the logical equivalent of the following ejs syntax:

<% if (process.env.REACT_APP_ENV !== 'production') { %>
  <meta name="robots" content="noindex,nofollow" />
<% } %>

@XiSenao
Copy link
Collaborator

XiSenao commented Jan 2, 2024

@fritzfelix I feel that vite may not natively support it, perhaps you can use a similar plugin to pre-parse the ejs module.

import ejs from 'ejs';

{
  enforce: 'pre',
  name: 'ejs-inject',
  transformIndexHtml: {
    order: 'pre',
    handler(html) {
      const template = ejs.compile(html);
      process.env.REACT_APP_ENV = 'production';
      return template(process.env);
    }
  }
}

@bluwy
Copy link
Member Author

bluwy commented Jan 3, 2024

Yeah, conditionals are not planned for Vite and is documented at https://vitejs.dev/guide/env-and-mode.html#html-env-replacement. We don't want to add a potential proprietary templating style to the HTML by default.

@nuthinking
Copy link

I ended up using ViteEjsPlugin, I couldn't figure out how to use this feature 🤷‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: html 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.

Accessing env variables from index.html