-
Notifications
You must be signed in to change notification settings - Fork 16
/
vite.config.js
130 lines (124 loc) · 3.39 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
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
/* eslint-disable */
import react from '@vitejs/plugin-react';
import crypto from 'crypto';
import path from 'path';
import { defineConfig, splitVendorChunkPlugin } from 'vite';
import { viteExternalsPlugin } from 'vite-plugin-externals';
import { createHtmlPlugin } from 'vite-plugin-html';
const buildHash =
/* Netlify */ process.env.COMMIT_REF ||
/* CI */ process.env.BUILD_SHA ||
/* GitHub Actions */ process.env.GITHUB_SHA ||
/* Local */ crypto
.createHash('sha256')
.update(new Date().toISOString())
.digest('hex');
const data = {
appVersion: buildHash,
};
const cdnjsBaseUrl = process.env.VITE_BAOSHUO_CDNJS
? '//cdnjs.baoshuo.ren/ajax/libs'
: '//cdnjs.cloudflare.com/ajax/libs';
const externalPackageList = {
react: {
globalVariableName: 'React',
devScript: 'umd/react.development.js',
prodScript: 'umd/react.production.min.js',
},
'react-dom': {
globalVariableName: 'ReactDOM',
devScript: 'umd/react-dom.development.js',
prodScript: 'umd/react-dom.production.min.js',
},
history: {
globalVariableName: 'HistoryLibrary',
devScript: 'history.development.js',
prodScript: 'history.production.min.js',
},
'react-router': {
globalVariableName: 'ReactRouter',
devScript: 'react-router.development.js',
prodScript: 'react-router.production.min.js',
},
'react-router-dom': {
globalVariableName: 'ReactRouterDOM',
devScript: 'react-router-dom.development.js',
prodScript: 'react-router-dom.production.min.js',
},
'react-is': {
globalVariableName: 'ReactIs',
devScript: 'umd/react-is.development.js',
prodScript: 'umd/react-is.production.min.js',
},
};
const externalStylesheetList = {
'semantic-ui': {
file: 'semantic.min.css',
version: '2.4.1',
},
};
// https://vitejs.dev/config/
export default defineConfig(({ command }) => ({
plugins: [
splitVendorChunkPlugin(),
react(),
createHtmlPlugin({
minify: true,
entry: '/src/main.tsx',
inject: {
data,
tags: [
...Object.entries(externalStylesheetList).map(
([name, { file, version }]) => ({
injectTo: 'head',
tag: 'link',
attrs: {
rel: 'stylesheet',
href:
cdnjsBaseUrl +
'/' +
name +
'/' +
(version ?? require(`${name}/package.json`).version) +
'/' +
file,
},
})
),
...Object.entries(externalPackageList).map(
([name, { devScript, prodScript }]) => ({
injectTo: 'head',
tag: 'script',
attrs: {
defer: true,
src:
cdnjsBaseUrl +
'/' +
name +
'/' +
require(`${name}/package.json`).version +
'/' +
(command === 'build' ? prodScript ?? devScript : devScript),
},
})
),
],
},
}),
viteExternalsPlugin({
...Object.fromEntries(
Object.entries(externalPackageList).map(
([name, { globalVariableName }]) => [name, globalVariableName]
)
),
}),
],
build: {
sourcemap: true,
},
resolve: {
alias: {
'@': path.join(__dirname, './src'),
},
},
}));