-
Notifications
You must be signed in to change notification settings - Fork 126
/
strip-bad-reexports.js
53 lines (49 loc) · 1.44 KB
/
strip-bad-reexports.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
/* eslint-env node */
const resolve = require('resolve');
const { transform } = require('@babel/core');
const Funnel = require('broccoli-funnel');
const { writeFileSync, readFileSync, existsSync, unlinkSync } = require('fs');
const { join } = require('path');
module.exports = class StripBadReexports extends Funnel {
constructor(tree, files) {
super(tree);
this.filesToStrip = files;
}
build() {
super.build();
for (let file of this.filesToStrip) {
let srcFile = join(this.inputPaths[0], file);
if (!existsSync(srcFile)) {
continue;
}
let src = readFileSync(srcFile, 'utf8');
let plugins = [[stripBadReexportsTransform, { resolveBase: this.outputPath }]];
let destFile = join(this.outputPath, file);
unlinkSync(destFile);
writeFileSync(destFile, transform(src, { plugins }).code);
}
}
shouldLinkRoots() {
// We want to force funnel to copy things rather than just linking the whole
// directory, because we're planning to mutate it.
return false;
}
}
function stripBadReexportsTransform() {
return {
visitor: {
ExportNamedDeclaration(path, state) {
if (
path.node.source &&
path.node.source.type === 'StringLiteral'
) {
try {
resolve.sync(path.node.source.value, { basedir: state.opts.resolveBase });
} catch (err) {
path.remove();
}
}
},
},
};
}