-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
101 lines (94 loc) · 2.72 KB
/
Copy pathvite.config.js
File metadata and controls
101 lines (94 loc) · 2.72 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
import { defineConfig } from 'vite'
import fs from 'fs'
// import { parse } from "yaml";
import { resolve, basename, extname } from 'path'
import { globSync } from 'tinyglobby'
import VitePluginBrowserSync from 'vite-plugin-browser-sync'
import browserslist from 'browserslist-to-esbuild'
// const lando = parse(fs.readFileSync('../../../../.lando.yml', 'utf8'));
const lando = { name: 'TEST' }
function getEntries() {
const files = globSync(['components/**/*.library.js'], {})
return files.reduce((entries, file) => {
const name = basename(file, '.library.js')
entries[name] = file
return entries
}, {})
}
function removeEmptyJsFiles() {
return {
name: 'remove-empty-js-files',
closeBundle() {
const files = globSync(['dist/**/*.js'], {})
files.forEach(file => {
const content = fs.readFileSync(file, 'utf-8')
const stripped = content
.replace(/\/\*[\s\S]*?\*\//g, '')
.replace(/\/\/.*/g, '')
.trim()
if (stripped === '') {
fs.unlinkSync(file)
}
})
}
}
}
export default defineConfig(({ command }) => {
const input = getEntries()
// A direct `vite build` with no components has nothing to bundle, so exit
// cleanly instead of failing on an empty rollup input. Storybook loads this
// same config and supplies its own entries, so leave it alone.
if (
command === 'build' &&
!process.env.STORYBOOK &&
Object.keys(input).length === 0
) {
console.log(
'No build entries found (components/**/*.library.js); nothing to build.'
)
process.exit(0)
}
return {
base: './',
build: {
target: browserslist(),
outDir: resolve(import.meta.dirname, './dist'),
reportCompressedSize: false,
rollupOptions: {
input,
output: {
entryFileNames: '[name].js',
chunkFileNames: '[name]-[hash].js',
assetFileNames: ({ name }) => {
const ext = extname(name)
switch (ext) {
case '.css':
return '[name].css'
default:
return '[name]-[hash][extname]'
}
}
}
}
},
plugins: [
VitePluginBrowserSync({
dev: { enable: false },
preview: { enable: false },
buildWatch: {
enable: process.env.BROWSERSYNC !== 'false',
bs: {
host: 'localhost',
proxy: `https://${lando.name}.lndo.site`,
files: ['./dist', './templates', './components'],
watchEvents: ['add', 'change', 'unlink', 'addDir', 'unlinkDir'],
ghostMode: false,
ui: false,
open: false
}
}
}),
removeEmptyJsFiles()
]
}
})