-
Notifications
You must be signed in to change notification settings - Fork 30
/
vite.config.js
95 lines (91 loc) · 2.78 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import path from 'path'
import pkg from './package.json'
import { defineConfig, loadEnv } from 'vite'
import legacy from '@vitejs/plugin-legacy'
const CONSOLE_CODE = `console.log(\`%c🍊%c Shikwasa Podcast Player v${pkg.version} %c https://shikwasa.js.org\`,'background-color:#00869B40;padding:4px;','background:#00869B80;color:#fff;padding:4px 0','padding: 2px 0;')`
function replaceHTMLPlugin(replaceStrings) {
return {
name: 'inject-html',
transformIndexHtml: {
enforce: 'pre',
transform: (html) => {
for(const [key, value] of Object.entries(replaceStrings)) {
const regex = new RegExp(`<%- ${key} %>`, 'gm')
html = html.replace(regex,value)
}
return html
},
},
}
}
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), 'APP')
let config = {
define: { 'CONSOLE_MSG': CONSOLE_CODE },
plugins: [{
...legacy({ targets: ['>0.2%', 'not ie <= 8'] }),
apply: 'build',
}],
}
if (process.env.TARGET === 'lib') {
config.build = {
lib: {
entry: path.resolve(__dirname, 'src/main.js'),
name: 'Shikwasa',
formats: ['es', 'cjs', 'umd', 'iife'],
fileName: (format) => {
const infix = format === 'umd' ? 'min' : format
return `shikwasa.${infix}.js`
},
},
}
// prevent accidentally copying demo build from public to dist
config.publicDir = false
} else {
config.build = { outDir: 'public' }
if (process.env.TARGET === 'ci') {
const name = 'cypress'
config.pages = {
[name]: {
fileName: `pages/${name}.html`,
template: `pages/${name}.html`,
chunks: name,
entry: `pages/${name}.js`,
}
}
}
if (process.env.TARGET === 'demo') {
const REPLACED_STRINGS = {
APP_GA: env.APP_GA,
prependScript: mode === 'production' ? env.APP_PROD_HEAD : env.APP_DEV_HEAD,
appendScript: mode === 'production' ? env.APP_PROD_SCRIPT : env.APP_DEV_SCRIPT,
}
const name = 'index'
config = {
...config,
root: mode === 'production' ? 'pages' : process.cwd(),
pages: {
[name]: {
fileName: `${name}.html`,
template: `${name}.html`,
chunks: name,
entry: `${name}.js`,
},
},
server: {
open: mode === 'production' ? true : '/pages/index.html',
},
publicDir: path.resolve(__dirname, 'pages/public'),
build: {
outDir: '../public',
emptyOutDir: true,
rollupOptions: {
input: path.resolve(__dirname, 'pages/index.html'),
},
},
}
config.plugins.push(replaceHTMLPlugin(REPLACED_STRINGS))
}
}
return config
})