vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
68.61k stars 6.2k forks source link

In the define section of vite.config, it overrides the fine in the node_modules instead of only affecting user files. #18579

Open misakamayako opened 1 week ago

misakamayako commented 1 week ago

Describe the bug

As stated in the title, in this example, the define section replaces the version field exported by the vue package with the data I defined, causing a critical error.

Reproduction

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

Steps to reproduce

just run npm build

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: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @vitejs/plugin-vue2: ^2.3.1 => 2.3.1 
    @vitejs/plugin-vue2-jsx: ^1.1.1 => 1.1.1 
    vite: ^4 => 4.5.5

Used Package Manager

npm

Logs

[commonjs--resolver] Export 'undefined' is not defined (8825:653) in /home/projects/vitejs-vite-jiy9qb/node_modules/vue/dist/vue.runtime.esm.js
file: /home/projects/vitejs-vite-jiy9qb/node_modules/vue/dist/vue.runtime.esm.js:8825:653
8823: }
8824: 
8825: export { EffectScope, computed, customRef, Vue as default, defineAsyncComponent, defineComponent, del, effectScope, getCurrentInstance, getCurrentScope, h, inject, isProxy, isReactive, isReadonly, isRef, isShallow, markRaw, mergeDefaults, nextTick, onActivated, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onDeactivated, onErrorCaptured, onMounted, onRenderTracked, onRenderTriggered, onScopeDispose, onServerPrefetch, onUnmounted, onUpdated, provide, proxyRefs, reactive, readonly, ref$1 as ref, set, shallowReactive, shallowReadonly, shallowRef, toRaw, toRef, toRefs, triggerRef, unref, useAttrs, useCssModule, useCssVars, useListeners, useSlots, "0.0.0", w...
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ^
error during build:
SyntaxError: Export 'undefined' is not defined (8825:653)
    at pp$4.raise (file:///home/projects/vitejs-vite-jiy9qb/node_modules/rollup/dist/es/shared/node-entry.js:21646:13)
    at pp$8.parseTopLevel (file:///home/projects/vitejs-vite-jiy9qb/node_modules/rollup/dist/es/shared/node-entry.js:18919:14)
    at Parser.parse (file:///home/projects/vitejs-vite-jiy9qb/node_modules/rollup/dist/es/shared/node-entry.js:18683:15)
    at _0x528afb.parse (file:///home/projects/vitejs-vite-jiy9qb/node_modules/rollup/dist/es/shared/node-entry.js:18733:35)
    at Graph.contextParse (file:///home/projects/vitejs-vite-jiy9qb/node_modules/rollup/dist/es/shared/node-entry.js:25779:38)
    at tryParse (file:///home/projects/vitejs-vite-jiy9qb/node_modules/vite/dist/node/chunks/dep-b2890f90.js:7398:12)
    at analyzeTopLevelStatements (file:///home/projects/vitejs-vite-jiy9qb/node_modules/vite/dist/node/chunks/dep-b2890f90.js:7417:15)
    at Object.transformAndCheckExports (file:///home/projects/vitejs-vite-jiy9qb/node_modules/vite/dist/node/chunks/dep-b2890f90.js:9447:68)
    at Object.transform (file:///home/projects/vitejs-vite-jiy9qb/node_modules/vite/dist/node/chunks/dep-b2890f90.js:9638:41)
    at eval (file:///home/projects/vitejs-vite-jiy9qb/node_modules/rollup/dist/es/shared/node-entry.js:25569:40)

Validations

bluwy commented 1 week ago

It's expected for the define replacement to handle inside node_modules, you can add things like process.env.LIB_VALUE: ".." that will be replaced in the library, depending if you need to.

For this specific case, you're using Vite 4 which had a much looser replacement handling that can caused issues like this. It should be fixed in Vite 5 now, which worked for me when I upgrade it in the repro.