-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvite.config.ts
158 lines (146 loc) · 4.16 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
155
156
157
158
import yamlPlugin from '@rollup/plugin-yaml'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import fs from 'fs'
import { defineConfig } from 'vite'
import yaml from 'yaml'
const LOCALES_PATH = 'src/locales/'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
pluginManifest(),
availableLocales(),
localeFiles(),
yamlPlugin(),
svelte(),
],
define: {
__VERSION__: JSON.stringify(process.env.npm_package_version),
},
resolve: {
alias: {
'~tint': '/node_modules/tint/dist',
'@src': '/src',
},
},
css: {
preprocessorOptions: {
sass: {
additionalData: (d) => {
const prepend = `@use "@src/styles/utils.sass" as tint\n`
const match = d.match(/^\s*/)
const spaces = match ? match[0] : ''
return `${spaces}${prepend}\n${d}`
},
},
},
},
build: {
minify: false,
rollupOptions: {
input: {
main: 'index.html',
offscreen: 'offscreen.html',
'service-worker': 'src/scripts/service-worker.ts',
},
output: {
entryFileNames: `[name].js`,
chunkFileNames: `assets/[name].js`,
assetFileNames: `assets/[name].[ext]`,
},
},
},
})
/**
* Copies the template manifest.json from src/, replaces the version number,
* adds experimental mappings from the config file and then emits it into
* dist/.
*/
function pluginManifest() {
return {
name: 'copy-extension-manifest',
async buildStart() {
this.addWatchFile('package.json')
this.addWatchFile('src/manifest.json')
this.addWatchFile('configs/experiment-mappings.yaml')
},
async generateBundle() {
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf-8'))
const manifest = JSON.parse(fs.readFileSync('src/manifest.json', 'utf-8'))
manifest.version = packageJson.version
this.emitFile({
type: 'asset',
fileName: 'manifest.json',
source: JSON.stringify(manifest, null, 2),
})
},
}
}
/** Copies the manifest translations from src/locales/ into dist/_locales/. */
function localeFiles() {
return {
name: 'copy-locale-files',
async buildStart() {
this.addWatchFile('src/locales/')
},
async generateBundle() {
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf-8'))
const manifest = JSON.parse(fs.readFileSync('src/manifest.json', 'utf-8'))
const toManifestFormat = (content: { [key: string]: string }) => {
// the desired format is:
// key: { message: value }
return Object.fromEntries(
Object.entries(content).map(([key, value]) => [
key,
{ message: value },
]),
)
}
const files = fs
.readdirSync('src/locales/')
// only yaml files
.filter((file) => file.endsWith('.yaml'))
// load yaml files and return locale and content
.map((file) => {
const locale = file.replace('.yaml', '')
const content = yaml.parse(
fs.readFileSync(`src/locales/${file}`, 'utf-8'),
)
return { locale, content: toManifestFormat(content.manifest) }
})
for (const { locale, content } of files) {
this.emitFile({
type: 'asset',
fileName: `_locales/${locale}/messages.json`,
source: JSON.stringify(content, null, 2),
})
}
},
}
}
function availableLocales() {
const virtualModuleId = 'virtual:available-locales'
const resolvedVirtualModuleId = '\0' + virtualModuleId
return {
name: 'locale-importer', // required, will show up in warnings and errors
resolveId(id) {
if (id === virtualModuleId) {
return resolvedVirtualModuleId
}
},
load(id) {
if (id !== resolvedVirtualModuleId) {
return
}
const locales = fs
.readdirSync(LOCALES_PATH)
.filter((file) => file.endsWith('.yaml'))
.map((file) => file.replace('.yaml', ''))
// return supportedLanguages -> locales
return `export const availableLocales = ${JSON.stringify(
locales,
null,
2,
)}`
},
}
}