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

Weird parsing error in build when using import.meta.env inside string #7840

Closed
7 tasks done
leonbloy opened this issue Apr 21, 2022 · 1 comment
Closed
7 tasks done

Comments

@leonbloy
Copy link

Describe the bug

A cryptic parser error is thrown by vite build, apparenttly when a static string inside jsx includes the string literal "import.meta.env"

Steps:
Create a clean react app with

npm create vite@latest my-vue-app -- --template react
cd my-vue-app
npm install

Then overwrite src/App.jsx with this content:

import React from 'react';

function App() {
	console.log('import.meta.env.MODE', import.meta.env.MODE);
	return (
		<div>hi</div>	
	);
};

export default App;

Then
> npm run dev
works as expected (and prints to console: import.meta.env.MODE development ) but
> npm run build
gives error:

> my-vue-app@0.0.0 build
> vite build

vite v2.9.5 building for production...
✓ 11 modules transformed.
[rollup-plugin-dynamic-import-variables] Unexpected token (4:16)
file: C:/temp/my-vue-app/src/App.jsx:4:16
error during build:
SyntaxError: Unexpected token (4:16)
    at Parser.pp$4.raise (C:\temp\my-vue-app\node_modules\rollup\dist\shared\rollup.js:19680:13)
    at Parser.pp$9.unexpected (C:\temp\my-vue-app\node_modules\rollup\dist\shared\rollup.js:16976:8)
    at Parser.pp$9.expect (C:\temp\my-vue-app\node_modules\rollup\dist\shared\rollup.js:16970:26)
    at Parser.pp$5.parseExprList (C:\temp\my-vue-app\node_modules\rollup\dist\shared\rollup.js:19547:12)
    at Parser.pp$5.parseSubscript (C:\temp\my-vue-app\node_modules\rollup\dist\shared\rollup.js:18909:25)
    at Parser.pp$5.parseSubscripts (C:\temp\my-vue-app\node_modules\rollup\dist\shared\rollup.js:18866:24)
    at Parser.pp$5.parseExprSubscripts (C:\temp\my-vue-app\node_modules\rollup\dist\shared\rollup.js:18850:21)
    at Parser.pp$5.parseMaybeUnary (C:\temp\my-vue-app\node_modules\rollup\dist\shared\rollup.js:18813:17)
    at Parser.pp$5.parseExprOps (C:\temp\my-vue-app\node_modules\rollup\dist\shared\rollup.js:18740:19)
    at Parser.pp$5.parseMaybeConditional (C:\temp\my-vue-app\node_modules\rollup\dist\shared\rollup.js:18723:19)

The problem seems to be the import.meta.env inside the string. If I change the line to
console.log('impArt.meta.env.MODE', import.meta.env.MODE);
it works.

Reproduction

https://stackblitz.com/edit/vitejs-vite-g2q1j9?file=src/App.jsx

System Info

System:
    OS: Windows 10 10.0.19044
    CPU: (6) x64 Intel(R) Core(TM) i5-9400 CPU @ 2.90GHz
    Memory: 8.29 GB / 15.87 GB
  Binaries:
    Node: 16.14.0 - ~\scoop\apps\nvm\current\nodejs\nodejs\node.EXE
    npm: 8.3.1 - ~\scoop\apps\nvm\current\nodejs\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (100.0.1185.44)

Used Package Manager

npm

Logs

No response

Validations

@bluwy
Copy link
Member

bluwy commented Apr 21, 2022

Duplicate of #3304. Vite replaces the env vars with regex, so a workaround for now is to do something like 'import.meta\0.env.MODE'.

@bluwy bluwy closed this as completed Apr 21, 2022
@github-actions github-actions bot locked and limited conversation to collaborators May 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants