作者:admin,发布日期:2021-02-06
阅读:23518;评论:594

在vite运行build时,默认会把所有的库文件合并到一个大文件中,产生一个打包后的js文件,其中会包含各种库文件,会导致最后打包完成后的文件过大,同时减慢打包速度,这时可以把库文件代码使用cdn访问,加快打包和网页打开速度。

首先需要确认的是,vite在开发时使用的是自己的一套机制,也就是将代码转换成浏览器原生可以使用的type="module",然后让浏览器的js引擎去直接运行js,不再需要使用构建工具打包,所以可以做到秒开。而vite的编译使用的是另外一个开源的包rollup,这也是这篇文章的主角,而且需要注意的是,开发时dev server是不会去使用cdn的代码的,只会引用我们安装的node_modules内部的代码。

但是这个时候又出现问题了,搞定这些问题前前后后花了我大半天的时间,终于功夫不负有心人,搞定了。

使用rollup自带的ouput.globals

首先参考的是vite用户在github的一条issues,其中尤大提到了rollupOptions.external这个东西,然后我参考了vite的文档,文档中指向了rollup的配置。

Snipaste_2021-02-06_22-56-33.jpg

跳转之后就可以看到官方的文档的第一条就是说明的external,经过百度查找,我打开了第一篇csdn的文章。

https://blog.csdn.net/qq_29722281/article/details/95596372

内部提到了使用globals来指定全局模块名称,这个地方给的样例代码其实已经有问题了:

20190712105338146.png

新版的rollup更新后,globals选项已经被移动到了output.globals,如果不修改则会发生以下错误:

Unknown input options: globals. Allowed options: acorn, acornInjectPlugins, cache, context, experimentalCacheExpiry, external, inlineDynamicImports, input, manualChunks, moduleContext, onwarn, perf, plugins, preserveEntrySignatures, preserveModules, preserveSymlinks, shimMissingExports, strictDeprecations, treeshake, watch

提示我们globals选项不存在,我们需要将globals放入output中才可以使用。

于是我就按照文档,添加了一个globals选项,同时使用external选项来排除打包。

当时代码是这样的:

external: ["vue"],
output: {
    globals: {
        vue: "Vue",
    },
},

添加完成之后,问题就开始出现了:

rollup Failed to resolve module specifier "vue"

查看打包后的代码可以发现,我们的import代码还是原样import了vue,但是这种语法浏览器其实是解析不了了,因为网页没有node_modules,浏览器也就没有办法去别处找这个包。

就是因为这个问题,前前后后搞了半天,终于在谷歌搜到了一个rollup用户的issue,其中的用户和我具有一样的情况,也就是globals选项无效。

参考下方地址:

https://github.com/rollup/rollup/issues/3188

然后经过一番研究,我才找到了最终的问题(当然我之前也有思考过是不是不支持,但是用其他的插件也是无效):

output.globals only sets the global variable for external imports in IIFE bundles (and UMD bundles when they are used in a script tag), in other formats it has no effect. https://www.npmjs.com/package/rollup-plugin-external-globals is probably what you want.

Ah sorry, I see you found that plugin, is it working for you? There are technical reasons this is not supported in core, mostly because it would need to work very differently for non-IIFE formats, potentially provide larger output after minification, and it is totally unclear how to handle this with UMD.

翻译过来,就是说自带的global只支持iife或者umd打包的库文件,这时候我才恍然大悟,我使用的包都是commonjs打包的(关于各种打包的方式使用大家可以谷歌学习),所以才出现这种问题,其实就是本身不支持(不过经过测试其他方式的好像也不可以,可能就是vite本身的问题吧)。

反正经过这个提示,问题已经大概解决了,我们只需要使用插件就能解决这个问题。

使用rollup-plugin-external-globals插件来解决问题

参考:https://github.com/rollup/rollup/issues/2374

插件地址:https://www.npmjs.com/package/rollup-plugin-external-globals

安装插件

yarn add -D rollup-plugin-external-globals

添加配置

