forked from teia-community/teia-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
191 lines (181 loc) · 5.37 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import viteTsconfigPaths from 'vite-tsconfig-paths'
import svgrPlugin from 'vite-plugin-svgr'
import path from 'path'
import eslintPlugin from 'vite-plugin-eslint'
import { splitVendorChunkPlugin } from 'vite'
import { copySync } from 'fs-extra'
import rollupNodePolyFill from 'rollup-plugin-polyfill-node'
import mdPlugin from 'vite-plugin-markdown'
import child_process from 'child_process'
import { ViteEjsPlugin } from 'vite-plugin-ejs'
import filterReplace from 'vite-plugin-filter-replace'
// Gets the current git commit (used in <head>)
const commitHash = child_process
.execSync('git rev-parse HEAD')
.toString()
.trim()
/**
* React PDF Copy plugin
* I could not find an official way to use with vite, this small plugin
* does what is explained here: https://github.com/wojtekmaj/react-pdf/blob/v5.x/README.md
* but for Vitejs
* @type {import('vite').PluginOption}
* */
const copyPdfData = () => {
const prod = process.env.NODE_ENV === 'production'
const pdfjsDistPath = path.dirname(require.resolve('pdfjs-dist/package.json'))
const pdfWorkerPath = path.join(pdfjsDistPath, 'build', 'pdf.worker.min.js')
const cMapsDir = path.join(pdfjsDistPath, 'cmaps')
const standardFontsDir = path.join(pdfjsDistPath, 'standard_fonts')
const copy_data = (root = '') => {
copySync(pdfWorkerPath, `${root}pdf.worker.min.js`, {
overwrite: true,
})
copySync(cMapsDir, `${root}cmaps/`, {
overwrite: true,
})
copySync(standardFontsDir, `${root}standard_fonts/`, {
overwrite: true,
})
}
return {
name: 'vite-plugin-copy-react-pdf',
buildStart: !prod
? () => {
copy_data('public/')
}
: null,
writeBundle: prod
? () => {
copy_data('build/')
}
: null,
}
}
// Project based aliases
const teiaAliases = {
'@atoms': path.resolve(__dirname, 'src', 'atoms'),
'@components': path.resolve(__dirname, 'src', 'components'),
'@constants': path.resolve(__dirname, 'src', 'constants'),
'@context': path.resolve(__dirname, 'src', 'context'),
'@data': path.resolve(__dirname, 'src', 'data'),
'@pages': path.resolve(__dirname, 'src', 'pages'),
'@hooks': path.resolve(__dirname, 'src', 'hooks'),
'@icons': path.resolve(__dirname, 'src', 'icons'),
'@styles': path.resolve(__dirname, 'src', 'styles'),
'@style': './index.module.scss',
'@utils': path.resolve(__dirname, 'src', 'utils'),
'@types': path.resolve(__dirname, 'src', 'types.d.ts'),
}
export default defineConfig(({ mode }) => {
const prod = mode === 'production'
let prod_plugs = []
if (prod) {
prod_plugs.push(eslintPlugin())
}
return {
base: process.env.GH_BASE_URL || '/',
clearScreen: prod,
appType: 'mpa',
plugins: [
...prod_plugs,
filterReplace(
[
{
filter: 'node_modules/@airgap/beacon-ui/dist/esm/utils/qr.js',
replace: {
from: "import * as qrcode from 'qrcode-generator';",
to: "import qrcode from 'qrcode-generator';",
},
},
{
filter: [
'node_modules/@airgap/beacon-dapp/dist/walletbeacon.dapp.min.js',
'node_modules/@airgap/beacon-ui/dist/cjs/ui/alert/alert-templates.js',
'node_modules/@airgap/beacon-ui/dist/esm/ui/alert/alert-templates.js',
],
replace: {
from: /\\n@media\s*\(min-height:\s*700px\).*translateY\(-50%\);\\n\s*\}\\n\}/g,
to: '',
},
},
],
{
apply: 'build',
enforce: 'post',
}
),
react(),
splitVendorChunkPlugin(),
viteTsconfigPaths(),
// import svg as ReactComponent
svgrPlugin(),
// import md as ReactComponent
mdPlugin({ mode: 'react' }),
// our custom plugin
//copyPdfData(),
// EJS replacement in HTML
ViteEjsPlugin({
BUILD_COMMIT: commitHash,
}),
],
define: {
global: 'globalThis',
},
server: {
host: true,
port: 3000,
},
build: {
commonjsOptions: {
transformMixedEsModules: true
},
outDir: 'build',
sourcemap: !prod,
rollupOptions: {
plugins: [rollupNodePolyFill()],
output: {
// manualChunks: processChunks,
manualChunks: {
three: ['three'],
contracts: [
'@taquito/beacon-wallet',
'@taquito/michelson-encoder',
'@stablelib/ed25519',
'@stablelib/nacl',
'@stablelib/x25519-session',
'@taquito/taquito',
],
pdf: ['react-pdf', 'pdfjs-dist'],
ui: [
'classnames',
'prop-types',
'react',
'react-router-dom',
'react-dom',
'framer-motion',
'zustand',
],
},
},
},
},
optimizeDeps: {
commonjsOptions: {
transformMixedEsModules: true,
},
esbuildOptions: {},
},
resolve: {
alias: {
'readable-stream': 'vite-compatible-readable-stream',
stream: 'vite-compatible-readable-stream',
path: require.resolve('path-browserify'),
util: 'rollup-plugin-node-polyfills/polyfills/util',
...teiaAliases,
},
},
}
})