forked from primefaces/primereact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
238 lines (203 loc) · 7.9 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { babel } from '@rollup/plugin-babel';
import replace from '@rollup/plugin-replace';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import postcss from 'rollup-plugin-postcss';
import alias from '@rollup/plugin-alias';
import { terser } from 'rollup-plugin-terser';
import fs from 'fs-extra';
import path from 'path';
let entries = [];
let core = {};
// alias entries
const ALIAS_COMPONENT_ENTRIES = [
{ find: '../utils/Utils', replacement: 'primereact/utils' },
{ find: '../api/Api', replacement: 'primereact/api' },
{ find: '../hooks/Hooks', replacement: 'primereact/hooks' },
{ find: '../ripple/Ripple', replacement: 'primereact/ripple' },
{ find: '../csstransition/CSSTransition', replacement: 'primereact/csstransition' },
{ find: '../portal/Portal', replacement: 'primereact/portal' },
{ find: '../keyfilter/KeyFilter', replacement: 'primereact/keyfilter' },
{ find: '../tooltip/Tooltip', replacement: 'primereact/tooltip' },
{ find: '../virtualscroller/VirtualScroller', replacement: 'primereact/virtualscroller' },
{ find: '../terminalservice/TerminalService', replacement: 'primereact/terminalservice' },
{ find: '../overlayservice/OverlayService', replacement: 'primereact/overlayservice' },
{ find: '../checkox/Checkbox', replacement: 'primereact/checkbox' },
{ find: '../button/Button', replacement: 'primereact/button' },
{ find: '../inputtext/InputText', replacement: 'primereact/inputtext' },
{ find: '../inputnumber/InputNumber', replacement: 'primereact/inputnumber' },
{ find: '../messages/Messages', replacement: 'primereact/messages' },
{ find: '../progressbar/ProgressBar', replacement: 'primereact/progressbar' },
{ find: '../dropdown/Dropdown', replacement: 'primereact/dropdown' },
{ find: '../dialog/Dialog', replacement: 'primereact/dialog' },
{ find: '../paginator/Paginator', replacement: 'primereact/paginator' },
{ find: '../tree/Tree', replacement: 'primereact/tree' }
];
// dependencies
const GLOBAL_DEPENDENCIES = {
'react': 'React',
'react-dom': 'ReactDOM',
'react-transition-group': 'ReactTransitionGroup'
};
const GLOBAL_COMPONENT_DEPENDENCIES = {
...GLOBAL_DEPENDENCIES, ...(ALIAS_COMPONENT_ENTRIES.reduce((acc, cur) => ({ ...acc, [cur.replacement]: cur.replacement.replace('\/', '.') }), {}))
};
// externals
const EXTERNAL = ['react', 'react-dom', 'react-transition-group', '@babel/runtime', '@fullcalendar/core', 'chart.js/auto', 'quill'];
const EXTERNAL_COMPONENT = [...EXTERNAL, ...(ALIAS_COMPONENT_ENTRIES.map(entries => entries.replacement))];
// plugins
const BABEL_PLUGIN_OPTIONS = {
exclude: 'node_modules/**',
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: ['@babel/plugin-transform-runtime', '@babel/plugin-proposal-class-properties'],
skipPreflightCheck: true,
babelHelpers: 'runtime',
babelrc: false
};
const ALIAS_PLUGIN_OPTIONS_FOR_COMPONENT = {
entries: ALIAS_COMPONENT_ENTRIES
};
const REPLACE_PLUGIN_OPTIONS = {
'process.env.NODE_ENV': JSON.stringify('production'),
preventAssignment: true
};
const RESOLVE_PLUGIN_OPTIONS = {
extensions: ['.js']
};
const COMMONJS_PLUGIN_OPTIONS = {
exclude: process.env.INPUT_DIR + '**',
sourceMap: false
};
const POSTCSS_PLUGIN_OPTIONS = {
sourceMap: false
};
const TERSER_PLUGIN_OPTIONS = {
compress: {
keep_infinity: true,
pure_getters: true,
reduce_funcs: false
}
}
const PLUGINS = [
replace(REPLACE_PLUGIN_OPTIONS),
resolve(RESOLVE_PLUGIN_OPTIONS),
commonjs(COMMONJS_PLUGIN_OPTIONS),
babel(BABEL_PLUGIN_OPTIONS),
postcss(POSTCSS_PLUGIN_OPTIONS)
];
const PLUGINS_COMPONENT = [
alias(ALIAS_PLUGIN_OPTIONS_FOR_COMPONENT),
...PLUGINS
];
function addEntry(name, input, output, isComponent = true) {
const exports = name === 'primereact.api' || name === 'primereact' ? 'named' : 'auto';
const useCorePlugin = ALIAS_COMPONENT_ENTRIES.some(entry => entry.replacement.replace('primereact/', '') === name.replace('primereact.', ''));
const plugins = isComponent ? PLUGINS_COMPONENT : PLUGINS;
const external = isComponent ? EXTERNAL_COMPONENT : EXTERNAL;
const inlineDynamicImports = true;
const getEntry = (isMinify) => {
return {
input,
plugins: [...plugins, isMinify && terser(TERSER_PLUGIN_OPTIONS), useCorePlugin && corePlugin()],
external,
inlineDynamicImports
}
};
const get_CJS_ESM = (isMinify) => {
return {
...getEntry(isMinify),
output: [
{
format: 'cjs',
file: `${output}.cjs${isMinify ? '.min' : ''}.js`,
exports
},
{
format: 'esm',
file: `${output}.esm${isMinify ? '.min' : ''}.js`,
exports
}
]
}
};
const get_IIFE = (isMinify) => {
return {
...getEntry(isMinify),
output: [
{
format: 'iife',
name,
file: `${output}${isMinify ? '.min' : ''}.js`,
globals: isComponent ? GLOBAL_COMPONENT_DEPENDENCIES : GLOBAL_DEPENDENCIES,
exports
}
]
}
};
entries.push(get_CJS_ESM());
entries.push(get_IIFE());
// Minify
entries.push(get_CJS_ESM(true));
entries.push(get_IIFE(true));
}
function corePlugin() {
return {
name: 'corePlugin',
generateBundle(outputOptions, bundle) {
if (outputOptions.format === 'iife') {
Object.keys(bundle).forEach(id => {
const chunk = bundle[id];
const name = id.replace('.min.js', '').replace('.js', '');
const filePath = `./dist/core/core${id.indexOf('.min.js') > 0 ? '.min.js': '.js'}`;
core[filePath] ? (core[filePath][name] = chunk.code) : (core[filePath] = { [`${name}`]: chunk.code });
});
}
}
};
}
function addCore() {
const lastEntry = entries[entries.length - 1];
lastEntry.plugins = [
...lastEntry.plugins,
{
name: 'coreMergePlugin',
generateBundle() {
Object.entries(core).forEach(([filePath, value]) => {
const code = ALIAS_COMPONENT_ENTRIES.reduce((val, entry) => {
const name = entry.replacement.replace('primereact/', '');
val += value[name] + '\n';
return val;
}, '');
fs.outputFile(path.resolve(__dirname, filePath), code, {}, function(err) {
if (err) {
return console.error(err);
}
});
});
}
}
]
}
function addComponent() {
fs.readdirSync(path.resolve(__dirname, process.env.INPUT_DIR), { withFileTypes: true })
.filter(dir => dir.isDirectory())
.forEach(({ name: folderName }) => {
fs.readdirSync(path.resolve(__dirname, process.env.INPUT_DIR + folderName)).forEach(file => {
let name = file.split(/(.js)$/)[0].toLowerCase();
if (name === folderName) {
const input = process.env.INPUT_DIR + folderName + '/' + file;
const output = process.env.OUTPUT_DIR + folderName + '/' + name;
addEntry('primereact.' + folderName, input, output, true);
}
});
});
}
function addPrimeReact() {
const input = process.env.INPUT_DIR + 'primereact.all.js';
const output = process.env.OUTPUT_DIR + 'primereact.all';
addEntry('primereact', input, output, false);
}
addComponent();
addPrimeReact();
addCore();
export default entries;