-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathrollup.config.js
70 lines (65 loc) · 2.31 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
import fs from 'fs';
import changeCase from 'change-case';
import createBanner from 'create-banner';
import commonjs from '@rollup/plugin-commonjs';
import nodeResolve from '@rollup/plugin-node-resolve';
import postcss from 'rollup-plugin-inline-postcss';
import replace from '@rollup/plugin-replace';
import terser from '@rollup/plugin-terser';
import typescript from '@rollup/plugin-typescript';
import config from './tsconfig.json';
const pkg = JSON.parse(fs.readFileSync(`${process.cwd()}/package.json`));
const isCropperJS = pkg.name === 'cropperjs';
const normalizeGlobalName = (name) => changeCase.pascalCase(name.replace(/[a-z]+-/, ''));
const name = isCropperJS ? 'Cropper' : normalizeGlobalName(pkg.name);
const banner = createBanner({
data: {
name: isCropperJS ? 'Cropper.js' : name,
year: '2015-present',
},
template: 'inline',
});
const bundles = ['unbundled', 'bundled'];
const formats = ['esm', 'umd'];
const modes = ['development', 'production'];
const external = Object.keys(pkg.dependencies || {});
const globals = external.reduce((map, key) => {
map[key] = normalizeGlobalName(key);
return map;
}, {});
export default bundles.reduce((configs, bundle) => {
const isBundled = bundle === 'bundled';
return configs.concat(formats.map((format) => {
const isESM = format === 'esm';
return {
input: 'src/index.ts',
output: modes.reduce((outputs, mode) => {
const isProduction = mode === 'production';
return outputs.concat(!isBundled && isProduction ? [] : {
format,
name: isESM ? undefined : name,
banner: isCropperJS ? banner : undefined,
globals: isBundled ? undefined : globals,
file: (isESM ? pkg.module : pkg.main)
.replace('.raw', isBundled ? '' : '.raw')
.replace('.js', `${isProduction ? '.min' : ''}.js`),
plugins: isProduction ? [terser()] : undefined,
});
}, []),
external: isBundled ? undefined : external,
plugins: [
nodeResolve(),
commonjs(),
postcss({
include: [/\/style\.ts$/],
styleRegex: /(?:export default `)([^`]+)(?:`;)/g,
}),
typescript(config.compilerOptions),
replace({
preventAssignment: true,
__VERSION__: pkg.version,
}),
],
};
}));
}, []);