-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
80 lines (73 loc) · 2.01 KB
/
vite.config.js
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
import { defineConfig } from 'vite'
import { globSync } from 'glob'
import jsYaml from 'js-yaml'
import { readFileSync } from 'fs'
export default defineConfig(({ mode }) => {
const env = mode === 'production' ? '"production"' : '"development"'
return {
build: {
cssCodeSplit: true,
manifest: true,
rollupOptions: {
external: '[Drupal, once]',
input: getInputs(),
output: {
dir: 'web/libraries/compiled',
entryFileNames: (assetInfo) => {
return assetInfo.name + '-[hash].js'
},
},
},
},
css: { devSourcemap: true },
define: { 'process.env.NODE_ENV': env },
}
})
/**
* Gets an array of input file paths.
*/
function getInputs() {
// Use glob to find all relevant YAML files
const files = globSync('./web/modules/**/*.foxy.yml')
// Use reduce to process each file and collect all JS files
return files.reduce((acc, libraryFilePath) => {
try {
const moduleDirectoryPath = libraryFilePath.substring(
0,
libraryFilePath.lastIndexOf('/'),
)
const moduleName = moduleDirectoryPath.substring(
moduleDirectoryPath.lastIndexOf('/') + 1,
)
const fileContents = readFileSync(libraryFilePath, 'utf8')
const data = jsYaml.load(fileContents)
const jsFiles = extractJsFiles(data)
jsFiles.forEach((filePath) => {
acc[moduleName] = moduleDirectoryPath + '/' + filePath
})
return acc
} catch (err) {
console.error(`Error processing file ${libraryFilePath}:`, err)
return acc
}
}, {})
}
/**
* Extracts JS files from a given YAML object.
*/
function extractJsFiles(data) {
let jsFiles = []
function traverse(obj) {
if (obj && typeof obj === 'object') {
Object.keys(obj).forEach((key) => {
if (key === 'js' && typeof obj[key] === 'object') {
jsFiles.push(...Object.keys(obj[key]))
} else {
traverse(obj[key])
}
})
}
}
traverse(data)
return jsFiles
}