File size: 2,790 Bytes
ea35075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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',
        async writeBundle(options, bundle) {
            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]+$/, ''))
            }
        },
        config(config, env) {
            const common: UserConfig = {
                base: './',
                resolve: {
                    alias: [
                        {
                            find: /.*\/assets(\.ts)?$/,
                            replacement: resolve(
                                __dirname,
                                'src/environments/extension/assets.ts'
                            ),
                        },
                    ],
                },
                build: {
                    target: 'ESNext',
                    emptyOutDir: false,
                    rollupOptions: {
                        output: {
                            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
        },
    }
}