我在uni-app项目中打包APP时控制台提示报错Invalid value "iife" for option "output.format" - UMD and IIFE output formats are not supported for,查阅资料后发现是因为我在vite-config.ts文件中使用了import uni from '@dcloudio/vite-plugin-uni';我就把import uni from '@dcloudio/vite-plugin-uni'里面的值打印出来看这个插件里面的配置会把打包模块的format值修改为iife,导致打包报错,

下面是打印出来的值

 Resolved Rollup Options: {
15:29:54.398   sourcemapExcludeSources: false,
15:29:54.408   sourcemapPathTransform: [Function: sourcemapPathTransform],
15:29:54.415   manualChunks: {},
15:29:54.426   inlineDynamicImports: false,
15:29:54.433   chunkFileNames: [Function: chunkFileNames],
15:29:54.440   name: 'AppService',
15:29:54.446   banner: '\n' +
15:29:54.453     "if (typeof Promise !== 'undefined' && !Promise.prototype.finally) {\n" +
15:29:54.460     '  Promise.prototype.finally = function(callback) {\n' +
15:29:54.467     '    const promise = this.constructor\n' +
15:29:54.473     '    return this.then(\n' +
15:29:54.478     '      value => promise.resolve(callback()).then(() => value),\n' +
15:29:54.485     '      reason => promise.resolve(callback()).then(() => {\n' +
15:29:54.491     '        throw reason\n' +
15:29:54.498     '      })\n' +
15:29:54.504     '    )\n' +
15:29:54.510     '  }\n' +
15:29:54.517     '};\n' +
15:29:54.526     '\n' +
15:29:54.531     "if (typeof uni !== 'undefined' && uni && uni.requireGlobal) {\n" +
15:29:54.538     '  const global = uni.requireGlobal()\n' +
15:29:54.543     '  ArrayBuffer = global.ArrayBuffer\n' +
15:29:54.549     '  Int8Array = global.Int8Array\n' +
15:29:54.554     '  Uint8Array = global.Uint8Array\n' +
15:29:54.561     '  Uint8ClampedArray = global.Uint8ClampedArray\n' +
15:29:54.569     '  Int16Array = global.Int16Array\n' +
15:29:54.575     '  Uint16Array = global.Uint16Array\n' +
15:29:54.582     '  Int32Array = global.Int32Array\n' +
15:29:54.587     '  Uint32Array = global.Uint32Array\n' +
15:29:54.595     '  Float32Array = global.Float32Array\n' +
15:29:54.603     '  Float64Array = global.Float64Array\n' +
15:29:54.609     '  BigInt64Array = global.BigInt64Array\n' +
15:29:54.619     '  BigUint64Array = global.BigUint64Array\n' +
15:29:54.647     '};\n' +
15:29:54.657     '\n' +
15:29:54.662     '\n' +
15:29:54.670     'if(uni.restoreGlobal){\n' +
15:29:54.677     '  uni.restoreGlobal(Vue,weex,plus,setTimeout,clearTimeout,setInterval,clearInterval)\n' +
15:29:54.686     '}\n',
15:29:54.694   format: 'iife',
15:29:54.705   amd: { autoId: true },
15:29:54.712   entryFileNames: 'app-service.js',
15:29:54.721   globals: { vue: 'Vue', '@vue/shared': 'uni.VueShared' }
15:29:54.729 }
15:29:54.887 ​Browserslist: caniuse-lite is outdated. Please run:
15:29:54.895   npx update-browserslist-db@latest
15:29:54.906   Why you should do it regularly: https://github.com/browserslist/update-db#readme​
15:29:55.227 Resolved Rollup Options: {
15:29:55.234   sourcemapExcludeSources: false,
15:29:55.241   sourcemapPathTransform: [Function: sourcemapPathTransform],
15:29:55.249   manualChunks: {},
15:29:55.260   inlineDynamicImports: false,
15:29:55.266   chunkFileNames: [Function: chunkFileNames],
15:29:55.274   entryFileNames: [Function: entryFileNames],
15:29:55.281   plugins: [
15:29:55.287     {
15:29:55.294       name: 'dynamic-import-polyfill',
15:29:55.301       renderDynamicImport: [Function: renderDynamicImport]
15:29:55.306     }
15:29:55.313   ],
15:29:55.319   globals: { vue: 'Vue', vuex: 'Vuex', pinia: 'Pinia' }
15:29:55.324 }

然后我就在 把format的值替换为es后就可以正常打包了,不知道为什么会有这种情况

下面是我的vite.config.ts

import { defineConfig } from 'vite';
import uni from '@dcloudio/vite-plugin-uni';
import WindiCSS from 'vite-plugin-windicss';
import MiniProgramTailwind from '@dcasia/mini-program-tailwind-webpack-plugin/rollup';

const plugins = [
    uni(),
    WindiCSS({
        scan: {
            dirs: ['.'], // 当前目录下所有文件
            fileExtensions: ['vue', 'js', 'ts'], // 同时启用扫描 vue/js/ts
        },
    }),
    {
        name: 'print-rollup-options', // 自定义插件名称
        configResolved(config) {
            config.build.rollupOptions.output.format='es' // 替换format
            console.log('Resolved Rollup Options:', config.build.rollupOptions.output);
        },
    },
];
// console.log("uni",plugins)
process.env.UNI_PLATFORM === 'mp-weixin' && plugins.push(MiniProgramTailwind());

export default defineConfig({
    server: {
        host: '0.0.0.0',
    },
    plugins,
    optimizeDeps: {
        include: ['dayjs'],
    },
});

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