• vue3+vite项目在按需导入vant后, 打包构建时报错


    问题描述:  vue3+vite项目在按需加载的插件是vite-plugin-style-import1.3.0, 在本地运行没问题, 在jsk上构建报错

    1. Error: [vite]: Rollup failed to resolve import "/home/jenkins/agent/workspace/ECC-ecc-pay-mobile-frontend-test/ecc_frontend/pay-mobile/node_modules/vant/lib/vant/es/cell/style" from "src/main.ts".
    2. [2022-08-08 17:40:11] This is most likely unintended because it can break your application at runtime.
    3. [2022-08-08 17:40:11] If you do want to externalize this module explicitly add it to
    4. [2022-08-08 17:40:11] `build.rollupOptions.external`
    5. [2022-08-08 17:40:11] at onRollupWarning (/home/jenkins/agent/workspace/ECC-ecc-pay-mobile-frontend-test/ecc_frontend/pay-mobile/node_modules/vite/dist/node/chunks/dep-c9998dc6.js:41797:19)

    原因分析:

    根据报错信息,发现是vant的样式引入路径不对。
    程序解析为:项目路径/node_modules/vant/lib/vant/es/组件/style     

    实际应该是:项目路径/node_modules/vant/lib/ vant/es/组件/style多了一个vant/lib路径。
    多了一个vant/lib路径。

    而在vite.config.ts中也已经配置了按需加载的代码

    1. import styleImport from "vite-plugin-style-import";
    2. ...
    3. plugins: [
    4. styleImport({
    5. libs: [
    6. {
    7. libraryName: "vant",
    8. esModule: true,
    9. resolveStyle: (name) => `vant/es/${name}/style`
    10. },
    11. ],
    12. }),
    13. ],

    解决方案:

    考虑到vite-plugin-style-import版本更新的原因, 将这个包更新到1.4.1, 并且更新代码

    1. import styleImport, { VantResolve } from "vite-plugin-style-import";
    2. plugins: [
    3. styleImport({
    4. resolves: [VantResolve()],
    5. libs: [
    6. {
    7. libraryName: "vant",
    8. esModule: true,
    9. resolveStyle: (name) => `vant/es/${name}/style`
    10. },
    11. ],
    12. }),
    13. ],

    修改后, 运行没问题,  打包没问题, 大功告成!

  • 相关阅读:
    【Mysql】EXPLAIN
    C++异常处理
    dpdk ring多/单生产者、多/单消费者
    java序列化,看这篇就够了
    消息治理,到底需要治理哪些内容?
    全新锂电池充电板,让充电更加安全
    Starfish OS:以现实为纽带,打造元宇宙新范式
    【微信小程序】微信小程序自定义标题跟随滚动
    [nlp] chathome—家居装修垂类大语言模型的开发和评估
    java设计模式和面向对象编程思想
  • 原文地址:https://blog.csdn.net/qq_43340606/article/details/126244154