-
Notifications
You must be signed in to change notification settings - Fork 101
/
vite.config.js
151 lines (136 loc) · 4.24 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
/**
* Copyright 2022 The Subscribe with Google Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {defineConfig} from 'vite';
import {readFileSync, readdirSync, writeFileSync} from 'fs';
import {visualizer} from 'rollup-plugin-visualizer';
import commonjs from '@rollup/plugin-commonjs';
import replace from '@rollup/plugin-replace';
import {resolveConfig} from './build-system/tasks/compile-config';
const args = require('./build-system/tasks/args');
// Choose plugins.
const config = resolveConfig();
const replacementValues = Object.entries(config).reduce((obj, [key, value]) => {
obj[key] = `const ${key} = '${value}' ??`;
return obj;
}, {});
const plugins = [
commonjs({
transformMixedEsModules: true,
}),
replace({
delimiters: ['const ', ' = '],
include: ['./src/constants.ts'],
preventAssignment: false,
values: replacementValues,
}),
// Wrap generated code in outer function to avoid leaking into global scope.
// b/293444391.
{
name: 'add-outer-iife',
apply: 'build',
generateBundle(options, bundle) {
const chunks = Object.values(bundle).filter(
(entry) => entry.type === 'chunk' && entry.code
);
for (const chunk of chunks) {
chunk.code = `(() => { ${chunk.code} })();\n`;
}
},
},
// Point sourcemaps to a Swgjs release on GitHub.
{
name: 'fix-sourcemaps',
apply: 'build',
writeBundle(outputConfig) {
const outputDir = outputConfig.dir || '';
const filenames = readdirSync(outputDir).filter((filename) =>
filename.endsWith('.map')
);
for (const filename of filenames) {
// Load sourcemap.
const path = outputDir + '/' + filename;
const sourcemap = JSON.parse(
readFileSync(outputDir + '/' + filename).toString()
);
// Point to a Swgjs release on GitHub.
sourcemap.sourceRoot = `https://raw.githubusercontent.com/subscriptions-project/swg-js/${config.INTERNAL_RUNTIME_VERSION}/`;
// Fix relative paths.
sourcemap.sources = sourcemap.sources.map((source) =>
source.replace(/^\.\.\/src\//, 'src/')
);
// Save changes.
writeFileSync(path, JSON.stringify(sourcemap) + '\n');
}
},
},
];
if (args.visualize) {
// Visualize bundle to see which modules are taking up space.
plugins.push(
visualizer({
filename: './build/rollup-visualization.html',
})
);
}
const builds = {
basic: {
output: args.minifiedBasicName || 'basic-subscriptions.js',
input: './src/basic-main.ts',
},
classic: {
output: args.minifiedName || 'subscriptions.js',
input: './src/main.ts',
},
gaa: {
output: args.minifiedGaaName || 'subscriptions-gaa.js',
input: './src/gaa-main.ts',
},
};
const {input, output} = builds[args.target || 'classic'];
export default defineConfig({
plugins,
build: {
emptyOutDir: false,
commonjsOptions: {
sourceMap: true,
},
target: 'ios12',
minify: 'terser',
terserOptions: {
// eslint-disable-next-line google-camelcase/google-camelcase
mangle: {properties: {keep_quoted: true, regex: '_$'}},
// Disables converting computed properties ({['hello']: 5}) into regular prop ({ hello: 5}).
// This was an assumption baked into closure.
compress: {
// eslint-disable-next-line google-camelcase/google-camelcase
computed_props: false,
// Settled on this count by incrementing number until there was no more
// effect on minification quality.
passes: 3,
},
},
rollupOptions: {
input,
output: [
{
format: 'iife',
entryFileNames: output,
sourcemap: true,
},
],
},
},
});