-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
91 lines (82 loc) · 2.58 KB
/
index.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
// SSG related things that should be deleted
// when in SSR mode
const SSG_EXPORTS_TO_DELETE = [
"getStaticProps",
"getStaticPaths",
"unstable_getStaticProps",
"unstable_getStaticPaths",
];
// SSR related things that should be deleted
// when in SSG mode
const SSR_EXPORTS_TO_DELETE = [
"getServerSideProps",
"unstable_getServerProps",
"unstable_getServerSideProps",
];
const shouldExportBeDeleted = (mode, name) => {
const isSsr = mode === "ssr";
const toDelete = isSsr ? SSG_EXPORTS_TO_DELETE : SSR_EXPORTS_TO_DELETE;
return toDelete.indexOf(name) > -1;
};
// This is adapted from here: https://github.com/vercel/next.js/blob/canary/packages/next/build/babel/plugins/next-ssg-transform.ts#L253
module.exports = (mode) => {
return {
visitor: {
Program(programPath) {
programPath.traverse({
ExportNamedDeclaration(exportNamedPath) {
const specifiers = exportNamedPath.get("specifiers");
if (specifiers.length) {
specifiers.forEach((s) => {
if (
shouldExportBeDeleted(
mode,
t.isIdentifier(s.node.exported)
? s.node.exported.name
: s.node.exported.value,
exportNamedState
)
) {
s.remove();
}
});
if (exportNamedPath.node.specifiers.length < 1) {
exportNamedPath.remove(); // bye
}
return;
}
const decl = exportNamedPath.get("declaration");
if (decl == null || decl.node == null) {
return;
}
switch (decl.node.type) {
case "FunctionDeclaration": {
const name = decl.node.id.name;
if (shouldExportBeDeleted(mode, name)) {
exportNamedPath.remove(); // l8ter
}
break;
}
case "VariableDeclaration": {
const inner = decl.get("declarations");
inner.forEach((d) => {
if (d.node.id.type !== "Identifier") {
return;
}
const name = d.node.id.name;
if (shouldExportBeDeleted(mode, name)) {
exportNamedPath.remove(); // nice seeing you
}
});
break;
}
default: {
break;
}
}
},
});
},
},
};
};