-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.mts
More file actions
188 lines (169 loc) · 6.17 KB
/
Copy pathvite.config.mts
File metadata and controls
188 lines (169 loc) · 6.17 KB
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
179
180
181
182
183
184
185
186
187
188
import fs from 'node:fs';
import { createRequire } from 'node:module';
import path from 'node:path';
import typescript from '@rollup/plugin-typescript';
import { languageConfiguration, textMateGrammar } from '@coderline/alphatab-language-server';
import type { OutputOptions, OutputPlugin } from 'rollup';
import license from 'rollup-plugin-license';
import { type MinifyOptions, minify } from 'terser';
import {
defaultClientMainFields,
defineConfig,
type LibraryOptions,
type UserConfig
} from 'vite';
const require = createRequire(import.meta.url);
const projectDir = import.meta.dirname;
const bravuraWoff2Path = require.resolve('@coderline/alphatab/font/Bravura.woff2');
const sonivoxSf3Path = require.resolve('@coderline/alphatab/soundfont/sonivox.sf3');
const terserOptions: MinifyOptions = {
mangle: {
properties: {
regex: /^_/
}
}
};
function getGitBranch(): string {
const headPath = path.resolve(projectDir, '.git/HEAD');
if (!fs.existsSync(headPath)) {
return '';
}
const buf = fs.readFileSync(headPath, 'utf8');
const match = /ref: refs\/heads\/([^\n]+)/.exec(buf);
return match ? match[1] : '';
}
function licenseHeaderPlugin() {
return license({
banner: {
commentStyle: 'ignored',
content: { file: path.resolve(projectDir, 'LICENSE.header') },
data() {
return {
branch: getGitBranch(),
build: process.env.GITHUB_RUN_NUMBER || 0
};
}
}
});
}
function terserMinPlugin(): OutputPlugin {
return {
name: 'min',
async writeBundle(opts, bundle) {
for (const file of Object.keys(bundle)) {
const chunk = bundle[file];
if ((file.endsWith('.mjs') || file.endsWith('.js')) && chunk.type === 'chunk' && chunk.isEntry) {
const o = { ...terserOptions };
if (opts.format === 'es') {
o.module = true;
} else if (opts.format === 'cjs') {
o.toplevel = true;
}
const result = await minify(chunk.code, o);
const outputFile = path.resolve(
opts.dir!,
file.replace('.mjs', '.min.mjs').replace('.js', '.min.js')
);
await fs.promises.writeFile(outputFile, result.code!);
}
}
}
};
}
function baseConfig(): UserConfig {
return {
esbuild: false,
plugins: [licenseHeaderPlugin()],
build: {
emptyOutDir: false,
lib: { entry: {} },
minify: false,
rollupOptions: {
external: [/^vscode/, /node:\w+/],
output: [],
onLog(level, log, handler) {
if (log.code === 'CIRCULAR_DEPENDENCY' || log.code === 'EMPTY_BUNDLE') {
return;
}
handler(level, log);
}
}
}
};
}
export default defineConfig(({ mode }) => {
const config = baseConfig();
config.build!.sourcemap = true;
config.resolve ??= {};
config.resolve.mainFields = defaultClientMainFields.filter(f => f !== 'browser');
config.resolve.mainFields.unshift('require');
if (mode === 'previewApp') {
// Preview app: UMD bundle that runs in the webview, bundles alphaTab in.
config.plugins!.unshift(typescript({ tsconfig: './tsconfig.json', module: 'preserve', include: ['**/*.ts'] }));
const lib = config.build!.lib! as LibraryOptions;
lib.entry = { preview: path.resolve(projectDir, 'src/preview/app/index.ts') };
config.plugins!.push({
name: 'import-meta',
resolveImportMeta() {
return '{}';
}
});
(config.build!.rollupOptions!.output as OutputOptions[]).push({
dir: 'dist/',
format: 'umd',
name: 'preview',
entryFileNames: '[name].js',
chunkFileNames: '[name].js',
plugins: [terserMinPlugin()]
});
config.plugins!.push({
name: 'copy-alphatab-assets',
apply: 'build',
async writeBundle() {
const outDir = path.join(projectDir, 'dist', 'assets');
await fs.promises.mkdir(outDir, { recursive: true });
await fs.promises.copyFile(sonivoxSf3Path, path.join(outDir, 'sonivox.sf3'));
await fs.promises.copyFile(bravuraWoff2Path, path.join(outDir, 'Bravura.woff2'));
}
});
config.plugins!.push({
name: 'app-html',
apply: 'build',
async writeBundle() {
await fs.promises.copyFile(
path.join(projectDir, 'src/preview/app/index.html'),
path.join(projectDir, 'dist/preview.html')
);
}
});
return config;
}
// Extension host: CommonJS, vscode left external.
config.plugins!.unshift(typescript({ tsconfig: './tsconfig.json', module: 'preserve', include: ['**/*.ts'] }));
const lib = config.build!.lib! as LibraryOptions;
lib.entry = {
extension: path.resolve(projectDir, 'src/extension.ts'),
server: path.resolve(projectDir, 'src/server.ts')
};
(config.build!.rollupOptions!.output as OutputOptions[]).push({
dir: 'dist/',
format: 'cjs',
entryFileNames: '[name].js'
});
config.plugins!.push({
name: 'language-files',
apply: 'build',
async buildStart() {
await fs.promises.writeFile(
path.join(projectDir, 'language-configuration.json'),
JSON.stringify(languageConfiguration, null, 2)
);
await fs.promises.mkdir(path.join(projectDir, 'syntaxes'), { recursive: true });
await fs.promises.writeFile(
path.join(projectDir, 'syntaxes/alphatex.tmLanguage.json'),
JSON.stringify(textMateGrammar, null, 2)
);
}
});
return config;
});