Skip to content

Commit ccca4b3

Browse files
authoredOct 18, 2021
fix(gatsby): only remove unused code when apis got removed (#33527)
1 parent 8dbf550 commit ccca4b3

File tree

11 files changed

+99
-7
lines changed

11 files changed

+99
-7
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { fetch, Response } from 'node-fetch';
2+
3+
const usedReference = 'my cool ref';
4+
const unusedReference = 'I hope to be removed';
5+
6+
export default function () {
7+
const x = new Response({})
8+
anotherSelfReferencedOne();
9+
10+
return usedReference
11+
}
12+
13+
// such a structure is being generated by regenerator-runtime
14+
function renderPage() {
15+
renderPage = () => { };
16+
}
17+
18+
function anotherSelfReferencedOne() {
19+
anotherSelfReferencedOne = () => { };
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { fetch, Response } from 'node-fetch';
2+
const usedReference = 'my cool ref';
3+
const unusedReference = 'I hope to be removed';
4+
export default function () {
5+
const x = new Response({});
6+
anotherSelfReferencedOne();
7+
return usedReference;
8+
} // such a structure is being generated by regenerator-runtime
9+
10+
function renderPage() {
11+
renderPage = () => {};
12+
}
13+
14+
function anotherSelfReferencedOne() {
15+
anotherSelfReferencedOne = () => {};
16+
}

‎packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/exports/input.mjs

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import * as fs from "fs"
2-
31
const { fetch, Response } = require('node-fetch');
4-
const unusedReference = fs.readFileSync('./myfile.json');
52
const usedReference = 'used reference';
63

74
module.exports = function () {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/** @jsx jsx */
2+
import { jsx } from 'react'
3+
4+
export default function MyComponent() {
5+
return <div>Hello World</div>
6+
}
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"presets": [
3+
"@babel/preset-react"
4+
],
5+
"plugins": [
6+
[
7+
"../../../../babel-plugin-remove-api",
8+
{
9+
"apis": [
10+
"getServerData",
11+
"config"
12+
]
13+
}
14+
]
15+
]
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @jsx jsx */
2+
import { jsx } from 'react';
3+
export default function MyComponent() {
4+
return jsx("div", null, "Hello World");
5+
}

‎packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/options.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
}
1111
]
1212
]
13-
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
3+
export default function MyComponent() {
4+
return <div>Hello World</div>
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"presets": [
3+
"@babel/preset-react"
4+
],
5+
"plugins": [
6+
[
7+
"../../../../babel-plugin-remove-api",
8+
{
9+
"apis": [
10+
"getServerData",
11+
"config"
12+
]
13+
}
14+
]
15+
]
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import React from 'react';
2+
export default function MyComponent() {
3+
return /*#__PURE__*/React.createElement("div", null, "Hello World");
4+
}

‎packages/gatsby/src/utils/babel/babel-plugin-remove-api.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ export default declare(function removeApiCalls(
2020
name: `remove-api`,
2121
visitor: {
2222
Program: {
23-
exit(path): void {
23+
exit(path, state): void {
24+
if (!state.apiRemoved) {
25+
return
26+
}
27+
2428
// babel doesn't remove references very well so we loop until nothing gets removed
2529
let removed = false
2630

@@ -90,7 +94,7 @@ export default declare(function removeApiCalls(
9094
},
9195

9296
// Remove export statements
93-
ExportNamedDeclaration(path): void {
97+
ExportNamedDeclaration(path, state): void {
9498
const declaration = path.node.declaration
9599

96100
if (t.isExportNamedDeclaration(path.node)) {
@@ -126,12 +130,13 @@ export default declare(function removeApiCalls(
126130
}
127131

128132
if (apiToCheck && apisToRemove.includes(apiToCheck)) {
133+
state.apiRemoved = true
129134
path.remove()
130135
}
131136
},
132137

133138
// remove exports
134-
ExpressionStatement(path): void {
139+
ExpressionStatement(path, state): void {
135140
if (
136141
!t.isAssignmentExpression(path.node.expression) ||
137142
!t.isMemberExpression(path.node.expression.left) ||
@@ -143,6 +148,7 @@ export default declare(function removeApiCalls(
143148
const apiToCheck = (path.node.expression.left.property as t.Identifier)
144149
.name
145150
if (apiToCheck && apisToRemove.includes(apiToCheck)) {
151+
state.apiRemoved = true
146152
path.remove()
147153
}
148154
},

0 commit comments

Comments
 (0)
Please sign in to comment.