-
Notifications
You must be signed in to change notification settings - Fork 5
/
vite.config.js
178 lines (162 loc) · 4.94 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
/// <reference types="vitest" />
import { resolve, extname } from 'path'
import { readFileSync } from 'fs'
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// import monacoEditorPlugin from 'vite-plugin-monaco-editor'
import dotenv from 'dotenv'
dotenv.config({ path: './.env' })
/* eslint-disable no-unused-vars */
const { APP_ENV } = process.env
const DEV = APP_ENV === 'development'
const PREVIEW = APP_ENV === 'preview'
const PROD = APP_ENV === 'production'
const TEST = !!process.env.VITEST
/* eslint-enable no-unused-vars */
const DEBUG = true
const noop = () => {}
const console = new Proxy(globalThis.console, {
get: (t, prop) => (DEBUG ? t[prop] : noop),
})
/** @typedef {import('vite').UserConfig} ViteConf */
/** @typedef {import('vite').PluginOption} PluginOpt */
const SH_ENV = {
NAME: process.env.SHOPIFY_SHOP_NAME,
VER: process.env.VITE_SHOPIFY_API_VER,
AT: process.env.SHOPIFY_ACCESS_TOKEN,
}
console.log('\nShopify env variables')
console.table(SH_ENV)
/** @type {ViteConf["server"]} */
const serverOptions = {
port: 3000,
proxy: {
[`/admin/api/${SH_ENV.VER}`]: {
secure: false,
changeOrigin: true,
target: `https://${SH_ENV.NAME}.myshopify.com`,
headers: {
'X-Shopify-Access-Token': SH_ENV.AT,
},
},
},
}
/** @type {ViteConf["build"]} */
const buildOptions =
DEV || TEST
? {}
: {
target: 'es2020',
minify: true,
rollupOptions: {
output: {
format: 'iife',
},
// input: {
// content: './src/content-scripts/build-entry.ts',
// },
// output: {
// entryFileNames: `assets/[name].js`,
// chunkFileNames: `assets/[name].js`,
// assetFileNames: `assets/[name].[ext]`,
// },
// output: {
// entryFileNames: 'src/pages/[name]/index.js',
// chunkFileNames: 'assets/js/[name].[hash].js',
// assetFileNames: assetInfo => {
// const { dir, name: _name } = path.parse(assetInfo.name)
// const assetFolder = dir.split('/').at(-1)
// const name = assetFolder + firstUpperCase(_name)
// return `assets/[ext]/${name}.chunk.[ext]`
// },
// },
},
}
/* @ts-check */
/**
* @type {import('vite').UserConfigFn} options
*/
export default async ({ mode }) => {
console.log({ mode, APP_ENV, TEST })
return defineConfig({
plugins: [
// monacoEditorPlugin({
// languageWorkers: [
// 'editorWorkerService',
// 'css',
// 'html',
// 'json',
// 'typescript',
// ],
// }),
plainTextLoader({
ext: 'svg',
}),
react(),
// PROD && crx({ manifest }),
].filter(Boolean),
server: serverOptions,
resolve: {
alias: {
$utils: resolve(__dirname, './src/utils'),
$lib: resolve(__dirname, './src/lib'),
$common: resolve(__dirname, './src/content-scripts/common'),
$ui: resolve(__dirname, './src/content-scripts/common/ui'),
$hooks: resolve(__dirname, './src/content-scripts/common/hooks'),
'$query-clients': resolve(
__dirname,
'./src/content-scripts/query-clients.ts'
),
$router: resolve(__dirname, './src/content-scripts/router'),
$queries: resolve(__dirname, './src/content-scripts/queries'),
$routes: resolve(__dirname, './src/content-scripts/routes'),
$gql: resolve(__dirname, './src/content-scripts/generated/graphql.tsx'),
$types: resolve(__dirname, './src/content-scripts/types'),
},
},
build: buildOptions,
define: {
'process.env': {},
__DEV__: DEV,
},
test: {
environment: 'jsdom',
},
})
}
// ---------------------------------------
// Plugins
// ---------------------------------------
/**
*
* @param {object} options
* @param {string|RegExp|string[]|RegExp[]|Function} options.ext
* @param {string|RegExp|string[]|RegExp[]|Function} options.path
* @returns {PluginOpt}
*/
function plainTextLoader(opts = {}) {
function tryMatching(match, against, fnArgs = []) {
const type = typeof match
return (
(type === 'string' && match === against) ||
(type === 'string' && new RegExp(match).test(against)) ||
(match instanceof RegExp && match.test(against)) ||
(type === 'function' && match.apply(this, fnArgs)) ||
(Array.isArray(match) && match.some(m => tryMatching(m, against, fnArgs)))
)
}
return {
transform(code, id) {
// Try matching with extension
let canTransform =
opts.ext && tryMatching(opts.ext, extname(id), [code, id])
if (!canTransform && opts.path) {
canTransform = tryMatching(opts.path, id, [code, id])
}
if (canTransform) {
const fileContents = readFileSync(id, 'utf-8')
return `export default ${JSON.stringify(fileContents)}`
}
},
}
}