This repository has been archived by the owner on Jul 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
151 lines (147 loc) · 4.21 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
import { resolve } from 'node:path'
import { defineConfig } from 'vite'
import fs from 'node:fs'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import banner from 'vite-plugin-banner'
import { visualizer } from 'rollup-plugin-visualizer'
import smvp from 'speed-measure-vite-plugin'
const version = JSON.parse(fs.readFileSync('./package.json', 'utf-8')).version
function genBannerStr(customItems = []) {
const items = [
['name', 'Figma CSS Better'],
['namespace', 'https://github.com/lbb00'],
['version', version],
['description', 'Figma CSS 转为微信小程序样式,rpx,figma,微信,小程序'],
['author', 'lbb00'],
['homepage', 'https://github.com/lbb00/figma-css-better'],
['supportURL', 'https://github.com/lbb00/figma-css-better/issues'],
[
'updateURL',
'https://github.com/lbb00/figma-css-better/raw/master/figma-css-better.user.js',
],
[
'downloadURL',
'https://github.com/lbb00/figma-css-better/raw/master/figma-css-better.user.js',
],
['match', '*://www.figma.com/file/*'],
['match', 'https://lbb00.github.io/figma-css-better/setting'],
['run-at', 'document-end'],
['icon', 'https://www.google.com/s2/favicons?domain=figma.com'],
[
'license',
'MIT; https://github.com/lbb00/figma-css-better/blob/main/LICENSE',
],
['grant', 'unsafeWindow'],
['grant', 'GM_getValue'],
['grant', 'GM_setValue'],
['grant', 'GM_deleteValue'],
['grant', 'GM_xmlhttpRequest'],
...customItems,
]
const maxLabelLen = items.reduce(
(max, [i]) => (max > i.length ? max : i.length),
0
)
return (
'// ==UserScript==\n' +
items.reduce((str, [label, content]) => {
label = label.trim()
content = content.trim()
return (
str +
`// @${
label.length < maxLabelLen
? label +
[...new Array(maxLabelLen - label.length)].map(() => ' ').join('')
: label
} ${content}\n`
)
}, '') +
'// ==/UserScript==\n'
)
}
export default defineConfig(({ mode }) => {
const isDev = mode === 'development'
const previewPort = 8087
if (isDev) {
const LOCAL_URL = `http://127.0.0.1:${previewPort}/figma-css-better.local.user.js`
// https://github.com/Tampermonkey/tampermonkey/issues/723
fs.writeFileSync(
'./figma-css-better.dev.user.js',
`${genBannerStr()}
(async function () {
let isInit = false
function checkReload() {
GM.xmlHttpRequest({
url: '${LOCAL_URL}',
onload: async (response) => {
const text = response.responseText
const storageData = await GM.getValue('DEV_CachedScriptKey', '')
if (text.length !== storageData.length || text !== storageData) {
console.log('reload!')
await GM.setValue('DEV_CachedScriptKey', text)
window.location.reload()
} else {
if(!isInit) {
try{
eval(text)
}catch(e){
console.log(e)
}
isInit = true
}
setTimeout(checkReload, 1000)
}
},
onerror: () => {
setTimeout(checkReload, 1000)
},
})
}
checkReload()
})()
`
)
}
return {
root: '.',
esbuild: {
minifyIdentifiers: false,
minifyWhitespace: false,
},
build: {
target: 'esnext',
outDir: './',
minify: !isDev,
lib: {
name: 'FigmaCssBetter',
entry: resolve(__dirname, 'src/main.js'),
formats: ['iife'],
fileName: () => {
return isDev
? 'figma-css-better.local.user.js'
: 'figma-css-better.user.js'
},
},
},
preview: {
port: previewPort,
},
define: {
'process.env.NODE_ENV': JSON.stringify(mode),
'process.env.LANG': JSON.stringify('cn'),
},
plugins: smvp([
visualizer(),
svelte({
emitCss: false,
}),
!isDev &&
banner({
outDir: './',
verify: false,
content: genBannerStr(),
}),
]),
}
})