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

Defining global object in vite config breaks any imports with global in their name #12814

Closed
7 tasks done
jsakas opened this issue Apr 10, 2023 · 3 comments
Closed
7 tasks done

Comments

@jsakas
Copy link

jsakas commented Apr 10, 2023

Describe the bug

If I add something like this in my Vite config:

// vite.config.js
export default {
  define: {
    global: {},
  },
};

And also have some imports with global in the name like:

import { globalReducer } from './global-reducer'

The build process fails with this error message:

Could not resolve "./{}-reducer" from "main.js"

This only happens in vite build, not during development.

Reproduction

https://stackblitz.com/edit/vitejs-vite-dzocfw?file=main.js

Steps to reproduce

Run npm run build in the terminal in the stackblitz URL.

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.0-beta.2 => 4.3.0-beta.4

Used Package Manager

npm

Logs

No response

Validations

@stackblitz
Copy link

stackblitz bot commented Apr 10, 2023

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

@jsakas jsakas changed the title Defining `global object in vite config breaks any imports with global in their name Defining global object in vite config breaks any imports with global in their name Apr 10, 2023
@jsakas
Copy link
Author

jsakas commented Apr 10, 2023

I should add that the reason we even added global: {} was because we are receiving this error:

index.js:19 Uncaught ReferenceError: global is not defined
    at ensureCache2 (index.js:19:2)
    at ../../node_modules/global-cache/index.js (index.js:29:13)
    at __require2 (chunk-TFWDKVI3.js?v=3373edb8:18:50)
    at ../../node_modules/react-with-styles-interface-css/dist/index.js (index.js:12:43)
    at __require2 (chunk-TFWDKVI3.js?v=3373edb8:18:50)
    at ../../node_modules/react-with-styles-interface-css/index.js (index.js:2:18)
    at __require2 (chunk-TFWDKVI3.js?v=3373edb8:18:50)
    at ../../node_modules/react-dates/lib/utils/registerCSSInterfaceWithDefaultTheme.js

Due to the dependency tree of react-dates -> react-with-styles-interface-css -> global-cache.

@jsakas
Copy link
Author

jsakas commented Apr 10, 2023

Further digging, I guess this is warned against in the docs... https://vitejs.dev/config/shared-options.html#define

@jsakas jsakas closed this as completed Apr 10, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Apr 25, 2023
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

1 participant