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

vite6.0.0-beta.8打包预览报错 #37

Closed
xiaoxian521 opened this issue Nov 5, 2024 · 6 comments
Closed

vite6.0.0-beta.8打包预览报错 #37

xiaoxian521 opened this issue Nov 5, 2024 · 6 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@xiaoxian521
Copy link

作者,你好,目前在升级vite6时遇到vite-plugin-fake-server打包后报错,如下图

image

vite6.0.0-beta.8已接近正式版 https://github.com/vitejs/vite/milestone/17

可能是vite6正式引入了 环境概念 带来了一些Break Change

@condorheroblog
Copy link
Owner

错误显示无法获取 path-to-regexp 的 match 方法,path-to-regexp 是通过 Vite 打包到浏览器的

export async function buildPackage(packageName: string) {
const camelCasePackageName = packageName.replace(/-\w/g, str => str[1].toUpperCase());
// Call the build function with build configurations
const result = await build({
// Do not use a configuration file
configFile: false,
build: {
// Do not output files
write: false,
lib: {
// Entry file is the specified package path
entry: require.resolve(packageName),
// Convert package name to camel case
name: camelCasePackageName,
// Output format is iife
formats: ["iife"],
// Output file name is the same as the package name
fileName: packageName,
},
rollupOptions: {
output: {
// Export method is named export
exports: "named",
// Allow extension of existing global variables
extend: true,
},
},
// Do not minify
minify: false,
},
});
// Check if result is an array, if so, take the first element, otherwise use result directly
const _result = Array.isArray(result) ? result[0] : result;
// If the _result does not have an output property, return
if (!("output" in _result)) {
return;
}
return `window.__VITE__PLUGIN__FAKE__SERVER__.${camelCasePackageName} = (function() { ${_result.output[0].code} return this.${camelCasePackageName}; }).apply({});`;
}

但是在 Vite6 中的输出不一致了 😅

image

@condorheroblog condorheroblog self-assigned this Nov 5, 2024
@condorheroblog condorheroblog added bug Something isn't working enhancement New feature or request labels Nov 5, 2024
@xiaoxian521
Copy link
Author

感谢你的回答,貌似做个判断,能兼容下vite多版本 😄

@condorheroblog
Copy link
Owner

不知道这算不算是他们的一个 bug😂

@condorheroblog
Copy link
Owner

vitejs/vite#18574

管他的,先提一个 bug 😂

@condorheroblog
Copy link
Owner

Rollup 把 strictRequires 默认值从 auto 改为 true 了,我在打包的时候强制指定打包参数 strictRequires 还为 "auto"。

build: {
	commonjsOptions: {
		strictRequires: "auto",
	},
},

同时在代码中增加判断逻辑——没有别名导出就使用默认导出。

@condorheroblog
Copy link
Owner

v2.1.3 已经发布

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants