-
Notifications
You must be signed in to change notification settings - Fork 11
/
rollup.config.js
113 lines (103 loc) · 2.53 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
import path from 'path';
import babel from '@rollup/plugin-babel';
import terser from '@rollup/plugin-terser';
import replace from '@rollup/plugin-replace';
import { DEFAULT_EXTENSIONS } from '@babel/core';
import typescript from '@rollup/plugin-typescript';
import pkg from './package.json' assert { type: 'json' };
const globals = {
'react': 'React',
'react-dom': 'ReactDOM',
'styled-components': 'styled',
'react-window': 'ReactWindow'
};
const input = './src/index.ts';
const name = 'ReactFunctionalSelect';
const external = (id) => !id.startsWith('\0') && !id.startsWith('.') && !path.isAbsolute(id);
/**
* Replace Plugin config
*/
const replacePlugin = replace({
preventAssignment: true,
'process.env.NODE_ENV': JSON.stringify('production'),
});
// Remove data-testid attribute (since undefined in non-test environments)
// Perform as final step in transformed, bundled code (for esm and cjs builds)
const removeTestIdPlugin = replace({
preventAssignment: true,
',"data-testid":undefined': '',
delimiters: ['', '']
});
/**
* Babel Plugin config (prevents use of root babel.config.js with babelrc and configFile as false)
*/
const babelPlugin = (useESModules) => {
const targets = useESModules ? { esmodules: true } : undefined;
return babel({
babelrc: false,
configFile: false,
babelHelpers: 'runtime',
exclude: 'node_modules/**',
extensions: [...DEFAULT_EXTENSIONS, '.ts', '.tsx'],
presets: [['@babel/preset-env', {targets, loose: true}], '@babel/preset-react'],
plugins: [
['@babel/plugin-transform-runtime', {useESModules}],
[
'babel-plugin-styled-components',
{
ssr: true,
pure: true,
minify: true,
fileName: false,
displayName: true,
transpileTemplateLiterals: true,
},
],
],
});
};
/**
* Common plugins in each build
*/
const commonPlugins = (useESModules = true) => [
typescript(),
replacePlugin,
babelPlugin(useESModules),
terser(),
removeTestIdPlugin,
];
export default [
// COMMONJS
{
external,
input,
output: {
file: pkg.main,
format: 'cjs',
exports: 'named',
},
plugins: commonPlugins(false),
},
// MODULE
{
external,
input,
output: {
file: pkg.module,
format: 'esm',
},
plugins: commonPlugins(true),
},
// BROWSER/UMD
{
external: Object.keys(globals),
input,
output: {
file: pkg.umd,
format: 'umd',
globals,
name,
},
plugins: commonPlugins(true),
},
];