-
Notifications
You must be signed in to change notification settings - Fork 612
/
dynamic-module-parser.ts
166 lines (136 loc) · 5.07 KB
/
dynamic-module-parser.ts
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
/* eslint-disable no-console */
import * as fs from 'fs';
import * as path from 'path';
import * as glob from 'glob';
import * as ts from 'typescript';
const defaultCompilerOptions: ts.CompilerOptions = {
target: ts.ScriptTarget.ES2020,
module: ts.ModuleKind.ESNext,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
allowJs: true,
strict: false,
esModuleInterop: true,
skipLibCheck: true,
noEmit: true,
};
/**
* Maps each index module export to a dynamic module request (import specifier).
*/
export type DynamicModuleMap = { [exportName: string]: string };
/**
* Map all exports of the given index module to their corresponding dynamic modules.
*
* Example: `@patternfly/react-core` package provides ESModules index at `dist/esm/index.js`
* which exports Alert component related code & types via `dist/esm/components/Alert/index.js`
* module.
*
* Given the example above, this function should return a mapping like so:
* ```js
* {
* Alert: 'dist/dynamic/components/Alert',
* AlertProps: 'dist/dynamic/components/Alert',
* AlertContext: 'dist/dynamic/components/Alert',
* // ...
* }
* ```
*
* The above mapping can be used when generating import statements like so:
* ```ts
* import { Alert } from '@patternfly/react-core/dist/dynamic/components/Alert';
* ```
*
* It may happen that the same export is provided by multiple dynamic modules;
* in such case, the resolution favors modules with most specific file paths, for example
* `dist/dynamic/components/Wizard/hooks` is favored over `dist/dynamic/components/Wizard`.
*
* Dynamic modules nested under `deprecated` or `next` directories are ignored.
*
* If the referenced index module does not exist, an empty object is returned.
*/
export const getDynamicModuleMap = (
basePath: string,
indexModule = 'dist/esm/index.js',
resolutionField = 'module',
tsCompilerOptions = defaultCompilerOptions,
): DynamicModuleMap => {
if (!path.isAbsolute(basePath)) {
throw new Error('Package base path must be absolute');
}
const indexModulePath = path.resolve(basePath, indexModule);
if (!fs.existsSync(indexModulePath)) {
return {};
}
const dynamicModulePathToPkgDir = glob
.sync(`${basePath}/dist/dynamic/**/package.json`)
.reduce<Record<string, string>>((acc, pkgFile) => {
// eslint-disable-next-line
const pkg = require(pkgFile);
const pkgModule = pkg[resolutionField];
if (!pkgModule) {
throw new Error(`Missing field ${resolutionField} in ${pkgFile}`);
}
const pkgResolvedPath = path.resolve(path.dirname(pkgFile), pkgModule);
const pkgRelativePath = path.dirname(path.relative(basePath, pkgFile));
acc[pkgResolvedPath] = pkgRelativePath;
return acc;
}, {});
const dynamicModulePaths = Object.keys(dynamicModulePathToPkgDir);
const compilerHost = ts.createCompilerHost(tsCompilerOptions);
const program = ts.createProgram(
[indexModulePath, ...dynamicModulePaths],
tsCompilerOptions,
compilerHost,
);
const errorDiagnostics = ts
.getPreEmitDiagnostics(program)
.filter((d) => d.category === ts.DiagnosticCategory.Error);
if (errorDiagnostics.length > 0) {
const { getCanonicalFileName, getCurrentDirectory, getNewLine } = compilerHost;
console.error(
ts.formatDiagnostics(errorDiagnostics, {
getCanonicalFileName,
getCurrentDirectory,
getNewLine,
}),
);
throw new Error(`Detected TypeScript errors while parsing modules at ${basePath}`);
}
const typeChecker = program.getTypeChecker();
const getExportNames = (sourceFile: ts.SourceFile) =>
typeChecker
.getExportsOfModule(typeChecker.getSymbolAtLocation(sourceFile))
.map((symbol) => symbol.getName());
const indexModuleExports = getExportNames(program.getSourceFile(indexModulePath));
const dynamicModuleExports = dynamicModulePaths.reduce<Record<string, string[]>>(
(acc, modulePath) => {
acc[modulePath] = getExportNames(program.getSourceFile(modulePath));
return acc;
},
{},
);
const getMostSpecificModulePath = (modulePaths: string[]) =>
modulePaths.reduce<string>((acc, p) => {
const pathSpecificity = p.split(path.sep).length;
const currSpecificity = acc.split(path.sep).length;
if (pathSpecificity > currSpecificity) {
return p;
}
if (pathSpecificity === currSpecificity) {
return !p.endsWith('index.js') && acc.endsWith('index.js') ? p : acc;
}
return acc;
}, '');
return indexModuleExports.reduce<DynamicModuleMap>((acc, exportName) => {
const foundModulePaths = Object.keys(dynamicModuleExports).filter((modulePath) =>
dynamicModuleExports[modulePath].includes(exportName),
);
const filteredModulePaths = foundModulePaths.filter((modulePath) => {
const dirNames = path.dirname(modulePath).split(path.sep);
return !dirNames.includes('deprecated') && !dirNames.includes('next');
});
if (filteredModulePaths.length > 0) {
acc[exportName] = dynamicModulePathToPkgDir[getMostSpecificModulePath(filteredModulePaths)];
}
return acc;
}, {});
};