-
Notifications
You must be signed in to change notification settings - Fork 2
/
rollup.config.js
123 lines (106 loc) · 3.08 KB
/
rollup.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
// @ts-check
import babel from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import url from '@rollup/plugin-url';
import copy from 'rollup-plugin-copy';
import pkg from './package.json' with { type: 'json' };
const extensions = ['.js', '.jsx', '.ts', '.tsx'];
const pluginTransformConstToLet = () => ({
renderChunk(chunk) {
return chunk.replace(/\bconst\b/g, 'let');
},
});
// Replace well-known string constants with shorter, yet unique, names!
const pluginShortenKnownStrings = () => ({
renderChunk(chunk) {
const pairs = [
['v-movement', 'mv'],
['draw-console', 'dc'],
['viewport', 'vp'],
['drag-state', 'ds'],
['pointer-target', 'pt'],
['spring-constraint', 'sc'],
['bounding-box', 'bb'],
['circle-shape', 'cs'],
['user-controlled', 'uc'],
['debug-drawable-circle', 'ddc'],
['debug-drawable-rect', 'ddr'],
['enemy-miasma', 'em'],
['enemy-targetable', 'et'],
['impedance-value', 'imv'],
['health-value', 'hv'],
['enemy-impedance', 'eim'],
];
const shorts = new Set();
for (const [long, short] of pairs) {
if (shorts.has(short))
throw new Error(`Need a unique short name! Got ${short}`);
shorts.add(short);
chunk = chunk.replace(
new RegExp(`['"]${long}['"]`, 'g'),
JSON.stringify(short),
);
}
return chunk;
},
});
// Replace typescript assertions. Hacky to do it via regex, but it's well-known
// and easier than writing a rollup plugin. Note that this uses a negative
// lookbehind to avoid targeting the function definition itself, only supported
// in V8.
const pluginRemoveTypescriptAssertions = () => ({
renderChunk(chunk) {
return chunk.replace(
/(?<!function )(assertDefinedFatal|assertExhaustive|assertSpatialHandleIsInt)\([^)]+\)/g,
'',
);
},
});
export default {
input: './src/index.ts',
plugins: [
resolve({ extensions }),
// Copy imported static files into the dist folder
// limit is the number of bytes before the image is copied instead of inlined.
// 0 means always copy.
// Probably want to tweak this, depending on the size and format of the image.
url({
limit: 0,
include: [
'**/*.svg',
'**/*.png',
'**/*.jpg',
'**/*.gif',
'**/*.mp4',
'**/*.mp3',
'**/*.ttf',
],
}),
commonjs(),
babel({
extensions,
babelHelpers: 'bundled',
include: ['src/**/*', 'assets/**/*.ts'],
}),
replace({
preventAssignment: true,
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
}),
...(process.env.NODE_ENV === 'production'
? [
pluginTransformConstToLet(),
pluginShortenKnownStrings(),
pluginRemoveTypescriptAssertions(),
]
: []),
copy({ targets: [{ src: 'src/index.html', dest: 'dist/' }] }),
],
output: [
{
file: 'dist/bundle.js',
format: 'iife',
},
],
};