Repository navigation
Expand file tree
/
Copy pathrollup.config.js
More file actions
134 lines (129 loc) · 3.44 KB
/
Copy pathrollup.config.js
File metadata and controls
134 lines (129 loc) · 3.44 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
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
import typescript from '@rollup/plugin-typescript';
// Externals provided by the `vega` browser bundle when loaded via <script>.
const vegaExternals = ['vega-scenegraph'];
const vegaGlobals = {
'vega-scenegraph': 'vega',
};
/**
* Strips comments and collapses whitespace inside the template literals of the
* shader modules. WGSL is written with the same care as the TypeScript around
* it, and terser will not touch a template literal's text, so the source
* comments ship verbatim: about 30 KB of the minified bundle, a fifth of it.
*
* Only the text spans of a template literal are touched. Interpolations are
* copied through untouched, and whitespace collapses to a single space rather
* than vanishing, so tokens stay separated.
*/
const minifyWgsl = () => ({
name: 'minify-wgsl',
transform(code, id) {
if (!/src[\\/]shaders[\\/].*\.ts$/.test(id)) {
return null;
}
const squeeze = text =>
text
.replace(/\/\*[\s\S]*?\*\//g, ' ')
.replace(/\/\/.*/g, '')
.replace(/\s+/g, ' ');
let out = '';
let i = 0;
// depth of ${ } we are inside, so a nested template is handled too
const stack = [];
let text = null;
while (i < code.length) {
const c = code[i];
if (text !== null) {
if (c === '\\') {
text += code.slice(i, i + 2);
i += 2;
continue;
}
if (c === '`') {
out += squeeze(text) + '`';
text = null;
i++;
stack.pop();
continue;
}
if (c === '$' && code[i + 1] === '{') {
out += squeeze(text) + '${';
text = null;
stack.push('expr');
i += 2;
continue;
}
text += c;
i++;
continue;
}
if (c === '`') {
out += '`';
text = '';
stack.push('tpl');
i++;
continue;
}
if (c === '}' && stack[stack.length - 1] === 'expr') {
stack.pop();
out += '}';
text = '';
i++;
continue;
}
out += c;
i++;
}
return { code: out, map: { mappings: '' } };
},
});
const plugins = () => [
minifyWgsl(),
commonjs(),
resolve({ browser: true }),
typescript({
tsconfig: './tsconfig.json',
noEmit: false,
outputToFilesystem: true,
}),
];
export default [
// UMD builds for <script> usage: bundles d3-color and the path/geometry
// helpers so only vega itself is required on the page.
{
input: 'index.ts',
external: vegaExternals,
output: [
{
file: 'build/vega-webgpu-renderer.js',
format: 'umd',
name: 'vegaWebGPURenderer',
sourcemap: true,
globals: vegaGlobals,
},
{
file: 'build/vega-webgpu-renderer.min.js',
format: 'umd',
name: 'vegaWebGPURenderer',
sourcemap: true,
globals: vegaGlobals,
plugins: [terser()],
},
],
plugins: plugins(),
},
// ESM build for bundlers: the esm dependencies stay external, the commonjs
// geometry helpers are bundled, so nothing here imports commonjs.
{
input: 'index.ts',
external: [...vegaExternals, 'd3-color'],
output: {
file: 'build/vega-webgpu-renderer.module.js',
format: 'esm',
sourcemap: true,
},
plugins: plugins(),
},
];