-
Notifications
You must be signed in to change notification settings - Fork 193
/
vite-plugin-extension.ts
79 lines (78 loc) · 2.93 KB
/
vite-plugin-extension.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
import { type UserConfig, type Plugin } from 'vite'
import { type OutputOptions } from 'rollup'
import { resolve } from 'path'
import { rename } from 'fs/promises'
export default function (): Plugin {
return {
name: 'extension',
config(config, env) {
const common: UserConfig = {
base: './',
resolve: {
alias: [
{
// Replace assets.ts for extension
find: /.*\/assets(\.ts)?$/,
replacement: resolve(
__dirname,
'src/environments/extension/assets.ts'
),
},
],
},
build: {
target: 'ESNext',
rollupOptions: {
output: {
// Add a hash to the filename to prevent caching
// but rename it in the writeBundle callback.
entryFileNames: '[name].js?[hash]',
chunkFileNames: '[name].js?[hash]',
assetFileNames: '[name][extname]?[hash]',
},
},
},
}
switch (env.mode) {
case 'extension-editor':
{
common.build.outDir = 'pages'
common.build.rollupOptions.input = {
index: resolve(__dirname, 'index.html'),
}
}
break
case 'extension-entry':
{
common.build.outDir = 'javascript'
common.build.rollupOptions.input = {
index: resolve(
__dirname,
'src/environments/extension/entry.ts'
),
}
common.publicDir = false
const output = common.build.rollupOptions
.output as OutputOptions
output.format = 'iife'
}
break
default:
throw Error(`Unknown mode: ${env.mode}`)
}
return common
},
async writeBundle(options, bundle) {
// Remove hash from filename
for (const key in bundle) {
const b = bundle[key]
const type = b.type
if (type !== 'chunk' && type !== 'asset') {
continue
}
const fileName = resolve(options.dir, b.fileName)
await rename(fileName, fileName.replace(/\?[0-9a-f]+$/, ''))
}
},
}
}