import type { Plugin } from 'vite';
|
|
import { Buffer } from 'node:buffer';
|
import { readFileSync } from 'node:fs';
|
import path from 'node:path';
|
import process from 'node:process';
|
import { fileURLToPath } from 'node:url';
|
|
import vue from '@vitejs/plugin-vue';
|
import { defineConfig, loadEnv } from 'vite';
|
|
import { onlyOfficeDevelopmentPlugin, resolveOnlyOfficeDevOptions } from './vite/onlyoffice-plugin';
|
|
const PLUGIN_GUID = 'asc.{2D6D6CB6-F6FC-45B7-8EC7-0CC1940F1A6F}';
|
const PLUGIN_VERSION = '0.1.0';
|
const pluginRoot = fileURLToPath(new URL('.', import.meta.url));
|
|
function onlyOfficeEnvironment(environment: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
|
return Object.fromEntries(Object.entries(environment).filter(([name]) => name.startsWith('ONLYOFFICE_')));
|
}
|
|
function productionAssets(): Plugin {
|
let manifest: Record<string, unknown> | undefined;
|
let icon: Buffer | undefined;
|
let retinaIcon: Buffer | undefined;
|
|
return {
|
name: 'eln-production-assets',
|
apply: 'build',
|
buildStart() {
|
const parsed = JSON.parse(readFileSync(path.resolve(pluginRoot, 'config.json'), 'utf8')) as Record<string, unknown>;
|
const variations = parsed.variations;
|
|
if (parsed.version !== PLUGIN_VERSION || parsed.guid !== PLUGIN_GUID) {
|
throw new Error('ONLYOFFICE plugin version or GUID changed during the production build');
|
}
|
if (!Array.isArray(variations) || typeof variations[0] !== 'object' || variations[0] === null) {
|
throw new Error('ONLYOFFICE plugin manifest has no production variation');
|
}
|
|
manifest = parsed;
|
icon = readFileSync(path.resolve(pluginRoot, 'resources/icon.png'));
|
retinaIcon = readFileSync(path.resolve(pluginRoot, 'resources/icon@2x.png'));
|
},
|
generateBundle() {
|
if (!manifest || !icon || !retinaIcon) {
|
throw new Error('Production assets were not initialized');
|
}
|
|
this.emitFile({ type: 'asset', fileName: 'config.json', source: `${JSON.stringify(manifest, null, 2)}\n` });
|
this.emitFile({ type: 'asset', fileName: 'resources/icon.png', source: icon });
|
this.emitFile({ type: 'asset', fileName: 'resources/icon@2x.png', source: retinaIcon });
|
this.emitFile({ type: 'asset', fileName: 'translations/langs.json', source: '[]\n' });
|
},
|
};
|
}
|
|
export default defineConfig(({ command, mode }) => {
|
const developmentEnvironment = {
|
...onlyOfficeEnvironment(loadEnv(mode, pluginRoot, '')),
|
...onlyOfficeEnvironment(process.env),
|
};
|
const development = command === 'serve' ? resolveOnlyOfficeDevOptions(developmentEnvironment) : undefined;
|
|
return {
|
base: development ? `${development.basePath}/` : './',
|
plugins: [vue(), ...(development ? [onlyOfficeDevelopmentPlugin(development)] : [productionAssets()])],
|
server: development
|
? {
|
hmr: {
|
clientPort: development.hmrClientPort,
|
host: development.hmrHost,
|
...(development.hmrPath ? { path: development.hmrPath } : {}),
|
protocol: development.hmrProtocol,
|
},
|
host: development.host,
|
port: development.port,
|
strictPort: true,
|
}
|
: undefined,
|
root: pluginRoot,
|
build: {
|
sourcemap: false,
|
rollupOptions: {
|
input: path.resolve(pluginRoot, 'index.html'),
|
},
|
},
|
};
|
});
|