Vite支持UMD方式的代码拆分,类似于webpack。

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

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

huangapple
  • 本文由 发表于 2023年5月22日 14:50:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303631.html
匿名

发表评论

匿名网友

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

确定