-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathrollup.config.js
222 lines (201 loc) · 5.67 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import terser from '@rollup/plugin-terser';
import filesize from 'rollup-plugin-filesize';
import ts from 'rollup-plugin-ts';
import pkg from './package.json' assert { type: 'json' };
import fs from 'fs';
import path from 'path';
const inputPath = 'src/anime.js';
const inputPathGUI = 'src/gui/gui.js';
const outputName = 'anime';
const jsDocTypes = fs.readFileSync('./src/types.js', 'utf-8').split('/* Exports */')[1];
/**
* @param {String} format
* @param {Boolean} [addTypes]
* @return {String}
*/
const banner = (format, addTypes) => {
const date = new Date();
return `/**
* anime.js - ${ format }
* @version v${ pkg.version }
* @author Julian Garnier
* @license MIT
* @copyright (c) ${ date.getFullYear() } Julian Garnier
* @see https://animejs.com
*/${addTypes ? jsDocTypes : ''}
`
}
/**
* @param {String} format
* @param {Boolean} [addTypes]
* @return {String}
*/
const GUIBanner = (format, addTypes) => {
const date = new Date();
return `/**
* anime.js GUI - ${ format }
* @version v${ pkg.version }
* @author Julian Garnier
* @license MIT
* @copyright (c) ${ date.getFullYear() } Julian Garnier
* @see https://animejs.com
*/${addTypes ? jsDocTypes : ''}
`
}
const terserModuleOptions = {
compress: {
passes: 10,
module: true,
},
mangle: true,
}
const terserScriptOptions = {
compress: {
passes: 10,
module: false,
},
mangle: true,
}
const terserStripComments = {
compress: false,
mangle: false,
}
const tasks = [];
const cleanupOptions = {
// Replace "import('./file.js')."
"import\\('\\.\\/[^']+\\.js'\\)\\.": '',
"/// <reference path='./types.js' />": '',
__packageVersion__: pkg.version.toString()
};
const cleanup = {
name: 'cleanup',
generateBundle(_, bundle) {
if (process.env.release) {
const guiPath = path.resolve('./lib/gui');
if (fs.existsSync(guiPath)) {
fs.rmSync(guiPath, { recursive: true, force: true });
console.log('Removed GUI folder from lib directory in release mode');
}
}
Object.keys(bundle).forEach((fileName) => {
const file = bundle[fileName];
let code = file.code;
for (const [find, replacement] of Object.entries(cleanupOptions)) {
const regExp = new RegExp(find, 'g');
code = code.replace(regExp, replacement);
}
file.code = code;
});
},
};
const prependTypes = {
name: 'prepend-file',
transform(code, id) {
if (id.includes('anime.js')) {
return {
code: `${jsDocTypes}\n${code}`,
map: null // If you're not handling source maps
};
}
return null;
}
};
tasks.push( // ESM
{
input: inputPath,
output: { file: pkg.module, format: 'esm', banner: banner('ESM', true) },
plugins: [prependTypes, cleanup]
},
);
if (!process.env.release) {
tasks.push( // GUI ESM
{
input: inputPathGUI,
output: { file: 'lib/gui/index.js', format: 'esm', banner: GUIBanner('ESM', true) },
plugins: [prependTypes, cleanup]
},
);
}
if (process.env.types) {
tasks.push( // TYPES
{
input: inputPath,
output: { file: './types/index.js', format: 'esm', banner: banner('ESM') },
plugins: [prependTypes, cleanup, ts()]
}
);
}
if (process.env.build || process.env.npm_config_minify) {
tasks.push( // ESM minified
{
input: inputPath,
output: { file: pkg.files[0] + '/anime.esm.min.js', format: 'esm', banner: banner('ESM') },
plugins: [cleanup, terser(terserModuleOptions), filesize({ showMinifiedSize: false })]
}
);
}
if (process.env.build) {
tasks.push( // UMD
{
input: inputPath,
output: { file: pkg.main, format: 'umd', name: outputName, banner: banner('UMD') },
plugins: [cleanup, terser(terserStripComments)]
}
);
tasks.push( // IIFE
{
input: inputPath,
output: { file: pkg.files[0] + '/anime.iife.js', format: 'iife', name: outputName, banner: banner('IIFE') },
plugins: [cleanup, terser(terserStripComments)]
}
);
tasks.push( // CJS
{
input: inputPath,
output: { file: pkg.exports['.'].require, format: 'cjs', name: outputName, banner: banner('CJS') },
plugins: [cleanup, terser(terserStripComments)]
}
);
tasks.push( // UMD, CJS & IIFE minified
{
input: inputPath,
output: [
{ file: pkg.files[0] + '/anime.umd.min.js', format: 'umd', name: outputName, banner: banner('UMD') },
{ file: pkg.files[0] + '/anime.min.cjs', format: 'cjs', name: outputName, banner: banner('CJS') },
{ file: pkg.files[0] + '/anime.iife.min.js', format: 'iife', name: outputName, banner: banner('IIFE') },
],
plugins: [cleanup, terser(terserScriptOptions)]
}
);
// tasks.push( // ES5
// {
// input: inputPath,
// output: { file: pkg.files[0] + '/anime.es5.iife.js', format: 'iife', name: outputName, banner: banner('ES5 IIFE') },
// plugins: [prependTypes, cleanup, babel({
// presets: ['@babel/preset-env'],
// babelHelpers: 'bundled',
// comments: false,
// parserOpts: {
// // @ts-ignore
// plugins: ['v8intrinsic', '@babel/plugin-transform-arrow-functions']
// }
// })]
// }
// );
// tasks.push( // ES5 Minified
// {
// input: inputPath,
// output: { file: pkg.files[0] + '/anime.es5.iife.min.js', format: 'iife', name: outputName, banner: banner('ES5 IIFE') },
// plugins: [cleanup, babel({
// presets: ['@babel/preset-env'],
// babelHelpers: 'bundled',
// comments: false,
// parserOpts: {
// // @ts-ignore
// plugins: ['v8intrinsic', '@babel/plugin-transform-arrow-functions']
// }
// })]
// }
// );
}
export default tasks;