-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollup.config.ts
153 lines (139 loc) · 3.24 KB
/
rollup.config.ts
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
import { resolve } from 'path';
import { fileURLToPath } from 'url';
import { createRequire } from 'module';
import { RollupOptions, defineConfig } from 'rollup';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import esbuild from 'rollup-plugin-esbuild';
import json from '@rollup/plugin-json';
import glslify from 'rollup-plugin-glslify';
import replace from '@rollup/plugin-replace';
import sourcemaps from 'rollup-plugin-sourcemaps';
// import alias from '@rollup/plugin-alias';
import dts from 'rollup-plugin-dts';
import { visualizer } from 'rollup-plugin-visualizer';
import terser from '@rollup/plugin-terser';
import worker from 'rollup-plugin-web-worker-loader';
const rq = createRequire(import.meta.url);
const pkg = rq('./package.json');
const ROOT = fileURLToPath(import.meta.url);
const DEV = process.env.NODE_ENV === 'development';
const ANALYZER = process.env.BUILD_ANALYZER === 'true';
const MINIFY = process.env.MINIFY;
const PROD = !DEV;
const r = (p: string) => resolve(ROOT, '..', p);
const getFileName = (name) => {
const arr = name.split('.');
if (MINIFY) {
arr.splice(arr.length - 1, 0, 'min');
return arr.join('.');
}
return name;
}
const external = [
...Object.keys(pkg.dependencies),
'gl-matrix/mat4',
'gl-matrix/mat3',
'gl-matrix/quat',
'gl-matrix/vec2',
'gl-matrix/vec3',
'gl-matrix/vec4',
];
const plugins = [
// alias({
// entries: [
// { find: '@', replacement: r('./src') },
// ],
// }),
replace({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
preventAssignment: true,
}),
glslify(),
json({
namedExports: true,
}),
worker({
sourcemap: true,
inline: true,
extensions: ['.js', 'ts'],
}),
commonjs(),
nodeResolve({
preferBuiltins: false,
extensions: ['.ts', '.js', '.mjs', '.json']
}),
esbuild({ target: 'esnext' }),
sourcemaps(),
...(MINIFY ? [
terser({
compress: true,
}),
] : []),
...(ANALYZER ? [
visualizer({
sourcemap: true,
open: false
}),
] : [])
];
const esmBuild: RollupOptions = {
input: r('src/index.ts'),
output: {
format: 'esm',
file: getFileName(pkg.module),
sourcemap: !MINIFY,
},
external,
plugins,
onwarn(warning, warn) {
if (warning.code !== 'EVAL') warn(warning);
},
};
const cjsBuild: RollupOptions = {
input: r('src/index.ts'),
output: {
format: 'cjs',
file: getFileName(pkg.commonjs),
sourcemap: !MINIFY,
},
external,
plugins,
onwarn(warning, warn) {
if (warning.code !== 'EVAL') warn(warning);
},
}
const umdBuild: RollupOptions = {
input: r('src/index.ts'),
output: {
format: 'umd',
dir: undefined,
name: pkg.namespace,
sourcemap: !MINIFY,
file: getFileName(pkg.main),
},
external: [],
plugins,
onwarn(warning, warn) {
if (warning.code !== 'EVAL') warn(warning);
},
};
const typesBuild: RollupOptions = {
input: r('src/index.ts'),
output: {
format: 'esm',
file: pkg.types,
},
external,
plugins: [
dts({ respectExternal: true }),
],
};
const config = defineConfig([]);
config.push(esmBuild);
config.push(umdBuild);
if (PROD) {
config.push(cjsBuild);
}
config.push(typesBuild);
export default config;