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

在和自动导入插件同时使用时的问题优化 #21

Closed
nineszsoo opened this issue Sep 8, 2022 · 3 comments
Closed

在和自动导入插件同时使用时的问题优化 #21

nineszsoo opened this issue Sep 8, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@nineszsoo
Copy link

当我在项目中同时使用到了 unplugin-auto-import 自动导入相关 API 时, 打包的时候会因为 vite-plugin-externals 先进行外部库的处理转换,而导致 unplugin-auto-import 自动导入的 API 失效。

下面是我查看的使用自动导入 ref 时插件的执行顺序。

image

然后我在 node_modules 下面修改了 vite-plugin-externalsindex.js 中关于该插件的返回对象(添加了属性enforce: 'post')后就可以正常使用了。

image

希望老哥看下有没有更好的处理方法优化一下。

@crcong crcong added the enhancement New feature or request label Sep 10, 2022
@Clareyang213
Copy link

我按这种方式修改之后,打包后,unplugin-auto-import 自动导入还是有问题,还有没有其他解决方案

@Clareyang213
Copy link

解决了,vite打包的时候,自动导入应该使用'unplugin-auto-import/rollup'

`import AutoImportVite from 'unplugin-auto-import/vite';
import AutoImportRollup from 'unplugin-auto-import/rollup';

// cdn链接
const cdn = {
vue: 'Vue',
// 👇 配置 vue-demi 全局变量 👇
'vue-demi': 'VueDemi',
pinia: 'Pinia',
'vue-router': 'VueRouter'
};
export default defineConfig(({ command }) => {
return {
plugins: [
vue(),
viteExternalsPlugin(cdn.externals),
command !== 'build' &&
AutoImportVite({
// 后续vue/vue-router/pinia的API都不需要再单独import到setup里面了
imports: ['vue', 'vue-router', 'pinia']
})
],
build: {
rollupOptions: {
plugins: [AutoImportRollup({ imports: ['vue', 'vue-router', 'pinia'] })]
}
}
};
});`

@crcong crcong closed this as completed in 08ddba0 Jan 29, 2023
@cq-cute
Copy link

cq-cute commented Aug 6, 2024

解决了,vite打包的时候,自动导入应该使用'unplugin-auto-import/rollup'

`import AutoImportVite from 'unplugin-auto-import/vite'; import AutoImportRollup from 'unplugin-auto-import/rollup';

// cdn链接 const cdn = { vue: 'Vue', // 👇 配置 vue-demi 全局变量 👇 'vue-demi': 'VueDemi', pinia: 'Pinia', 'vue-router': 'VueRouter' }; export default defineConfig(({ command }) => { return { plugins: [ vue(), viteExternalsPlugin(cdn.externals), command !== 'build' && AutoImportVite({ // 后续vue/vue-router/pinia的API都不需要再单独import到setup里面了 imports: ['vue', 'vue-router', 'pinia'] }) ], build: { rollupOptions: { plugins: [AutoImportRollup({ imports: ['vue', 'vue-router', 'pinia'] })] } } }; });`

这样配置之后打包的时候还是没有自动引入,你用的哪个版本啊

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

No branches or pull requests

4 participants