Vue / Vite v3.2.5 – 选项 “output.format” 的值 “umd” 无效

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

Vue / Vite v3.2.5 - Invalid value "umd" for option "output.format"

问题

Invalid value "umd" for option "output.format" - UMD and IIFE output formats are not supported for code-splitting builds. 无效值 "umd" 选项 "output.format" - 不支持UMD和IIFE输出格式用于代码拆分构建。

That's my vite.config.js 这是我的 vite.config.js

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue2';
const path = require('path')
import { resolve } from 'path'

export default defineConfig({
    plugins: [
        laravel({
            hotFile: 'public/widget.hot',
            input: [
                'resources/js/app.js',
                'resources/scss/app.scss',
                'resources/scss/index.scss'
            ],
            refresh: true,
        }),
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            },
        }),
    ],
    resolve: {
        alias: {
            vue: 'vue/dist/vue.esm.js',
        },
        dedupe: [
            'vue'
        ]
    },
    alias: {
        '~bootstrap': path.resolve(__dirname, 'node_modules/bootstrap'),
    },
    build: {
        cssCodeSplit: true,
        lib: {
            input: {
                app: "./resources/js/app.js"
            },
            entry: resolve(__dirname, 'resources/js/app.js'),
            output: {
                path: path.resolve(__dirname, 'dist'),
                filename: 'bundle.js',
            },
            name: 'bundle',
            fileName: 'app'
        },
        rollupOptions: {
            external: ['vue'],
            output: {
                globals: {
                    vue: 'Vue',
                },
                format: "esm",
                inlineDynamicImports: false,
            },
        },
    },
});

Does anyone know what's the problem here? My output.format value is "esm" and not "umd" ?! 有人知道问题在哪里吗?我的 output.format 值是 "esm" 而不是 "umd" ?! 谢谢帮助!

英文:

I use Vue2 with Vite v3.2.5 and when I run npm run build I get this error: Invalid value "umd" for option "output.format" - UMD and IIFE output formats are not supported for code-splitting builds.

That's my vite.config.js

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue2';
const path = require('path')
import { resolve } from 'path'
export default defineConfig({
plugins: [
laravel({
hotFile: 'public/widget.hot',
input: [
'resources/js/app.js',
'resources/scss/app.scss',
'resources/scss/index.scss'
],
refresh: true,
}),
vue({
template: {
transformAssetUrls: {
base: null,
includeAbsolute: false,
},
},
}),
],
resolve: {
alias: {
vue: 'vue/dist/vue.esm.js',
},
dedupe: [
'vue'
]
},
alias: {
'~bootstrap': path.resolve(__dirname, 'node_modules/bootstrap'),
},
build: {
cssCodeSplit: true,
lib: {
input: {
app: "./resources/js/app.js"
},
entry: resolve(__dirname, 'resources/js/app.js'),
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
name: 'bundle',
fileName: 'app'
},
rollupOptions: {
external: ['vue'],
output: {
globals: {
vue: 'Vue',
},
format: "esm",
inlineDynamicImports: false,
},
},
},
});

Does anyone know what's the problem here? My output.format value is "esm" and not "umd" ?!
Thanks for help!

答案1

得分: 3

当指定lib选项时,看起来 Vite 默认生成esumd捆绑包:

> 使用此配置运行 vite build 将使用面向库的 Rollup 预设,生成两种捆绑包格式:es 和 umd(可通过 build.lib 配置进行更改):

(来自https://vitejs.dev/guide/build.html#library-mode)

要更改这个,您需要在build.lib内部添加一个明确的formats选项:

        lib: {
input: {
app: "./resources/js/app.js"
},
formats: ['es'],
entry: resolve(__dirname, 'resources/js/app.js'),
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
name: 'bundle',
fileName: 'app'
},
英文:

When the lib option is specified, it looks like Vite defaults to generating es and umd bundles:

> Running vite build with this config uses a Rollup preset that is oriented towards shipping libraries and produces two bundle formats: es and umd (configurable via build.lib):

(from https://vitejs.dev/guide/build.html#library-mode)

To change this, you have to add an explicit formats option inside build.lib:

        lib: {
input: {
app: "./resources/js/app.js"
},
formats: ['es'],
entry: resolve(__dirname, 'resources/js/app.js'),
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
name: 'bundle',
fileName: 'app'
},

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

发表评论

匿名网友

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

确定