forked from GetStream/stream-chat-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
199 lines (191 loc) · 4.91 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import babel from '@rollup/plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import external from 'rollup-plugin-peer-deps-external';
import scss from 'rollup-plugin-scss';
import url from 'rollup-plugin-url';
import copy from 'rollup-plugin-copy-glob';
import globals from 'rollup-plugin-node-globals';
import visualizer from 'rollup-plugin-visualizer';
import replace from '@rollup/plugin-replace';
import builtins from '@stream-io/rollup-plugin-node-builtins';
import { terser } from 'rollup-plugin-terser';
import { prepend } from 'rollup-plugin-insert';
import PropTypes from 'prop-types';
import process from 'process';
import pkg from './package.json';
process.env.NODE_ENV = 'production';
const styleBundle = ({ min } = { min: false }) => ({
cache: false,
input: 'src/styles/index.scss',
output: [
{
dir: 'dist/css',
format: 'es',
},
],
plugins: [
scss({
output: min ? pkg.style.replace('.css', '.min.css') : pkg.style,
outputStyle: min ? 'compressed' : 'nested',
prefix: `@import "./variables.scss";`,
}),
],
watch: {
chokidar: false,
include: 'src/styles/',
},
});
const baseConfig = {
cache: false,
inlineDynamicImports: true,
input: 'src/index.ts',
watch: {
chokidar: false,
},
};
const externalDependencies = [
/@babel/,
'@braintree/sanitize-url',
'@fortawesome/free-regular-svg-icons',
'@fortawesome/react-fontawesome',
'custom-event',
/dayjs/,
/emoji-mart/,
'emoji-regex',
'i18next',
'isomorphic-ws',
'linkifyjs',
'lodash.debounce',
'lodash.isequal',
'lodash.throttle',
'lodash.uniqby',
'mdast-util-find-and-replace',
'mml-react',
'pretty-bytes',
'prop-types',
'react-fast-compare',
/react-file-utils/,
'react-images',
'react-is',
/react-markdown/,
'react-player',
'react-textarea-autosize',
'react-virtuoso',
'textarea-caret',
/uuid/,
];
const basePlugins = [
replace({
preventAssignment: true,
'process.env.NODE_ENV': JSON.stringify('production'),
}),
// Replace our alias for a relative path so the jsdoc resolution still
// works after bundling.
replace({
delimiters: ['', ''],
"import('types')": "import('../types')",
preventAssignment: true,
}),
// Remove peer-dependencies from final bundle
external(),
typescript(),
babel({
babelHelpers: 'runtime',
exclude: 'node_modules/**',
}),
commonjs({
namedExports: {
'node_modules/linkifyjs/index.js': ['find'],
'node_modules/react-is/index.js': ['isValidElementType'],
'prop-types': Object.keys(PropTypes),
},
}),
// import files as data-uris or es modules
url(),
copy(
[
{ dest: 'dist/scss', files: 'src/styles/**/*' },
{ dest: 'dist/assets', files: 'src/assets/*' },
{ dest: 'dist/i18n', files: 'src/i18n/*.json' },
],
{
verbose: process.env.VERBOSE,
watch: process.env.ROLLUP_WATCH,
},
),
// Json to ES modules conversion
json({ compact: true }),
process.env.BUNDLE_SIZE ? visualizer() : null,
];
const normalBundle = {
...baseConfig,
external: externalDependencies,
output: [
{
file: pkg.main,
format: 'cjs',
sourcemap: true,
},
{
file: pkg.module,
format: 'es',
sourcemap: true,
},
],
plugins: [...basePlugins],
};
const fullBrowserBundle = ({ min } = { min: false }) => ({
...baseConfig,
output: [
{
extend: true, // extend window, not overwrite it
file: min ? pkg.jsdelivr : pkg.jsdelivr.replace('.min', ''),
format: 'iife',
globals: {
react: 'React',
'react-dom': 'ReactDOM',
'stream-chat': 'StreamChat',
},
name: 'window', // write all exported values to window
sourcemap: true,
},
],
plugins: [
...basePlugins,
{
load: (id) => (id.match(/.s?css$/) ? '' : null),
name: 'ignore-css-and-scss',
resolveId: (importee) => (importee.match(/.s?css$/) ? importee : null),
},
builtins(),
resolve({
browser: true,
}),
globals({
buffer: false,
dirname: false,
filename: false,
globals: false,
process: true,
}),
// To work with globals rollup expects them to be namespaced, what is not the case with stream-chat.
// This injects some code to define stream-chat globals as expected by rollup.
prepend(
'window.StreamChat.StreamChat=StreamChat;window.StreamChat.logChatPromiseExecution=logChatPromiseExecution;window.StreamChat.Channel=Channel;window.ICAL=window.ICAL||{};',
),
min ? terser() : null,
],
});
export default () =>
process.env.ROLLUP_WATCH
? [styleBundle(), normalBundle]
: [
styleBundle(),
styleBundle({ min: true }),
normalBundle,
fullBrowserBundle({ min: true }),
fullBrowserBundle(),
];