-
Notifications
You must be signed in to change notification settings - Fork 8
/
rollup.config.js
124 lines (110 loc) · 3.09 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
import path from 'path';
import replace from 'rollup-plugin-replace';
import NodeResolve from 'rollup-plugin-node-resolve';
import typescript from 'rollup-plugin-typescript2';
import postcss from 'rollup-plugin-postcss';
// eslint-disable-next-line import/no-unresolved
import closure from '@ampproject/rollup-plugin-closure-compiler';
import camelCase from 'camelcase';
import autoprefixer from 'autoprefixer';
import packageInfo from './package.json';
const isProd = process.env.NODE_ENV === 'production';
const baseExternalDependencies = {
react: 'React',
'react-dom': 'ReactDOM',
// currently we use TypeScript so we don't need `PropTypes`
// 'prop-types': 'PropTypes',
};
const modularExternalDependencies = [
...Object.keys(baseExternalDependencies),
...Object.keys(packageInfo.dependencies || {}),
];
const browserExternalDependencies = Object.keys(baseExternalDependencies);
const baseConfig = {
input: 'src/index.ts',
plugins: [
NodeResolve({
module: true,
jsnext: true,
main: true,
modulesOnly: true,
}),
replace({
clsprefix: packageInfo.name,
}),
typescript({
cacheRoot: '.typescript-compile-cache',
clean: isProd,
useTsconfigDeclarationDir: isProd,
tsconfigOverride: Object.assign(
{},
isProd
? {
compilerOptions: {
declaration: true,
declarationDir: './types',
},
}
: {
compilerOptions: { declaration: true },
},
/**
* only exclude test code in rollup config,
* rather than exclude them in tsconfig.json,
* because when run tests,
* they still need the compiler info in tsconfig.json
*/
{
exclude: ['node_modules', 'src/__tests__/**'],
},
),
}),
postcss({
extensions: ['.css', '.styl', '.stylus'],
plugins: [autoprefixer],
extract: isProd
? path.resolve(__dirname, './styles.css')
: path.resolve(__dirname, './examples/src/lib/styles.css'),
}),
],
};
const devConfig = Object.assign({}, baseConfig, {
external: modularExternalDependencies,
output: {
file: 'examples/src/lib/index.js',
format: 'es',
sourcemap: 'inline',
},
});
const commonjsConfig = Object.assign({}, baseConfig, {
external: modularExternalDependencies,
output: {
file: packageInfo.main,
format: 'cjs',
},
});
const esConfig = Object.assign({}, baseConfig, {
external: modularExternalDependencies,
output: {
file: packageInfo.module,
format: 'es',
},
});
const browserConfig = Object.assign({}, baseConfig, {
external: browserExternalDependencies,
output: {
name: camelCase(packageInfo.name, { pascalCase: true }),
file: packageInfo.unpkg,
format: 'iife',
globals: baseExternalDependencies,
},
plugins: [
...baseConfig.plugins,
replace({
'process.env.NODE_ENV': JSON.stringify('production'),
}),
closure(),
],
});
const config = isProd ? [commonjsConfig, esConfig, browserConfig] : devConfig;
export default config;