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构建多页面应用时,构建产物丢失页面JS文件的问题建议 #16561

Closed
4 tasks done
windylaugit opened this issue Apr 30, 2024 · 2 comments
Labels
duplicate This issue or pull request already exists

Comments

@windylaugit
Copy link

windylaugit commented Apr 30, 2024

Description

遇到的问题

一个偶然的机会,我在开发一个使用Vite搭建的多页面应用项目时,使用了下面这样的配置:

export default defineConfig({
  build: {
    rollupOptions: {
      input: {
        index: 'index.html',
        'pageA/index': 'src/pages/pageA/index.html',
        'pageB/index': 'src/pages/pageB/index.html',
      }
    }
  }
})

(为了说明问题,我简化了不必要的配置项)
该项目使用vite server 命令运行时,一切正常,页面表现也如预期。
而当我使用vite buiild 命令构建时,构建过程正常, 但构建产物中的*/index.html等文件,丢失了本应该有的 script type='module' js文件引入, 而这些js文件实际已经被构建到dist/asset目录下。
经过几个小时的各种测试与分析,最终发现,正确的配置应当如下:

正确配置

import nodePath from 'node:path'

export default defineConfig({
  build: {
    rollupOptions: {
      input: {
        index: 'index.html',
        'pageA/index': nodePath.resolve(__dirname, 'src/pages/pageA/index.html'),
        'pageB/index': nodePath.resolve(__dirname, 'src/pages/pageB/index.html')
      }
    }
  }
})

结论

我认为这并非vitejs的一个bug,但是vitejs这么受前端开发者喜爱的工具,是否可以考虑对此进行一些有益的改进与优化?这应当会给vite的初学者解决掉一个难解的困惑, 也会让vite更符合文档中宣称的:"这确保了与开发服务器的工作方式保持一致的结构。".
非常感谢!

Suggested solution

[建议]
建议在 vite build命令中,对于多页面应用(mpa),对于使用了相对路径作为入口文件的配置,给予必要的控制台警告或者 强制报错。

Alternative

No response

Additional context

No response

Validations

Tasks

No tasks being tracked yet.
@sapphi-red
Copy link
Member

Duplicate of #15992

@sapphi-red sapphi-red marked this as a duplicate of #15992 Apr 30, 2024
@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Apr 30, 2024
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed enhancement: pending triage labels Apr 30, 2024
@windylaugit
Copy link
Author

Duplicate of #15992

感谢答复!
我仔细阅读了#15992以及相关的issue,并在更新vite版本到v5.2.11 之后,确认该问题已经被修复了!。

不过,我建议官方能在文档上进行一些必要说明,以避免一些未能及时更新新版vite的项目出现错误。

@github-actions github-actions bot locked and limited conversation to collaborators May 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants