-
Notifications
You must be signed in to change notification settings - Fork 12
/
vite.config.ts
118 lines (110 loc) · 3.83 KB
/
vite.config.ts
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { defineConfig, type UserConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import copy from 'rollup-plugin-copy';
import legacy from '@vitejs/plugin-legacy';
import fs from 'node:fs';
import { resolve, dirname } from 'node:path';
import readline from 'node:readline';
import { fileURLToPath } from 'url';
import { globSync } from 'glob';
import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite';
/**
* Get theme folder name
*/
export function getTheme(): string {
let theme = "gouvfr";
const themeInfoFiles = globSync('udata_front/theme/*/info.json');
if(themeInfoFiles.length === 0) {
throw new Error("at least one udata_front theme must be installed.");
}
if(themeInfoFiles.length === 1) {
const file = themeInfoFiles[0];
const info: Record<string, string> = require(`./${file}`);
theme = info.identifier;
}
return theme;
}
export async function getVersion(version?: string): Promise<string | null> {
if(version) {
return version;
}
const fileName = globSync('*.egg-info/PKG-INFO');
if(fileName.length > 0) {
const fileStream = fs.createReadStream(fileName[0]);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
const pattern = 'Version: ';
for await (const line of rl) {
if(line.startsWith(pattern)) {
version = line.substring(pattern.length);
rl.close();
}
}
}
return version ?? null;
}
export async function getConfig(): Promise<UserConfig> {
const theme = getTheme();
let version = await getVersion(process.env.buildno);
return {
base: `/_themes/${theme}/`,
plugins: [
vue(),
VueI18nPlugin({
compositionOnly: false,
/* options */
include: [
resolve(dirname(fileURLToPath(import.meta.url)), `udata_front/theme/${theme}/assets/js/locales/**`),
resolve(dirname(fileURLToPath(import.meta.url)), `udata_front/theme/gouvfr/datagouv-components/src/locales/**`)
],
}),
legacy({
targets: "> 0.1%, last 15 versions, Firefox ESR, not dead",
externalSystemJS: true,
}),
copy({
targets: [
{ src: `udata_front/theme/${theme}/assets/img`, dest: `udata_front/theme/${theme}/static/` },
{ src: "node_modules/systemjs/dist/s.min.js", dest: `udata_front/theme/${theme}/static/js/` },
{ src: "node_modules/leaflet/dist/leaflet.js", dest: `udata_front/theme/${theme}/static/js/` },
{ src: "node_modules/leaflet/dist/leaflet.css", dest: `udata_front/theme/${theme}/static/js/` },
],
hook: 'writeBundle'
}),
],
envDir: "udata_front/theme/gouvfr/datagouv-components",
build: {
cssCodeSplit: false,
rollupOptions: {
input: [
`udata_front/theme/${theme}/assets/js/index.ts`,
`udata_front/theme/${theme}/assets/js/admin.ts`,
`udata_front/theme/${theme}/assets/less/style.less`,
"node_modules/es-module-shims/dist/es-module-shims.js",
"node_modules/vue/dist/vue.esm-browser.prod.js",
"node_modules/vue-content-loader/dist/vue-content-loader.es.js",
],
preserveEntrySignatures: 'exports-only',
// make sure to externalize deps that shouldn't be bundled
// into your library
external: ['vue', 'vue-content-loader'],
output: {
dir: `./udata_front/theme/${theme}/static/`,
entryFileNames: `js/[name].${version}.js`,
chunkFileNames: `js/[name].${version}.js`,
assetFileNames: `assets/[name].[ext]`,
// Provide global variables to use in the UMD build
// for externalized deps
globals: {
vue: 'Vue',
"vue-content-loader": "ContentLoader"
}
}
}
}
};
}
// https://vitejs.dev/config/
export default defineConfig(getConfig());