aboutsummaryrefslogtreecommitdiffstats
path: root/config/vite-common.js
blob: 6b1d7c79bf48b8ee7b1dece035d9a4b1e73b06d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// @ts-check

// `resolveJsonModule` is disabled for this package, but vite will load the json nevertheless.
// @ts-expect-error
import { chrome as chromeVersion, node as nodeVersion } from '../.electron-vendors.cache.json';

/** @type {string} */
const banner = `/*!
 * Copyright (C)  2021-2022 Sophie contributors
 *
 * This file is part of Sophie.
 *
 * Sophie is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: AGPL-3.0-only
 */
`;

/** @type {string} */
export const chrome = `chrome${chromeVersion}`;

/** @type {string} */
export const node = `node${nodeVersion}`;

/**
 * Extends a vite configuration with common options.
 *
 * @param {import('vite').UserConfig} config The configuration to extend.
 * @return {import('vite').UserConfig} The configuration extended with common options.
 */
export function makeConfig(config) {
  return {
    mode: process.env.NODE,
    envDir: process.cwd(),
    ...config,
    build: {
      assetsDir: '.',
      outDir: 'dist',
      emptyOutDir: true,
      sourcemap: true,
      minify: process.env.MODE !== 'development',
      brotliSize: false,
      ...config.build,
      lib: (typeof config.build.lib === 'object') ? {
        fileName: (format) => format === 'cjs' ? 'index.cjs' : `index.${format}.js`,
        ...config.build.lib,
      } : undefined,
      rollupOptions: {
        ...config.build?.rollupOptions,
        output: {
          banner,
          ...config.build?.rollupOptions?.output,
        },
      },
    },
  };
}