-
Notifications
You must be signed in to change notification settings - Fork 87
/
vite.config.mts
124 lines (108 loc) · 3.58 KB
/
vite.config.mts
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
119
120
121
122
123
124
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: CC0-1.0
*/
import type { Plugin } from 'vite'
import { createLibConfig } from '@nextcloud/vite-config'
import { globSync } from 'glob'
import { join, resolve } from 'node:path'
import { defineConfig } from 'vite'
import md5 from 'md5'
import * as url from 'url'
import l10nPlugin from './build/l10n-plugin.mts'
// `__dirname` not available on ES modules by default
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
const appVersion = JSON.stringify(process.env.npm_package_version || 'nextcloud-vue')
const versionHash = md5(appVersion).slice(0, 7) as string
const SCOPE_VERSION = JSON.stringify(versionHash)
// Entry points which we build using vite
const entryPoints = {
...globSync(['src/components/*/index.js', 'src/components/*/index.ts']).reduce((acc, item) => {
const name = item
.replace(/\/index\.[jt]s/, '')
.replace('src/components/', 'Components/')
acc[name] = join(__dirname, item)
return acc
}, {}),
...globSync(['src/directives/*/index.js', 'src/directives/*/index.ts']).reduce((acc, item) => {
const name = item
.replace(/\/index\.[jt]s/, '')
.replace('src/directives/', 'Directives/')
acc[name] = join(__dirname, item)
return acc
}, {}),
...globSync(['src/functions/*/index.js', 'src/functions/*/index.ts']).reduce((acc, item) => {
const name = item
.replace(/\/index\.[jt]s/, '')
.replace('src/functions/', 'Functions/')
acc[name] = join(__dirname, item)
return acc
}, {}),
...globSync(['src/mixins/*/index.js', 'src/mixins/*/index.ts']).reduce((acc, item) => {
const name = item
.replace(/\/index\.[jt]s/, '')
.replace('src/mixins/', 'Mixins/')
acc[name] = join(__dirname, item)
return acc
}, {}),
...globSync(['src/composables/*/index.js', 'src/composables/*/index.ts']).reduce((acc, item) => {
const name = item
.replace(/\/index\.[jt]s/, '')
.replace('src/composables/', 'Composables/')
acc[name] = join(__dirname, item)
return acc
}, {}),
index: resolve(__dirname, 'src/index.js'),
}
// Plugin for stripping out <docs> sections from vue files
const vueDocsPlugin: Plugin = {
name: 'vue-docs-plugin',
transform(code, id) {
if (!/vue&type=doc/.test(id)) {
return
}
return 'export default ""'
},
}
// Customizations for the vite config
const overrides = defineConfig({
plugins: [
vueDocsPlugin,
l10nPlugin(resolve(__dirname, 'l10n')),
],
css: {
devSourcemap: true,
preprocessorOptions: {
scss: {
additionalData: `@use 'sass:math'; $scope_version:${SCOPE_VERSION}; @import 'variables'; @import 'material-icons';`,
sourceMapContents: false,
includePaths: [
resolve(__dirname, 'src/assets'),
],
},
},
},
})
// We need a callback config so we can access the vite build mode
export default defineConfig((env) => {
const createConfig = createLibConfig(entryPoints, {
// Add our overrides to the config
config: overrides,
// By default all dependencies are external, but no path imports
nodeExternalsOptions: {
// Packages with paths imports should be added here to mark them as external as well
include: [/^@nextcloud\/.+\//, /^@mdi\/svg\//],
// Make sure to not provide uncompiled vue files as dependencies, this will break unit tests
exclude: [/\.vue(\?|$)/],
},
// For backwards compatibility we include the css within the js files
inlineCSS: true,
// Build CommonJS files for backwards compatibility
libraryFormats: ['es', 'cjs'],
replace: {
PRODUCTION: JSON.stringify(env.mode === 'production'),
SCOPE_VERSION,
},
})
return createConfig(env)
})