-
Notifications
You must be signed in to change notification settings - Fork 4
/
rollup.config.js
122 lines (107 loc) · 3.71 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
import path from 'path';
import fs from 'fs';
import tslib from 'tslib';
import ts from 'typescript';
import glob from 'glob';
import {
topologicallySort,
listWorkspaces,
} from '@lidofinance/yarn-workspaces-list';
import typescript from 'rollup-plugin-typescript2';
import del from 'rollup-plugin-delete';
import resolve from '@rollup/plugin-node-resolve';
const excludedWorkspaces = ['.'];
const extensions = ['.ts', '.d.ts'];
const commonExternal = [];
const errors = {
MIGRATION_FILENAME_WRONG: (filePath, filenameRegExp) =>
`Migration file name '${filePath}' does not comply with the pattern '${filenameRegExp}'`,
MIGRATION_FILE_CLASSNAME_WRONG: (filePath, classname) =>
`Migration file '${filePath}' does not export class with correct name. Class name should be equal to file name. ` +
`Expected class name: '${classname}'.`,
};
export default async () => {
const packages = await listWorkspaces();
const filteredPackages = packages.filter(
({ location }) => !excludedWorkspaces.includes(location),
);
const sortedPackages = topologicallySort(filteredPackages);
const checkMigrationFile = (filePath) => {
const filenameRegExp = /Migration[0-9]{14}\.ts$/; // PLEASE DO NOT CHANGE THAT
const ext = path.extname(filePath);
const filename = path.basename(filePath, ext);
const contentRegExp = new RegExp(
`^export\\s*class\\s*${filename}\\s*extends\\s*Migration`,
'm',
); // PLEASE DO NOT CHANGE THAT
if (filePath.match(filenameRegExp) === null) {
throw new Error(
errors.MIGRATION_FILENAME_WRONG(filePath, filenameRegExp),
);
}
const content = fs.readFileSync(filePath, 'utf-8');
if (content.match(contentRegExp) === null) {
throw new Error(
errors.MIGRATION_FILE_CLASSNAME_WRONG(filePath, filename),
);
}
};
const getMigrationPaths = (packageDir) => {
const migrationsPath = path.join(__dirname, packageDir, 'src/migrations');
const migrationFileGlobPattern = 'Migration*.ts';
const migrationFilePaths = glob
.sync(migrationFileGlobPattern, {
cwd: migrationsPath,
})
.map((_) => path.join(migrationsPath, _));
migrationFilePaths.forEach((filePath) => {
checkMigrationFile(filePath);
});
return migrationFilePaths;
};
const config = sortedPackages.map((packageData) => {
const packageDir = packageData.location;
const packageJson = JSON.parse(
fs.readFileSync(path.join(packageDir, 'package.json'), 'utf-8'),
);
const { dependencies, peerDependencies } = packageJson;
const external = [
...commonExternal,
...Object.keys({ ...dependencies, ...peerDependencies }),
];
const cjsDir = path.join(packageDir, path.dirname(packageJson.main));
const entrypoint = path.join(packageDir, 'src/index');
const migrationFilePaths = getMigrationPaths(packageDir);
const inputs = [entrypoint, ...migrationFilePaths];
return {
input: inputs,
output: [
{
dir: cjsDir,
preserveModulesRoot: path.join(packageDir, 'src'),
preserveModules: true,
format: 'cjs',
exports: 'named',
},
],
plugins: [
del({ targets: path.join(packageDir, 'dist/*'), runOnce: true }),
resolve({ extensions }),
typescript({
tslib,
typescript: ts,
tsconfig: path.join(packageDir, 'tsconfig.json'),
tsconfigOverride: {
compilerOptions: {
paths: { tslib: [require.resolve('tslib/tslib.d.ts')] },
},
exclude: ['node_modules', 'dist', '**/*.spec.ts'],
include: ['src/**/*'],
},
}),
],
external,
};
});
return config;
};