-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.mjs
79 lines (72 loc) · 2.52 KB
/
rollup.config.mjs
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
import babel from '@rollup/plugin-babel';
import replace from 'rollup-plugin-replace';
import terser from '@rollup/plugin-terser';
import { cleandir } from 'rollup-plugin-cleandir';
import packageJson from './package.json' assert { type: 'json' };
const fileName = packageJson.name;
// Add a banner at the top of the minified code
const banner = [
`/*!`,
` * ${packageJson.name}@${packageJson.version} ${packageJson.repository.url}`,
` * Compiled ${new Date().toUTCString().replace(/GMT/g, 'UTC')}`,
` *`,
` * ${packageJson.name} is licensed under the MIT License.`,
` * http://www.opensource.org/licenses/mit-license`,
` */`,
].join('\n');
/**
* Generates output configurations for Rollup.
* @param {boolean} pMinify - Whether to generate minified configurations.
* @returns {Object[]} An array of output configurations.
*/
const generateOutputConfigs = (pMinify) => {
const outputFormats = ['iife', 'es'];
return outputFormats.map((pFormat) => {
const isMinified = pMinify ? '.min' : '';
const fileExtension = pFormat === 'es' ? 'mjs' : 'js';
// Uppercase library name for global IIFE represeting this bindle. [LibraryNameBundle].bundleInstance.foo
const iifeName = pFormat === 'iife' ? `${packageJson.name.slice(0, 1).toUpperCase()}${packageJson.name.slice(1, packageJson.name.length)}Bundle` : undefined;
return {
file: `dist/${pFormat}/${fileName}${isMinified}.${fileExtension}`,
format: pFormat,
name: pFormat === 'iife' ? iifeName : undefined,
sourcemap: true,
banner: pMinify ? undefined : banner,
plugins: pMinify
? [
terser({
mangle: {
// Exclude the bundle name from mangling
reserved: iifeName ? [iifeName] : [],
},
module: iifeName ? false : true,
toplevel: iifeName ? false : true,
keep_classnames: iifeName ? false : true,
format: {
comments: 'some',
preamble: banner,
},
}),
]
: [],
};
});
};
const config = {
input: 'src/lens.mjs',
output: [
// Build regular
...generateOutputConfigs(false),
// Build minified
...generateOutputConfigs(true),
],
plugins: [
// Clean the directory first
cleandir('./dist'),
// Replace version in source with package.json version
replace({ ['VERSION_REPLACE_ME']: packageJson.version }),
// Transpile ES6 to ES5 (CommonJS)
babel({ babelHelpers: 'bundled' }),
],
};
export default [config];