generated from hejny/spacetrim
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrollup.config.js
153 lines (134 loc) · 5.66 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
import jsonPlugin from '@rollup/plugin-json';
import typescriptPlugin from '@rollup/plugin-typescript';
import { readdirSync } from 'fs';
import { join } from 'path';
import polyfillNode from 'rollup-plugin-polyfill-node';
import { visualizer } from "rollup-plugin-visualizer";
// Note: Note using raw imports via `rollup-plugin-raw` - it is not maintained and has security and compatibility issues
export default function () {
return getPackagesMetadataForRollup()
.filter(({ isBuilded }) => isBuilded)
.map(({ packageBasename, entryIndexFilePath }) => {
const output = [
{
file: `./packages/${packageBasename}/esm/index.es.js`,
format: 'es',
sourcemap: true,
},
{
file: `./packages/${packageBasename}/umd/index.umd.js`,
name: `promptbook-${packageBasename}`,
format: 'umd',
sourcemap: true,
},
];
const plugins = [
typescriptPlugin({
tsconfig: './tsconfig.json',
// <- Note: This is essential propper type declaration generation
}),
jsonPlugin({
preferConst: true,
compact: true,
}),
];
const packageFullname = `@promptbook/${packageBasename}`;
if (
// TODO: [💚] DRY
packageFullname !== '@promptbook/node' &&
packageFullname !== '@promptbook/cli' &&
packageFullname !== '@promptbook/documents' &&
packageFullname !== '@promptbook/legacy-documents' &&
packageFullname !== '@promptbook/website-crawler'
) {
plugins.push(polyfillNode);
} else {
/*
TODO: [🧠] Maybe node-only packages should use something different than `umd`
@see https://rollupjs.org/configuration-options/#output-format
> output.push({
> file: `./packages/${packageBasename}/umd/index.cjs.js`, // <- Do not forget to change link to this file across the project
> name: `promptbook-${packageBasename}`,
> format: 'cjs',
> sourcemap: true,
> });
*/
}
plugins.push(visualizer({
emitFile: true,
filename: "stats.html", // <- TODO: [🧠] Pick better filename for this
}));
return {
input: entryIndexFilePath,
output,
plugins,
};
});
}
/**
* Gets metadata of all packages of Promptbook ecosystem
*
* There are 2 simmilar functions:
* - `getPackagesMetadata` Async version with declared types and extended information, use this in scripts
* - `getPackagesMetadataForRollup` - Sync version with less information, use this ONLY in rollup config
*/
export function getPackagesMetadataForRollup() {
const packagesMetadata = [];
const dirents = readdirSync(join(__dirname, 'src/_packages'), { recursive: false, withFileTypes: true });
// <- Note: In production it is not good practice to use synchronous functions
// But this is only tooling code and it is not a problem
// Unfortunately, there is no way to use async configuration in rollup.config.js
for (const dirent of dirents) {
if (!dirent.isFile()) {
continue;
}
if (!dirent.name.endsWith('.index.ts')) {
continue;
}
const packageBasename = dirent.name.split('.').shift();
if (!packageBasename) {
throw new Error('Invalid package name');
}
packagesMetadata.push({
entryIndexFilePath: `./src/_packages/${packageBasename}.index.ts`,
readmeFilePath: `./src/_packages/${packageBasename}.readme.md`,
isBuilded: true,
packageScope: 'promptbook',
packageBasename,
packageFullname: `@promptbook/${packageBasename}`,
additionalDependencies: [],
});
}
packagesMetadata.push({
readmeFilePath: `./src/_packages/promptbook.readme.md`,
entryIndexFilePath: null,
isBuilded: false,
packageScope: null,
packageBasename: 'promptbook',
packageFullname: 'promptbook',
additionalDependencies: packagesMetadata.map(({ packageFullname }) => packageFullname) /* <- Note: [🧃] */,
});
packagesMetadata.push({
readmeFilePath: `./src/_packages/ptbk.readme.md`,
entryIndexFilePath: null,
isBuilded: false,
packageScope: null,
packageBasename: 'ptbk',
packageFullname: 'ptbk',
additionalDependencies: ['promptbook' /* <- Note: [🧃] */],
});
/*/
// <- Note: Keep for testing single package
// Run:> npx rollup --config rollup.config.js
// or
// > node --max-old-space-size=8000 ./node_modules/rollup/dist/bin/rollup --config rollup.config.js
return packagesMetadata.filter(({ packageFullname }) => packageFullname === '@promptbook/website-crawler');
/**/
/**/
return packagesMetadata;
/**/
}
/**
* Note: [🧃] Packages `@promptbook/cli` and `@promptbook/types` are marked as dependencies (not devDependencies) to ensure that they are always installed
* TODO: Maybe make `PackageMetadata` as discriminated union - isBuilded+entryIndexFilePath
*/