使用方法与上方定义方法几乎相同,传入参数给插件初始化方法就行。

      plugins: [
        commonjs(),
        externalGlobals({
          vue: "Vue",
          "ant-design-vue": "antd",
          moment: "moment",
        }),
      ],

参数对解释:

  • vue - 这里需要和external对应,这个字符串就是(import xxx from aaa)中的aaa,也就是包的名字

  • Vue - 这个是js文件导出的全局变量的名字,比如说vue就是Vue,查看源码或者参考作者文档可以获得

下面是全部vite.config.js,供参考:

import vue from "@vitejs/plugin-vue";
import commonjs from "rollup-plugin-commonjs";
import externalGlobals from "rollup-plugin-external-globals";

/**
 * https://vitejs.dev/config/
 * @type {import('vite').UserConfig}
 */
export default {
  plugins: [vue()],
  build: {
    rollupOptions: {
      external: ["vue", "ant-design-vue", "moment"],
      plugins: [
        commonjs(),
        externalGlobals({
          vue: "Vue",
          "ant-design-vue": "antd",
          moment: "moment",
        }),
      ],
    },
  },
};

在index.html中导入静态文件

修改根目录下的index.html,添加cdn文件:

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ant-design-vue@2.0.0-rc.9/dist/antd.min.css">
  <script src="https://cdn.jsdelivr.net/npm/vue@3.0.5/dist/vue.global.prod.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/moment@2.29.1/moment.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/ant-design-vue@2.0.0-rc.9/dist/antd.js"></script>

整个文件参考下方:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <link rel="icon" href="/favicon.ico" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ant-design-vue@2.0.0-rc.9/dist/antd.min.css">
  <script src="https://cdn.jsdelivr.net/npm/vue@3.0.5/dist/vue.global.prod.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/moment@2.29.1/moment.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/ant-design-vue@2.0.0-rc.9/dist/antd.js"></script>
  <title>Vite App</title>
</head>

<body>
  <div id="app"></div>
  <script type="module" src="/src/main.js"></script>
</body>

</html>

编译测试

vite中提供了preview,可以供我们预览编译后的结果

yarn run build
yarn run serve

Snipaste_2021-02-06_23-20-31.jpg

打开网页发现原先报错已经不存在,问题解决。

Snipaste_2021-02-06_23-21-02.jpg

你可能感兴趣的文章

评论区

已有594位网友发表了看法:

1L luoyi  2021-05-07 16:21:23 回复
大佬牛逼,这问题困扰了我几天终于解决了
2L 访客  2021-05-20 17:14:42 回复
问一下 main.js js 中在如何写呢
2L admin  2021-05-21 09:33:46 回复
@访客 和原来一样
2L 访客  2022-01-06 16:14:33 回复
@访客 你的问题解决了么?用cdn 形式直接加载组件库么
3L 访客  2022-01-06 16:12:59 回复
main.js 代码如下
import { createApp } from 'vue'
import App from './App.vue'
import './index.css'
createApp(App).mount('#app')
在vue 直接用 ant-design-vue 的组件就不生效报错了,报错如下:
vue.js:8068 [Vue warn]: Failed to resolve component: a-button
If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement.
at
at
这个为什么组件找不到啊?大佬
3L admin  2022-01-10 16:36:23 回复
@访客 https://blog.craftyun.cn/post/231.html
得看这个,建议你加群问,要不然这边回复你也看不到
4L 访客  2022-01-18 14:56:42 回复
大佬,我按你上图使用启动之后报错Failed to resolve module specifier "vue". Relative references must start with either "/", "./", or "../". 这个是咋回事
4L admin  2022-01-19 15:42:24 回复
@访客 用esm试试
4L 访客  2022-04-02 14:45:20 回复
@访客 解决了吗兄弟
5L 访客  2022-02-14 17:16:52 回复
报错了
Uncaught TypeError: Failed to resolve module specifier "vue". Relative references must start with either "/", "./", or "../".
5L 访客  2022-04-01 20:06:33 回复
@访客 解决了吗兄弟
6L 访客  2023-06-19 10:26:57 回复
弄了好几天,也总算是解决了
6L 访客  2023-12-24 15:08:43 回复
@访客 怎么解决的
7L micateam  2023-08-28 21:10:43 回复
23年8月了,这个问题还在 没解决,我在单页编译没事把他编译成umd模式即可,多页不支持umd esm模式它就找不到vue了
8L 游客  2024-07-11 13:03:02 回复
楼主是男的还是女的?http://n9lavr.huagengshe.com
9L 游客  2024-07-11 14:45:18 回复
楼主写的很经典!http://shkg.shhtjt.cn
10L 游客  2024-07-11 14:54:00 回复
刚分手,心情不好!http://xzd8t.cryptoonair.com
11L 游客  2024-07-11 15:28:52 回复
看帖不回帖的人就是耍流氓,我回复了!http://www.ddman.net
12L 游客  2024-07-11 15:52:31 回复
楼上的很有激情啊!http://fb4.haitaotex.com
13L 游客  2024-07-11 15:55:03 回复
楼主内心很强大!http://3mb49.quatans.com
14L 游客  2024-07-11 16:01:50 回复
支持一个http://b92hf.haitaotex.com
15L 游客  2024-07-11 16:49:11 回复
太邪乎了吧?http://yh2pd.xiyancai.cn
16L 游客  2024-07-11 17:35:48 回复
信楼主,考试不挂科!http://mllo.shjsm.com
17L 游客  2024-07-11 17:39:23 回复
网站做得不错http://95te.shjsm.com
18L 游客  2024-07-11 17:59:44 回复
很有品味!http://xx4zzn.shjsm.com
19L 游客  2024-07-11 18:37:50 回复
看在楼主的面子上,认真回帖!http://2fly5.mldektwx.com/20240711/5.html
20L 游客  2024-07-11 18:56:01 回复
每次看到楼主的帖子都有惊吓!http://aesds.gdjianye.com
21L 游客  2024-07-11 19:30:53 回复
楼主是一个典型的文艺青年啊!http://8f6tp.huichengyu.com
22L 游客  2024-07-11 19:34:14 回复
谢谢楼主的分享!http://ocfuu.gdjianye.com
23L 游客  2024-07-11 19:52:11 回复
被楼主的逻辑打败了!http://15muo.lhkzyzb.com/2/5.html
24L 游客  2024-07-11 21:04:23 回复
楼主的帖子提神醒脑啊!http://pio9h.huagengshe.com
25L 游客  2024-07-11 21:05:03 回复
好东西,赞一个!http://g49d0.sxcinvest.com.cn
26L 游客  2024-07-11 22:36:39 回复
楼主很有艺术范!http://dd3.randisoft.com
27L 游客  2024-07-11 23:06:06 回复
我和我的小伙伴都惊呆了!http://uum7.jinglx.com
28L 游客  2024-07-11 23:26:02 回复
楼主很有艺术范!http://12saw.gzhkzyxy.com/07/4.html
29L 游客  2024-07-11 23:26:15 回复
今天是个特别的日子,值得纪念!http://x1veu.znboke.com
30L 游客  2024-07-11 23:42:02 回复
收藏了,很不错的内容!http://kfu.j6cq.com
31L 游客  2024-07-11 23:45:03 回复
顶一下,收藏了!http://9l9u2.hbhjgy.com
32L 游客  2024-07-11 23:53:57 回复
今天怎么了,什么人都出来了!http://xdle.czdy2010.cn
33L 游客  2024-07-11 23:58:50 回复
我裤子脱了,纸都准备好了,你就给我看这个?http://tam6.huichengyu.com
34L 游客  2024-07-12 00:55:37 回复
求加**!http://0phe4a.huagengshe.com
35L 游客  2024-07-12 01:07:39 回复
世界末日我都挺过去了,看到楼主我才知道为什么上帝留我到现在!http://1k8v6.cryptoonair.com
36L 游客  2024-07-12 01:20:14 回复
缺乏激情了!http://c0rx.multigruporadio.com
37L 游客  2024-07-12 01:29:56 回复
雷锋做好事不留名,都写在帖子里!http://qq8.blxhf.com
38L 游客  2024-07-12 01:31:16 回复
怎么我回帖都没人理我呢?http://2l7oul.blxhf.com
39L 游客  2024-07-12 01:31:25 回复
顶一下,收藏了!http://qinz8.blxhf.com
40L 游客  2024-07-12 01:59:49 回复
楼主很有艺术范!http://ljf.bjhuayee.com
41L 游客  2024-07-12 02:01:14 回复
怪事年年有,今年特别多!http://n381.gyyy01.com
42L 游客  2024-07-12 02:57:16 回复
看帖不回帖都是耍流氓!http://lxxata.songyang2008.com
43L 游客  2024-07-12 03:20:40 回复
楼主是我最崇拜的人!http://sam2.njkrx.cn
44L 游客  2024-07-12 03:53:09 回复
楼主是在找骂么?http://3jrt4.dlt32.com/w/4.html
45L 游客  2024-07-12 03:57:23 回复
收藏了,改天让朋友看看!http://3x04z7.yulaimetal.com
46L 游客  2024-07-12 05:02:21 回复
感觉不错!http://lostockhallband.com/news/86d099463.html
47L 游客  2024-07-12 05:16:13 回复
收藏了,楼主加油!http://037c6.yigaoyiqi.com
48L 游客  2024-07-12 05:18:23 回复
不错的帖子,值得收藏!http://b2jlea.dqxwire.com
49L 游客  2024-07-12 06:21:40 回复
你觉得该怎么做呢?http://6t4ve.tjzhtdgt.com/01/4.html
50L 游客  2024-07-12 06:33:27 回复
收藏了,改天让朋友看看!http://07nih.nnsxjd.com
51L 游客  2024-07-12 06:34:18 回复
每天顶顶贴,一身轻松啊!http://rim.haitaotex.com
52L 游客  2024-07-12 07:11:46 回复
看帖、回帖、拿分、走人http://aug.133mv.com
53L 游客  2024-07-12 07:13:20 回复
看帖回帖一条路!http://79i.lanweish.cn
54L 游客  2024-07-12 07:15:02 回复
支持一个http://0tp.133mv.com
55L 游客  2024-07-12 07:15:48 回复
好无聊啊!http://dkq.mztxy.com
56L 游客  2024-07-12 07:36:35 回复
怪事年年有,今年特别多!http://1eg.234en.com
57L 游客  2024-07-12 07:49:16 回复
一口气看完了,我要下去回味回味了!http://3ki.mztxy.com
58L 游客  2024-07-12 09:06:11 回复
鸟大了,什么林子都敢进啊!http://gffho.tongbanjiang.com/07/5.html
59L 游客  2024-07-12 09:47:19 回复
帖子好乱!http://0uopw.cdmjhjz.com/5/5.html
60L 游客  2024-07-12 11:04:55 回复
终于看完了,很不错!http://i417.cryptoonair.com
61L 游客  2024-07-12 13:58:37 回复
帖子很有深度!http://6filz.bjsyjh868.com/01/3.html
62L 游客  2024-07-12 15:54:53 回复
吹牛的人越来越多了!http://77cai.cn/news/84a399555.html
63L 游客  2024-07-12 16:16:56 回复
我默默的回帖,从不声张!http://losultimosteatro.com/news/2e099547.html
64L 游客  2024-07-12 17:15:41 回复
很给力!http://lnxw16.302tuan.com
65L 游客  2024-07-12 18:54:36 回复
怪事年年有,今年特别多!http://jxwgc.1005mu.com/f/4.html
66L 游客  2024-07-12 19:16:04 回复
楼主的帖子越来越有深度了!http://www.hntbhz.cn/post/10.html
67L 游客  2024-07-12 21:09:58 回复
十分赞同楼主!http://29g4a.lyshuidai.com/w/5.html
68L 游客  2024-07-12 21:30:51 回复
祖国尚未统一,我却天天灌水,好内疚!http://x90.jkd4whd.cn
69L 访客  2024-07-12 23:13:22 回复
微商货源网 https://z11.cn
70L 游客  2024-07-12 23:29:00 回复
我就搞不明白了,看帖回帖能死人么,居然只有我这么认真的在回帖!http://yj9.830x.cn
71L 访客  2024-07-13 00:03:07 回复
安福相册精准查找 https://z11.cn
72L 游客  2024-07-13 00:30:58 回复
好东西,赞一个!http://5u7g.4gcdma.com
73L 游客  2024-07-13 01:17:54 回复
没人理我,好伤心啊!http://dy96.zhpxjy.com
74L 游客  2024-07-13 04:27:55 回复
坚持回帖!http://d31.soulmaker.cn
75L 游客  2024-07-13 07:28:54 回复
好东西,学习学习!http://zeecb.mandoron.com/q/4.html
76L 游客  2024-07-13 08:19:24 回复
楼主发几张靓照啊!http://3j02u.71okok.com/07/4.html
77L 游客  2024-07-13 09:28:20 回复
终于看完了,很不错!http://yevno.huichengyu.com
78L 游客  2024-07-13 10:07:56 回复
世界末日我都挺过去了,看到楼主我才知道为什么上帝留我到现在!http://aijiankang99.com/news/85a399554.html
79L 游客  2024-07-13 10:53:09 回复
楼主是一个典型的文艺青年啊!http://el3.06bmy.com
80L 游客  2024-07-13 11:52:53 回复
楼上的这是啥态度呢?http://4jt.sdjkcy1319.com
81L 游客  2024-07-13 12:14:16 回复
缺乏激情了!http://6fvll.tlwjjt.com/2024/4.html
82L 游客  2024-07-13 12:51:21 回复
这么经典的话只有楼主能想到!http://d7sax.jyv0rh.com/01/4.html
83L 安福相册  2024-07-13 13:54:35 回复
莆田鞋 https://www.anfu0594.com
84L 游客  2024-07-13 14:08:04 回复
关注一下!http://9si2.huichengyu.com
85L 游客  2024-07-13 14:12:28 回复
鸟大了,什么林子都敢进啊!http://7fang.cn/news/53c399586.html
86L 安福相册  2024-07-13 15:22:03 回复
安福相册精准查找 https://www.anfu0594.com
87L 游客  2024-07-13 15:54:11 回复
我只是来赚积分的!http://n7k.dt683.com
88L 游客  2024-07-13 16:31:43 回复
楼主是男的还是女的?http://mofeimo.com/?pic_12/234.html
89L 游客  2024-07-13 17:08:57 回复
楼主人气很旺!http://www.hntbhz.cn/category-8.html
90L 安福相册  2024-07-13 17:41:37 回复
安福相册 https://www.anfu0594.com
91L 游客  2024-07-13 18:26:46 回复
林子大了,什么鸟都有了啊!http://d4rnn.cocinadebarrio.com/2024/3.html
92L 游客  2024-07-13 20:29:55 回复
楼主很有激情啊!http://8lj.cryptoonair.com
93L 安福相册  2024-07-13 22:05:09 回复
莆田鞋 https://www.anfu0594.com
94L 游客  2024-07-13 23:14:09 回复
看在楼主的面子上,认真回帖!http://j1y.nonauxsubventions.com
95L 游客  2024-07-14 01:03:56 回复
收藏了,以后可能会用到!http://www.hntbhz.cn/post/115.html
96L 游客  2024-07-14 02:24:04 回复
这么经典的话只有楼主能想到!http://www.hntbhz.cn/post/197.html
97L 游客  2024-07-14 02:48:40 回复
支持一下!http://t5kvj.glitznhitz.com
98L 游客  2024-07-14 04:52:39 回复
这么好的帖子,应该加精华!http://vkkf.shoujusuoye.com
99L 游客  2024-07-14 09:06:14 回复
被楼主的逻辑打败了!http://exgwsmyo.com/news/76a099473.html
100L 游客  2024-07-14 09:23:34 回复
勤奋灌水,天天向上!http://y2s.hg30889.com

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。