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

Env HTML replacement does not remove double quotes from variables defined in config.define #13424

Closed
7 tasks done
yannbriancon opened this issue Jun 5, 2023 · 1 comment · Fixed by #13425
Closed
7 tasks done
Labels
p4-important Violate documented behavior or significantly improves performance (priority)

Comments

@yannbriancon
Copy link

yannbriancon commented Jun 5, 2023

Describe the bug

I am using the env HTML replacement feature to replace a variable in the HTML from an env value defined in vite config.

When stringifying this variable in the vite config, as recommended in the doc, the replacement is wrapped in double quotes.

When not stringifying it, it shows correctly but the js code breaks with the error env.mjs:16 Uncaught SyntaxError: Unexpected identifier 'Title'.

I traced the issue back to the PR #12202 where we do not parse the config define values even though they have to be stringified (line 958)

Reproduction

https://stackblitz.com/edit/vitejs-vite-cbisjp?file=vite.config.js

Steps to reproduce

Add a vite config with a variable:

/* eslint-disable import/no-extraneous-dependencies */
import { defineConfig } from 'vite';

export default ({ mode }) => {
  return defineConfig({
    define: {
      'import.meta.env.TITLE': JSON.stringify('My Title'),
    },
  });
};

Add a replacement pattern in the index.html:

    <title>%TITLE%</title>

Run npm install & npm run dev

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    vite: ^4.3.9 => 4.3.9 

Used Package Manager

npm

Logs

No response

Validations

@stackblitz
Copy link

stackblitz bot commented Jun 5, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@sapphi-red sapphi-red added the p4-important Violate documented behavior or significantly improves performance (priority) label Jun 5, 2023
Tobbe added a commit to redwoodjs/redwood that referenced this issue Jul 19, 2023
For compatability with webpack, and for consistency with node/server
environment we want to support reading env vars from `process.env`. But
we also want to support Vite's way of doing it, which is exposing them
on `import.meta.env`.

And this is a copy/paste from the code
```
// Vite can automatically expose environment variables, but we
// disable that in `buildFeServer.ts` by setting `envFile: false`
// because we want to use our own logic for loading .env,
// .env.defaults, etc
// The two object spreads below will expose all environment
// variables listed in redwood.toml and all environment variables
// prefixed with REDWOOD_ENV_
```

Note: This does not work for Vite's html `%%` replacement because of
vitejs/vite#13424
But we've got a separate PR that takes care of that in #8929
jtoar pushed a commit to redwoodjs/redwood that referenced this issue Jul 19, 2023
For compatability with webpack, and for consistency with node/server
environment we want to support reading env vars from `process.env`. But
we also want to support Vite's way of doing it, which is exposing them
on `import.meta.env`.

And this is a copy/paste from the code
```
// Vite can automatically expose environment variables, but we
// disable that in `buildFeServer.ts` by setting `envFile: false`
// because we want to use our own logic for loading .env,
// .env.defaults, etc
// The two object spreads below will expose all environment
// variables listed in redwood.toml and all environment variables
// prefixed with REDWOOD_ENV_
```

Note: This does not work for Vite's html `%%` replacement because of
vitejs/vite#13424
But we've got a separate PR that takes care of that in #8929
@github-actions github-actions bot locked and limited conversation to collaborators Aug 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p4-important Violate documented behavior or significantly improves performance (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants