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

Global constant defined by config.define should not replace variable name #5269

Closed
7 tasks done
niceue opened this issue Oct 12, 2021 · 2 comments · Fixed by #5515
Closed
7 tasks done

Global constant defined by config.define should not replace variable name #5269

niceue opened this issue Oct 12, 2021 · 2 comments · Fixed by #5515

Comments

@niceue
Copy link

niceue commented Oct 12, 2021

Describe the bug

When config Vite:

{
  define: {
    __DEV__: JSON.stringify(process.env.NODE_ENV === 'development')
  }
}

Cause the following code replacement problems
https://github.com/BerkeleyTrue/warning/blob/122d08acff1b42ed421aa027c1f5fabe207f38d5/warning.js#L17

var __DEV__ = process.env.NODE_ENV !== 'production';

This line of code will be replaced with var false = false that Cause js to run an SyntaxError: Unexpected keyword 'false'.

Reproduction

When config Vite:

{
  define: {
    __DEV__: JSON.stringify(process.env.NODE_ENV === 'development')
  }
}

System Info

System:
  OS: macOS 10.15.7
  CPU: (8) x64 Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
  Memory: 1.65 GB / 16.00 GB
  Shell: 3.2.57 - /bin/bash
Binaries:
  Node: 14.18.0 - ~/.nvm/versions/node/v14.18.0/bin/node
  Yarn: 1.19.1 - /usr/local/bin/yarn
  npm: 6.14.15 - ~/.nvm/versions/node/v14.18.0/bin/npm
Browsers:
  Edge: 93.0.961.38
  Firefox: 87.0
  Safari: 15.0
npmPackages:
  @vitejs/plugin-vue: ^1.9.3 => 1.9.3 
  vite: ^2.6.7 => 2.6.5

Used Package Manager

yarn

Logs

17: var __DEV__ = process.env.NODE_ENV !== 'production';
error during build:
SyntaxError: Unexpected keyword 'false' (15:4) in /Users/jony/projects/vue3-proj/node_modules/warning/warning.js
    at Parser.pp$5.raise (/Users/jony/projects/vue3-proj/node_modules/vite/node_modules/rollup/dist/shared/rollup.js:19495:13)
    at Parser.pp$4.checkUnreserved (/Users/jony/projects/vue3-proj/node_modules/vite/node_modules/rollup/dist/shared/rollup.js:19395:12)
    at Parser.pp$4.parseIdent (/Users/jony/projects/vue3-proj/node_modules/vite/node_modules/rollup/dist/shared/rollup.js:19431:10)
    at Parser.pp$2.parseBindingAtom (/Users/jony/projects/vue3-proj/node_modules/vite/node_modules/rollup/dist/shared/rollup.js:18057:15)
    at Parser.pp$1.parseVarId (/Users/jony/projects/vue3-proj/node_modules/vite/node_modules/rollup/dist/shared/rollup.js:17378:18)
    at Parser.pp$1.parseVar (/Users/jony/projects/vue3-proj/node_modules/vite/node_modules/rollup/dist/shared/rollup.js:17361:10)
    at Parser.pp$1.parseVarStatement (/Users/jony/projects/vue3-proj/node_modules/vite/node_modules/rollup/dist/shared/rollup.js:17227:8)
    at Parser.pp$1.parseStatement (/Users/jony/projects/vue3-proj/node_modules/vite/node_modules/rollup/dist/shared/rollup.js:16975:17)
    at Parser.pp$1.parseTopLevel (/Users/jony/projects/vue3-proj/node_modules/vite/node_modules/rollup/dist/shared/rollup.js:16877:21)
    at Parser.parse (/Users/jony/projects/vue3-proj/node_modules/vite/node_modules/rollup/dist/shared/rollup.js:16658:15)
error Command failed with exit code 1.

Validations

@bluwy
Copy link
Member

bluwy commented Oct 12, 2021

Perhaps Vite could implement the same preventAssignment feature as @rollup/plugin-replace too. Currently, it uses its own plugin, which does simple replacements.

Related: #5256, #4271

@jamsch
Copy link

jamsch commented Nov 2, 2021

Got this as well when using the notistack npm package.
Adding __DEV__ to Vite's define config results in the error [commonjs] Unexpected keyword 'false' (731:4) in node_modules/notistack/dist/notistack.esm.js because the npm package includes a variable called __DEV__ at
https://github.com/iamhosseindhv/notistack/blob/8c6d6bf599b5af1599a1849d7f32f59b451cec77/src/utils/warning.ts

@github-actions github-actions bot locked and limited conversation to collaborators Jan 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants