防止将库捆绑在一起,而是从 CDN 上获取它们。

huangapple go评论78阅读模式
英文:

Prevent libraries from being bundled and pull them from cdn instead

问题

我有一个使用vite初始化的React应用程序。我想将该应用程序托管在Azure静态Web应用程序上,但显然我超过了大小限制,即524288000字节。

最大的贡献者似乎是duckdb-wasm和fluentui,所以我想不将它们捆绑在一起,而是让客户端从它们各自的CDN获取。

这可行吗?如果可以,我该如何做?

例如,在我的App.tsx文件中,我有以下代码:

import * as duckdb from '@duckdb/duckdb-wasm';
import duckdb_wasm_eh from '@duckdb/duckdb-wasm/dist/duckdb-eh.wasm?url';
import eh_worker from '@duckdb/duckdb-wasm/dist/duckdb-browser-eh.worker.js?url';
import { DefaultButton } from '@fluentui/react/lib/Button';
import { Stack, Text, initializeIcons, Pivot, PivotItem, Dropdown} from '@fluentui/react';

我该如何替换这些代码?另外,假设答案不同,我该如何告诉配置文件忽略它们?

编辑:这实际上是一个XY问题。我将包含我的详细信息,以防对其他人有所帮助

问题是在我的yml文件中,我需要将output_location指向./dist,因为这是vite默认放置所有内容的位置。如果没有这个设置,Azure或GitHub Action(我不确定是哪个)会保留所有的node_modules,当然太大了。设置了正确的output_location后,部署就顺利进行了。

英文:

I have a react app that I initiated with vite. I wanted to host the app on azure static web apps but apparently I'm exceeding the size limit which is 524288000 bytes.

The biggest contributors seem to be duckdb-wasm and fluentui so I'd like to not bundle those and just have the client get those from their respective CDNs.

Is that doable and if so, how would I do it?

For instance I have in my App.tsx

import * as duckdb from '@duckdb/duckdb-wasm';
import duckdb_wasm_eh from '@duckdb/duckdb-wasm/dist/duckdb-eh.wasm?url';
import eh_worker from '@duckdb/duckdb-wasm/dist/duckdb-browser-eh.worker.js?url';
import { DefaultButton } from '@fluentui/react/lib/Button';
import { Stack, Text, initializeIcons, Pivot, PivotItem, Dropdown} from '@fluentui/react';

How would I replace those? Further, assuming it isn't the same answer, how do I tell the configuration to ignore them?

Edit: this turned out to be a big XY problem. I'll include my details in case it helps anyone else

The issue was that in my yml file I needed to have output_location point to ./dist since that's where vite build puts everything by default. Without that the azure or github action (not sure which) kept all the node_modules which were, of course, too big. With the output_location properly set it deployed just fine.

答案1

得分: 0

加载CDN库而不是将它们捆绑在Vite项目中的方法:

  1. index.html中添加CDN链接:
<script src="CDN_LINK_FOR_DUCKDB"></script>
<script src="CDN_LINK_FOR_FLUENTUI"></script>
  1. 更新Vite的配置文件(vite.config.js):
export default {
  build: {
    rollupOptions: {
      external: ['@duckdb/duckdb-wasm', '@fluentui/react'],
    },
  },
  optimizeDeps: {
    exclude: ['@duckdb/duckdb-wasm', '@fluentui/react']
  }
}
  1. 在代码中引用全局变量:
const DefaultButton = GLOBAL_VAR_FOR_FLUENTUI.DefaultButton;

CDN_LINK_FOR_DUCKDBCDN_LINK_FOR_FLUENTUIGLOBAL_VAR_FOR_FLUENTUI替换为相应的值。

英文:

To load libraries from CDNs instead of bundling them in a Vite project:

  1. Add CDN Links in index.html:
&lt;script src=&quot;CDN_LINK_FOR_DUCKDB&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;CDN_LINK_FOR_FLUENTUI&quot;&gt;&lt;/script&gt;
  1. Update Vite's Configuration (vite.config.js):
export default {
  build: {
    rollupOptions: {
      external: [&#39;@duckdb/duckdb-wasm&#39;, &#39;@fluentui/react&#39;],
    },
  },
  optimizeDeps: {
    exclude: [&#39;@duckdb/duckdb-wasm&#39;, &#39;@fluentui/react&#39;]
  }
}
  1. Reference Global Variables in Your Code:
const DefaultButton = GLOBAL_VAR_FOR_FLUENTUI.DefaultButton;

Replace CDN_LINK_FOR_DUCKDB, CDN_LINK_FOR_FLUENTUI, and GLOBAL_VAR_FOR_FLUENTUI with the appropriate values.

huangapple
  • 本文由 发表于 2023年8月9日 02:02:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定