forked from shiyiya/oplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
154 lines (140 loc) · 4.86 KB
/
vite.config.ts
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import fs from 'fs'
import path from 'path'
import type { BuildOptions, UserConfig as ViteUserConfig, UserConfigExport } from 'vite'
import { defineConfig } from 'vite'
import banner from 'vite-plugin-banner'
import { getBabelOutputPlugin } from '@rollup/plugin-babel'
export const globals = {
'@oplayer/core': 'OPlayer',
'@oplayer/ui': 'OUI',
'@oplayer/hls': 'OHls',
'@oplayer/mpegts': 'OMpegts',
'@oplayer/torrent': 'OTorrent',
'@oplayer/danmaku': 'ODanmaku',
react: 'React',
// 'react/jsx-runtime':'',
dashjs: 'dashjs',
'hls.js': 'Hls',
'hls.js/dist/hls.min.js': 'Hls',
'mpegts.js/dist/mpegts.js': 'mpegts',
'webtorrent/dist/webtorrent.min.js': 'WebTorrent',
'webtorrent/webtorrent.min.js': 'WebTorrent'
}
const babelPlugins = [
'syntax-trailing-function-commas',
// x ??= 1
// '@babel/plugin-proposal-logical-assignment-operators',
'@babel/plugin-transform-logical-assignment-operators',
// x ?? 1
// '@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-transform-nullish-coalescing-operator',
// These use loose mode which avoids embedding a runtime.
// TODO: Remove object spread from the source. Prefer Object.assign instead.
// ['@babel/plugin-proposal-object-rest-spread', { loose: true, useBuiltIns: true }],
['@babel/plugin-transform-object-rest-spread', { loose: true, useBuiltIns: true }],
['@babel/plugin-transform-template-literals', { loose: true }],
// TODO: Remove array spread from the source. Prefer .apply instead.
['@babel/plugin-transform-spread', { loose: true, useBuiltIns: true }],
'@babel/plugin-transform-parameters',
// TODO: Remove array destructuring from the source. Requires runtime.
['@babel/plugin-transform-destructuring', { loose: true, useBuiltIns: true }]
]
const makeExternalPredicate = (externalArr: string[]) => {
if (externalArr.length === 0) return () => false
return (id: string) => new RegExp(`^(${externalArr.join('|')})($|/)`).test(id)
}
export const libFileName = (format: string) => `index.${{ es: 'es', umd: 'min' }[format]}.js`
function getBabelConfig() {
return {
allowAllFormats: true,
babelrc: false,
configFile: false,
presets: [],
plugins: [...babelPlugins]
}
}
export const rollupPlugins: any[] = [getBabelOutputPlugin(getBabelConfig())]
export const viteBuild = (packageDirName: string, options: BuildOptions = {}): BuildOptions => {
const pkg = JSON.parse(
fs.readFileSync(path.resolve(__dirname, `packages/${packageDirName}/package.json`), {
encoding: 'utf-8'
})
)
return mergeDeep<BuildOptions>(
{
minify: 'terser',
sourcemap: false,
commonjsOptions: {
sourceMap: false
},
lib: {
entry: resolvePath(`packages/${packageDirName}/src/index.ts`),
name: `oplayer_${packageDirName}`,
fileName: libFileName,
formats: ['es', 'umd']
},
rollupOptions: {
external: makeExternalPredicate([
// ...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {})
]),
output: {
dir: resolvePath(`packages/${packageDirName}/dist`),
globals
},
plugins: rollupPlugins as any
}
},
options
)
}
export const viteConfig = (
packageDirName: string,
options: ViteUserConfig = {},
withBanner: boolean = true
) => {
const vitePlugins = options.plugins ?? []
const { name, version, description, author, homepage } = JSON.parse(
fs.readFileSync(resolvePath(`packages/${packageDirName}/package.json`), { encoding: 'utf-8' })
)
const defaultPlugins = withBanner
? [
banner(
`/**\n * name: ${name}\n * version: v${version}\n * description: ${description}\n * author: ${author}\n * homepage: ${homepage}\n */`
)
]
: []
return defineConfig({
...options,
build: viteBuild(packageDirName, options.build),
plugins: [...vitePlugins, ...rollupPlugins, ...defaultPlugins] as any,
define: { __VERSION__: `'${version}'` }
})
}
export default defineConfig({
test: {
include: ['test/*', 'packages/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
environment: 'jsdom',
minThreads: 0,
maxThreads: 1
}
} as UserConfigExport)
const resolvePath = (str: string) => path.resolve(__dirname, str)
function isObject(item: unknown): item is Record<string, unknown> {
return Boolean(item && typeof item === 'object' && !Array.isArray(item))
}
function mergeDeep<T>(target: T, ...sources: T[]): T {
if (!sources.length) return target
const source = sources.shift()
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} })
mergeDeep(target[key] as T, source[key] as T)
} else {
Object.assign(target, { [key]: source[key] })
}
}
}
return mergeDeep(target, ...sources)
}