-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
285 lines (251 loc) · 8.22 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* eslint-disable filenames/match-exported */
import { promises as fs } from 'node:fs';
import { fileURLToPath } from 'node:url';
import path, { dirname } from 'node:path';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import terser from '@rollup/plugin-terser';
const plugins = [
resolve(),
commonjs(),
json(),
terser({
// Equivalent to:
// $ terser <source> --compress sequences=false --format semicolons=false --output <destination>
compress: {
sequences: false
},
mangle: false,
format: {
semicolons: false,
comments: function (_, comment) {
if (
comment.value.charAt(0) === '!' ||
/cc_on|copyright|license|preserve/i.test(comment.value)
) {
return true;
} else {
return false;
}
// if (comment.type === 'comment2') { // multiline comment
// return /@preserve|@license|@cc_on/i.test(comment.value);
// } else if (comment.type === 'comment1') { // single line comment
// if (comment.value.indexOf('!') === 0) {
// return true;
// } else {
// return /@preserve|@license|@cc_on/i.test(comment.value);
// }
// } else {
// return false;
// }
}
}
})
];
const output = {
exports: 'named',
sourcemap: true
};
/*
// Target output
const rollupConfig = [
{
input: 'src/index.js',
output: { ...output, format: 'cjs', file: 'dist/index.cjs' },
plugins
},
{
input: 'src/index.js',
output: { ...output, format: 'esm', file: 'dist/index.js' },
plugins
},
...
];
*/
// Read the list of files and directories recursively in the "src" directory
// and create a rollup config for each one.
const helpmateDir = path.resolve('./src');
const helpmateDirLength = helpmateDir.length;
const getFiles = async (dir) => {
let dirents = await fs.readdir(dir, { withFileTypes: true });
dirents = dirents.filter((ent) => {
if (
ent.name === '3rdparty' ||
ent.name === '3rdparty_customized' ||
ent.name === '.eslintrc'
) {
return false;
}
return true;
});
const files = await Promise.all(dirents.map((dirent) => {
const res = path.resolve(dir, dirent.name);
return dirent.isDirectory() ? getFiles(res) : res;
}));
return Array.prototype.concat(...files);
};
let files = await getFiles(helpmateDir);
files = (
files
.map((file) => {
return file.slice(helpmateDirLength + 1);
})
.map((file) => {
return '/' + file;
})
);
/*
Sample input:
[
'/array/index.js',
'/array/sortArrayOfObjectsByProperty.js',
'/async/eachOfLimitInOrder.js',
'/async/index.js',
'/fs/index.js',
'/fs/updateFileIfRequired.js',
'/index.js',
'/logger/index.js',
'/logger/noteDown.js',
'/tasks/index.js',
'/tasks/retryNTimesWithDelay.js'
]
Sample output:
[
'/array/sortArrayOfObjectsByProperty.js',
'/array/index.js',
'/async/eachOfLimitInOrder.js',
'/async/index.js',
'/fs/updateFileIfRequired.js',
'/fs/index.js',
'/logger/noteDown.js',
'/logger/index.js',
'/tasks/retryNTimesWithDelay.js',
'/tasks/index.js',
'/index.js'
]
*/
// Sort the files so that the index.js files are last (also applicable for individual sub-folders)
// That means, split by the last occurring "/" character and sort by the first part of the split (if the first part of
// the split is empty, then that entry should come last since it represents the root path), followed by sorting
// the second part while considering the index.js files as the last ones from the second part.
files.sort((a, b) => {
const aSplit = a.split('/').reverse();
const bSplit = b.split('/').reverse();
const aFirstPart = aSplit[1];
const bFirstPart = bSplit[1];
if (aFirstPart === bFirstPart) {
if (aSplit[0] === 'index.js') {
return 1;
} else if (bSplit[0] === 'index.js') {
return -1;
} else {
return aSplit[0] < bSplit[0] ? -1 : 1;
}
} else {
if (aFirstPart === '') {
return 1;
} else if (bFirstPart === '') {
return -1;
} else {
return aFirstPart < bFirstPart ? -1 : 1;
}
}
});
let previousFolder = null,
currentFolder = null;
const filesSeparatedByNullPerDirectory = [];
for (const file of files) {
currentFolder = dirname(file);
if (
previousFolder !== null &&
currentFolder !== previousFolder
) {
filesSeparatedByNullPerDirectory.push(null);
}
filesSeparatedByNullPerDirectory.push(file);
previousFolder = currentFolder;
}
files = filesSeparatedByNullPerDirectory;
// console.log('>>>>>>>>>>>>>>>');
// console.log(files);
// console.log(filesSeparatedByNullPerDirectory);
// console.log('<<<<<<<<<<<<<<<');
// Remove the "/" character from the beginning of each file which was added in one of the previous steps
files = files.map((file) => {
if (file === null) {
return null;
}
return file.slice(1);
});
// Remove the ".js" extension from each file
files = files.map((file) => {
if (file === null) {
return null;
}
return file.slice(0, -3);
});
const listOfImports = [];
const listOfRequires = [];
const listOfFiles = [];
const rollupConfig = files.map((file) => {
if (file === null) {
listOfImports.push('');
listOfRequires.push('');
listOfFiles.push('');
return null;
}
const source = file;
const input = `src/${source}.js`;
let namedExport = source.split('/').pop();
if (namedExport === 'index') {
namedExport = source.split('/').slice(-2)[0];
}
if (namedExport === 'index') {
namedExport = 'helpmateCss';
}
listOfImports.push(`import { ${namedExport} } from 'helpmate-css/dist/${source}.js';`);
listOfRequires.push(`const { ${namedExport} } = require('helpmate-css/dist/${source}.cjs');`);
listOfFiles.push(`src/${source}.js`);
return {
input,
output: [
{ ...output, format: 'cjs', file: `dist/${source}.cjs` },
{ ...output, format: 'esm', file: `dist/${source}.js` }
],
plugins
};
}).filter(x => x);
listOfImports.push(`import { helpmateCss } from 'helpmate-css';`);
listOfRequires.push(`const { helpmateCss } = require('helpmate-css');`);
console.log('List of files being bundled:');
// console.log(
// ' ' +
// files.map((file) => {
// return `src/${file}.js`;
// }).join('\n ')
// );
console.log(' ' + listOfFiles.join('\n '));
console.log('\nExamples of `import` for the modules:');
console.log(' ' + listOfImports.join('\n '));
console.log('\nExamples of `require` for the modules:');
console.log(' ' + listOfRequires.join('\n '));
const __dirname = dirname(fileURLToPath(import.meta.url));
const readmeTemplate = await fs.readFile(
path.resolve(__dirname, 'README.md.template')
);
const readme = (
readmeTemplate
.toString()
.replace('{{listOfImports}}', listOfImports.join('\n'))
.replace('{{listOfRequires}}', listOfRequires.join('\n'))
.replace('{{listOfFiles}}', listOfFiles.join('\n'))
// .replace('{{listOfFiles}}', files.map((file) => {
// return `src/${file}.js`;
// }).join('\n'))
);
await fs.writeFile(path.resolve(__dirname, 'README.md'), readme);
console.log('');
console.log(' ✓ Updated README.md file');
await fs.writeFile(path.resolve(__dirname, 'rollup.generated-config.json'), JSON.stringify(rollupConfig, null, 4));
export default rollupConfig; // eslint-disable-line import/no-default-export