-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathgenerate-mappings.js
64 lines (58 loc) · 1.76 KB
/
generate-mappings.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
const parser = require('@babel/parser');
const types = require('babel-types');
const fs = require('fs');
const path = require('path');
const packageJson = require('../package.json');
const root = path.resolve(__dirname, '..');
const output = path.join(root, 'lib/mappings.json');
const source = fs.readFileSync(path.resolve(root, 'src', 'index.tsx'), 'utf8');
const ast = parser.parse(source, {
sourceType: 'module',
plugins: [
'jsx',
'typescript',
'objectRestSpread',
'classProperties',
'asyncGenerators',
],
});
const index = packageJson.module;
const relative = (value /* : string */) =>
path.relative(root, path.resolve(path.dirname(index), value));
const mappings = ast.program.body.reduce((acc, declaration, index, self) => {
if (types.isExportNamedDeclaration(declaration)) {
if (declaration.source) {
declaration.specifiers.forEach((specifier) => {
acc[specifier.exported.name] = {
path: relative(declaration.source.value),
name: specifier.local.name,
};
});
} else {
declaration.specifiers.forEach((specifier) => {
const name = specifier.exported.name;
self.forEach((it) => {
if (
types.isImportDeclaration(it) &&
it.specifiers.some(
(s) =>
types.isImportNamespaceSpecifier(s) &&
s.local.name === specifier.local.name
)
) {
acc[name] = {
path: relative(it.source.value),
name: '*',
};
}
});
});
}
}
return acc;
}, {});
fs.existsSync(path.dirname(output)) || fs.mkdirSync(path.dirname(output));
fs.writeFileSync(
output,
JSON.stringify({ name: packageJson.name, index, mappings }, null, 2)
);