-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvite.js
More file actions
115 lines (96 loc) · 2.62 KB
/
vite.js
File metadata and controls
115 lines (96 loc) · 2.62 KB
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
const fs = require('node:fs')
const path = require('node:path')
const { defineConfig } = require('vite')
const POSTCSS_CONFIG_FILES = [
'postcss.config.js',
'postcss.config.cjs',
'postcss.config.mjs',
'postcss.config.ts',
]
const PROJECT_ROOT_MARKERS = [
'opencore.config.ts',
'opencore.config.js',
'opencore.config.mjs',
'opencore.config.cjs',
]
function findProjectRoot(startDir) {
let current = path.resolve(startDir)
while (true) {
for (const marker of PROJECT_ROOT_MARKERS) {
if (fs.existsSync(path.join(current, marker))) {
return current
}
}
const parent = path.dirname(current)
if (parent === current) {
return null
}
current = parent
}
}
function findPostcssConfig(projectRoot) {
if (!projectRoot) {
return null
}
for (const fileName of POSTCSS_CONFIG_FILES) {
const configPath = path.join(projectRoot, fileName)
if (fs.existsSync(configPath)) {
return configPath
}
}
return null
}
function resolveViewRoot(inputRoot) {
return path.resolve(inputRoot || process.env.OPENCORE_VIEW_ROOT || process.cwd())
}
function resolveProjectRoot(viewRoot, explicitProjectRoot) {
if (explicitProjectRoot) {
return path.resolve(explicitProjectRoot)
}
return findProjectRoot(viewRoot) || process.cwd()
}
function resolveOutDir(viewRoot, explicitOutDir, buildConfig) {
if (buildConfig && buildConfig.outDir) {
return buildConfig.outDir
}
const outDir = explicitOutDir || process.env.OPENCORE_VIEW_OUTDIR
if (outDir) {
return path.resolve(outDir)
}
return path.join(viewRoot, 'dist')
}
function resolveCssConfig(cssConfig, postcssConfigPath) {
if (!postcssConfigPath || cssConfig?.postcss !== undefined) {
return cssConfig
}
return {
...cssConfig,
postcss: postcssConfigPath,
}
}
function createOpenCoreViteConfig(config = {}) {
const { opencore, build, css, ...rest } = config
const viewRoot = resolveViewRoot(opencore?.root)
const projectRoot = resolveProjectRoot(viewRoot, opencore?.projectRoot)
const postcssConfigPath =
opencore?.postcss === false
? null
: typeof opencore?.postcss === 'string'
? path.resolve(projectRoot, opencore.postcss)
: findPostcssConfig(projectRoot)
return defineConfig({
...rest,
root: viewRoot,
base: rest.base ?? './',
css: resolveCssConfig(css, postcssConfigPath),
build: {
...build,
outDir: resolveOutDir(viewRoot, opencore?.outDir, build),
emptyOutDir: build?.emptyOutDir ?? true,
target: build?.target ?? opencore?.target ?? 'es2020',
},
})
}
module.exports = {
createOpenCoreViteConfig,
}