英文:
Vite library mode code splitting with UMD like in webpack
问题
在Vite中是否有一种启用类似于webpack的代码拆分的方法?
这是我的当前设置:
import {fileURLToPath, URL} from 'node:url';
import {resolve} from 'path';
import {defineConfig} from 'vite';
export default defineConfig({
optimizeDeps: {
force: true
},
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
},
build: {
outDir: resolve(__dirname, 'some/path'),
emptyOutDir: true,
lib: {
entry: resolve(__dirname, 'src/main.ts'),
name: 'Site',
formats: ['umd'],
fileName: 'site',
},
},
});
并且它不会自动对我的文件进行代码拆分,而是只创建一个单一的文件。
只有当我将 formats: ['umd'],
更改为 formats: ['cjs'],
时它才会起作用。但是我想使用 umd
模式。
我创建了一个最小的生产存储库:https://github.com/Anubarak/vite-code-splitting。
如果有人能够生成多个输出的JS文件,而不仅仅是一个umd
,请告诉我如何操作。
英文:
Is there a way to enable code-splitting in Vite for libraries like webpack does?
This is my current setup
import {fileURLToPath, URL} from 'node:url';
import {resolve} from 'path';
import {defineConfig} from 'vite';
export default defineConfig({
optimizeDeps: {
force: true
},
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
},
build: {
outDir: resolve(__dirname, 'some/path'),
emptyOutDir: true,
lib: {
entry: resolve(__dirname, 'src/main.ts'),
name: 'Site',
formats: ['umd'],
fileName: 'site',
},
},
});
and it won't automatically code split my files - instead it only creates one single file.
Only if I change formats: ['umd'],
to formats: ['cjs'],
it will work. However I would like to use umd
mode
I made a minimal production repo: https://github.com/Anubarak/vite-code-splitting.
If someone is able to produce multiple output JS files instead of just one with umd
please let me know how.
答案1
得分: 2
看起来那是不可能的,当我强制使用 dynamicInlineImports: false
时,我得到了一个错误:
Invalid value "umd" for option "output.format" - UMD and IIFE output formats are not supported for code-splitting builds
英文:
Seems that's impossible, when I forced dynamicInlineImports: false
I'd got an error:
Invalid value "umd" for option "output.format" - UMD and IIFE output formats are not supported for code-splitting builds
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论