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

Vite 4 is removing import #11356

Closed
7 tasks done
johandalabacka opened this issue Dec 13, 2022 · 7 comments
Closed
7 tasks done

Vite 4 is removing import #11356

johandalabacka opened this issue Dec 13, 2022 · 7 comments
Labels
bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@johandalabacka
Copy link

johandalabacka commented Dec 13, 2022

Describe the bug

If I have an import in App.vue which is a "double" import. The imported method is removed on build. While developing it works but it is gone on build. It works if I use vite@^3. I also works if I import is imported directly

App.vue

import api from './api/index.js'
import greetings from './api/greetings.js'


methods: {
  hello() {
    console.log('App.hello)
    api.greetings.hello() // <--- this is removed on build. Using yarn dev works
    greetings.hi() // <--- this is not removed on build
  }
}

api/index.js

import * as greetings from './greetings.js'
export default {
  greetings
}

api/greetings.js

export function hello () {
  console.log('hello from greeings.js')
}

export function hi () {
  console.log('hi from greeings.js')
}

Reproduction

https://github.com/johandalabacka/vite4-build-error

Steps to reproduce

Download repo and install

git clone https://github.com/johandalabacka/vite4-build-error
cd vite4-build-error
yarn install
yarn dev

Open http://localhost:5173/ and pressing the button and the output in the log is:

App.hello
hello from greetings
hi from greetings

But if we build it:

yarn build
yarn preview

Open http://localhost:4173/ and press the button and now output in the log is only:

App.hello
hi from greetings

System Info

System:
    OS: macOS 13.0.1
    CPU: (8) arm64 Apple M1 Pro
    Memory: 71.13 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.12.0 - ~/.nvm/versions/node/v18.12.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.12.0/bin/yarn
    npm: 8.19.2 - ~/.nvm/versions/node/v18.12.0/bin/npm
  Browsers:
    Chrome: 108.0.5359.98
    Firefox: 107.0.1
    Safari: 16.1
  npmPackages:
    @vitejs/plugin-vue: ^4.0.0 => 4.0.0 
    vite: ^4.0.0 => 4.0.1

Used Package Manager

yarn

Logs

No response

Validations

@sapphi-red sapphi-red added bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority) labels Dec 13, 2022
@sapphi-red
Copy link
Member

This was an upstream issue. I created an issue to rollup (rollup/rollup#4751).

@Foster0123
Copy link

Foster0123 commented Dec 14, 2022

@johandalabacka , I did run your code, all u need to do is to add the base property to vite.config.js, it works on production as well
// pasted here
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
base: "./",
plugins: [vue()],
})

@johandalabacka
Copy link
Author

johandalabacka commented Dec 14, 2022

@Foster0123 Thanks. I try it

Update: I tried your fix but it didn't make any difference. api.greetings.hello() is still removed. I don't think base has anything to do with how treeshaking works. It set the base-url used for finding assets and etc

@Foster0123
Copy link

Foster0123 commented Dec 15, 2022

did u run the build on a server ??

u need a local server to serve the build else the script will be blocked by CORS

@johandalabacka
Copy link
Author

@Foster0123 I'm not sure what you mean. Running yarn build and then yarn preview starts a local server serving the built files. And I watched in the built code and the call is missing. I also tried http-server dist and this didn't work either. The output in the log is

App.hello
hi from greetings

with hello from greetings is missing in both cases

@Foster0123
Copy link

Foster0123 commented Dec 16, 2022

yeah it seems to be a vite build issue, most likely a bug

@sapphi-red
Copy link
Member

Closing as this is fixed in rollup 3.7.5.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

3 participants