import fs from 'fs' import path from 'path' import type { Plugin } from 'vite' import type { RollupOptions } from 'rollup' import { MP_PLUGIN_JSON_NAME, parseJson, removeExt, resolveMainPathOnce, } from '@dcloudio/uni-cli-shared' import { UniMiniProgramPluginOptions } from '../plugin' import { createNonAppGenerateBundle } from './subpackage' import { extend } from '@vue/shared' import { notFound } from '../plugin/build' export function uniMiniProgramPluginPlugin({ style: { extname }, }: UniMiniProgramPluginOptions): Plugin { const entry = initPluginEntry() const rollupOptions: RollupOptions = {} if (entry) { rollupOptions.input = extend( { app: resolveMainPathOnce(process.env.UNI_INPUT_DIR), }, entry ) } return { name: 'vite:uni-mp-plugin', enforce: 'post', config() { return { build: { rollupOptions, }, } }, generateBundle: createNonAppGenerateBundle(extname), } } function initPluginEntry(): Record | void { const pluginJsonFilename = path.resolve( process.env.UNI_INPUT_DIR, MP_PLUGIN_JSON_NAME ) if (!fs.existsSync(pluginJsonFilename)) { notFound(pluginJsonFilename) } const pluginJson = parseJson( fs.readFileSync(pluginJsonFilename, 'utf8'), true ) if (!pluginJson.main) { return } const mainFilename = path.resolve(process.env.UNI_INPUT_DIR, pluginJson.main) if (!fs.existsSync(mainFilename)) { notFound(mainFilename) } return { [removeExt(pluginJson.main)]: mainFilename, } }