-
Notifications
You must be signed in to change notification settings - Fork 6
/
rollup.config.js
135 lines (123 loc) · 3.1 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
import { readdirSync, statSync } from 'fs'
import babel from 'rollup-plugin-babel'
import commonjs from 'rollup-plugin-commonjs'
import del from 'rollup-plugin-delete'
import resolve from 'rollup-plugin-node-resolve'
import replace from 'rollup-plugin-replace'
import { terser } from 'rollup-plugin-terser'
// Sets some constants
const EXTENSIONS = ['.js', '.json']
const CODES = [
'THIS_IS_UNDEFINED',
'MISSING_GLOBAL_NAME',
'CIRCULAR_DEPENDENCY',
]
const excludePaths = [
'.stories.js', // Storybook stories
'atoms/global', // global classes configuration
'atoms/icons/library', // Icons in library
'helpers/renderTimes', // not used
]
function shouldIncludePath(path) {
for (const excludePath of excludePaths) {
if (path.includes(excludePath)) return false
}
return true
}
/**
* Returns files and subfolder as an array from a starting folder
*
* @param {String} dir Starting directory/folder
*/
const walkFolder = dir => {
var results = []
var list = readdirSync(dir)
list.forEach(function (file) {
file = `${dir}/${file}`
var stat = statSync(file)
if (stat && stat.isDirectory()) {
// Recurse into a subdirectory
results = results.concat(walkFolder(file))
} else {
// Is a file
results.push(file)
}
})
return results
}
/**
* Returns an object with chunks information (name and source)
*
* Output example:
* { 'atoms/buttons/Button': 'src/atoms/buttons/Button.js', ... }
*
* @param {String} URI Starting directory/folder
*/
const getChunks = URI =>
walkFolder(URI)
.filter(path => path.includes('.js') && shouldIncludePath(path))
.reduce(
(acc, current) => ({
...acc,
[current.replace('.js', '').replace(`${URI}/`, '')]: `${current}`,
}),
{}
)
/**
* Discards some warnings (defined in CODES)
*
* @param {Object} warning
*/
const discardWarning = warning => {
if (CODES.includes(warning.code)) {
return
}
console.error(warning) // eslint-disable-line no-console
}
// Actual Rollup configuration
export default [
// CJS and ESM
{
onwarn: discardWarning,
input: getChunks('src'),
output: [
{
dir: 'dist/esm',
format: 'esm',
sourcemap: true,
},
{
dir: 'dist/cjs',
format: 'cjs',
exports: 'named',
sourcemap: true,
plugins: [terser()],
},
],
plugins: [
del({ targets: ['dist/cjs', 'dist/esm'] }),
babel({
babelrc: false,
presets: [
['@babel/preset-env', { modules: false }],
'@babel/preset-react',
],
extensions: EXTENSIONS,
exclude: 'node_modules/**',
plugins: [
// ['transform-react-remove-prop-types', { removeImport: true }],
],
}),
commonjs({ include: /node_modules/ }),
replace({ 'process.env.NODE_ENV': JSON.stringify('production') }),
resolve({
extensions: EXTENSIONS,
preferBuiltins: false,
customResolveOptions: {
moduleDirectory: 'node_modules',
},
}),
],
external: ['react', 'react-dom', 'prop-types'],
},
]