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

define settings breaks require paths strings #2878

Closed
6 tasks done
qnp opened this issue Apr 6, 2021 · 1 comment
Closed
6 tasks done

define settings breaks require paths strings #2878

qnp opened this issue Apr 6, 2021 · 1 comment

Comments

@qnp
Copy link
Contributor

qnp commented Apr 6, 2021

Describe the bug

As some lib rely on global, I had to shim global with:

// vite.config.js
import { defineConfig } from 'vite'

export default defineConfig({
  define: {
    global: {},
  },
});

However, I have some JS files that are located in a directory named /global. The import import lib from @/my/global/lib.js is broken at build time when doing vite build:

Could not resolve './my/{}/lib' from src/main.js
error during build:
Error: Could not resolve './my/{}/lib' from src/main.js

which is not what is expected and could lead to lot of problems, because any defined variable can break any import string where the variable name is present. For instance, i have also some files called global-something.js, and they are transformed into {}-something.js...

Reproduction

break-global.zip
yarn && yarn build

System Info

Output of npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers:

  System:
    OS: macOS Mojave 10.14.6
    CPU: (8) x64 Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
    Memory: 136.35 MB / 16.00 GB
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 12.20.1 - ~/.nvm/versions/node/v12.20.1/bin/node
    Yarn: 1.17.3 - ~/.yarn/bin/yarn
    npm: 6.14.10 - ~/.nvm/versions/node/v12.20.1/bin/npm
  Browsers:
    Chrome: 89.0.4389.114
    Firefox: 86.0.1
    Firefox Developer Edition: 71.0
    Firefox Nightly: 76.0a1
    Safari: 14.0.3
    Safari Technology Preview: 13.2
  npmPackages:
    vite: ^2.1.5 => 2.1.5

Used package manager: yarn

Before submitting the issue, please make sure you do the following

@patak-dev
Copy link
Member

The define docs were modified to better explain that it shouldn't be used to replace variables, but only constants here b4a1526 (still not deployed)
The replacements are done with a simple regex during build time, you should only use define for unique constants that are not going to interfere with the rest of the code when being replaced.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
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