英文:
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项目中的方法:
- 在
index.html
中添加CDN链接:
<script src="CDN_LINK_FOR_DUCKDB"></script>
<script src="CDN_LINK_FOR_FLUENTUI"></script>
- 更新Vite的配置文件(
vite.config.js
):
export default {
build: {
rollupOptions: {
external: ['@duckdb/duckdb-wasm', '@fluentui/react'],
},
},
optimizeDeps: {
exclude: ['@duckdb/duckdb-wasm', '@fluentui/react']
}
}
- 在代码中引用全局变量:
const DefaultButton = GLOBAL_VAR_FOR_FLUENTUI.DefaultButton;
将CDN_LINK_FOR_DUCKDB
、CDN_LINK_FOR_FLUENTUI
和GLOBAL_VAR_FOR_FLUENTUI
替换为相应的值。
英文:
To load libraries from CDNs instead of bundling them in a Vite project:
- Add CDN Links in
index.html
:
<script src="CDN_LINK_FOR_DUCKDB"></script>
<script src="CDN_LINK_FOR_FLUENTUI"></script>
- Update Vite's Configuration (
vite.config.js
):
export default {
build: {
rollupOptions: {
external: ['@duckdb/duckdb-wasm', '@fluentui/react'],
},
},
optimizeDeps: {
exclude: ['@duckdb/duckdb-wasm', '@fluentui/react']
}
}
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论