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

import.meta.env causes build error if a stringified object is used in "define" #13496

Closed
7 tasks done
AnotherHermit opened this issue Jun 12, 2023 · 2 comments · Fixed by #13600
Closed
7 tasks done

import.meta.env causes build error if a stringified object is used in "define" #13496

AnotherHermit opened this issue Jun 12, 2023 · 2 comments · Fixed by #13600
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) regression The issue only appears after a new release

Comments

@AnotherHermit
Copy link

AnotherHermit commented Jun 12, 2023

Describe the bug

It seems that there is a mutually exclusive issue with import.meta.env at the moment.
If there is a stringified object defined as a replacement value (e.g. import.meta.env.VITE_FOO: JSON.stringify({ a: 'a', b: 'b' }) then the build will error when it encounters a bare import.meta.env.
We have currently run in to this issue with two libraries we are using vite-plugin-sentry and zustand, where one uses object values and one uses the bare import.meta.env.

The documentation doesn't mention any restriction on which values can be used and it was suggested in a previous issue (#12857) that objects could be used. So I am assuming this is a bug in the current implementation.
This seems to only be a problem when building the project, during dev mode there are no problems.

My guess is that the bug was introduced in #13003 as there wasn't an issue before this change

Reproduction

https://stackblitz.com/edit/vitejs-vite-ttdkfj?file=main.js,vite.config.js&terminal=dev

Steps to reproduce

Run npm run build and the build will crash.
Comment out either the object in the config or the bare env access in the main file and the build will succeed.

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (2) 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: 9.4.2 - /usr/local/bin/npm
  npmPackages:
    vite: ^4.3.9 => 4.3.9

Used Package Manager

npm

Logs

vite v4.3.9 building for production...
✓ 2 modules transformed.
✓ built in 168ms
[vite:build-import-analysis] Parse error @:1:1
file: /home/projects/vitejs-vite-ttdkfj/main.js
error during build:
Error: Parse error @:1:1
    at parse$e (file://file:///home/projects/vitejs-vite-ttdkfj/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:16379:355)
    at Object.transform (file://file:///home/projects/vitejs-vite-ttdkfj/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:45127:27)

Validations

@stackblitz
Copy link

stackblitz bot commented Jun 12, 2023

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

@sapphi-red sapphi-red added the p3-minor-bug An edge case that only affects very specific usage (priority) label Jun 13, 2023
@reintroducing
Copy link

I just spent the whole morning trying to figure out what was going on with a production build and arrived at this same conclusion. The build works fine under 4.3.3 but fails under 4.3.4. We are also using the same dependencies listed here (zustand and vite-plugin-sentry). The build works fine when run locally, but in GHA it shows the following error under 4.3.4:

vite v4.3.4 building for production...
transforming...
✓ 145 modules transformed.
✓ built in 3.45s
> Rewriting sources
> Adding source map references
> Nothing to upload
Expecting Unicode escape sequence \uXXXX (Note that you need plugins to import files that are not JavaScript)
file: /home/runner/work/fe-care-coach/fe-care-coach/node_modules/zustand/esm/index.mjs:19:22
17: }
18: const createImpl = (createState) => {
19:   if ((import.meta.env && import.meta.env.MODE) !== "production" && typeof createState !== "function") {
                          ^
20:     console.warn(
21:       "[DEPRECATED] Passing a vanilla store will be unsupported in a future version. Instead use `import { useStore }...
file:///home/runner/work/fe-care-coach/fe-care-coach/node_modules/rollup/dist/es/shared/node-entry.js:2124
        base = Object.assign(new Error(base.message), base);
                             ^

Error [RollupError]: Expecting Unicode escape sequence \uXXXX (Note that you need plugins to import files that are not JavaScript)
    at error (file:///home/runner/work/fe-care-coach/fe-care-coach/node_modules/rollup/dist/es/shared/node-entry.js:2124:30)
    at Module.error (file:///home/runner/work/fe-care-coach/fe-care-coach/node_modules/rollup/dist/es/shared/node-entry.js:1[34](https://github.com/patientpattern/fe-care-coach/actions/runs/5326139913/jobs/9647769420#step:5:35)63:16)
    at Module.tryParse (file:///home/runner/work/fe-care-coach/fe-care-coach/node_modules/rollup/dist/es/shared/node-entry.js:14184:25)
    at Module.setSource (file:///home/runner/work/fe-care-coach/fe-care-coach/node_modules/rollup/dist/es/shared/node-entry.js:1[37](https://github.com/patientpattern/fe-care-coach/actions/runs/5326139913/jobs/9647769420#step:5:38)86:[39](https://github.com/patientpattern/fe-care-coach/actions/runs/5326139913/jobs/9647769420#step:5:40))
    at ModuleLoader.addModuleSource (file:///home/runner/work/fe-care-coach/fe-care-coach/node_modules/rollup/dist/es/shared/node-entry.js:23782:20) {
  cause: SyntaxError: Expecting Unicode escape sequence \uXXXX (19:518)
      at pp$4.raise (file:///home/runner/work/fe-care-coach/fe-care-coach/node_modules/rollup/dist/es/shared/node-entry.js:21186:13)
      at pp.invalidStringToken (file:///home/runner/work/fe-care-coach/fe-care-coach/node_modules/rollup/dist/es/shared/node-entry.js:23068:10)
      at pp.readWord1 (file:///home/runner/work/fe-care-coach/fe-care-coach/node_modules/rollup/dist/es/shared/node-entry.js:23232:16)
      at pp.readWord (file:///home/runner/work/fe-care-coach/fe-care-coach/node_modules/rollup/dist/es/shared/node-entry.js:23251:19)
      at pp.readToken (file:///home/runner/work/fe-care-coach/fe-care-coach/node_modules/rollup/dist/es/shared/node-entry.js:22539:19)
      at Parser.readToken (file:///home/runner/work/fe-care-coach/fe-care-coach/node_modules/rollup/dist/es/shared/node-entry.js:25069:24)
      at pp.nextToken (file:///home/runner/work/fe-care-coach/fe-care-coach/node_modules/rollup/dist/es/shared/node-entry.js:22532:15)
      at pp.next (file:///home/runner/work/fe-care-coach/fe-care-coach/node_modules/rollup/dist/es/shared/node-entry.js:22493:8)
      at pp$5.parseObj (file:///home/runner/work/fe-care-coach/fe-care-coach/node_modules/rollup/dist/es/shared/node-entry.js:20804:8)
      at pp$5.parseExprAtom (file:///home/runner/work/fe-care-coach/fe-care-coach/node_modules/rollup/dist/es/shared/node-entry.js:20546:17) {
    pos: 1104,
    loc: Position { line: 19, column: 518 },
    raisedAt: 1104
  },
  code: 'PARSE_ERROR',
  id: '/home/runner/work/fe-care-coach/fe-care-coach/node_modules/zustand/esm/index.mjs',
  pos: 1104,
  loc: {
    column: 22,
    file: '/home/runner/work/fe-care-coach/fe-care-coach/node_modules/zustand/esm/index.mjs',
    line: 19
  },
  frame: '17: }\n' +
    '18: const createImpl = (createState) => {\n' +
    '19:   if ((import.meta.env && import.meta.env.MODE) !== "production" && typeof createState !== "function") {\n' +
    '                          ^\n' +
    '20:     console.warn(\n' +
    '21:       "[DEPRECATED] Passing a vanilla store will be unsupported in a future version. Instead use `import { useStore }...',

@bluwy bluwy added the regression The issue only appears after a new release label Jun 21, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jul 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) regression The issue only appears after a new release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants