Skip to content

Commit

Permalink
fix(gatsby): only remove unused code when apis got removed (#33527)
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Oct 18, 2021
1 parent 8dbf550 commit ccca4b3
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 7 deletions.
@@ -0,0 +1,20 @@
import { fetch, Response } from 'node-fetch';

const usedReference = 'my cool ref';
const unusedReference = 'I hope to be removed';

export default function () {
const x = new Response({})
anotherSelfReferencedOne();

return usedReference
}

// such a structure is being generated by regenerator-runtime
function renderPage() {
renderPage = () => { };
}

function anotherSelfReferencedOne() {
anotherSelfReferencedOne = () => { };
}
@@ -0,0 +1,16 @@
import { fetch, Response } from 'node-fetch';
const usedReference = 'my cool ref';
const unusedReference = 'I hope to be removed';
export default function () {
const x = new Response({});
anotherSelfReferencedOne();
return usedReference;
} // such a structure is being generated by regenerator-runtime

function renderPage() {
renderPage = () => {};
}

function anotherSelfReferencedOne() {
anotherSelfReferencedOne = () => {};
}
@@ -1,7 +1,4 @@
import * as fs from "fs"

const { fetch, Response } = require('node-fetch');
const unusedReference = fs.readFileSync('./myfile.json');
const usedReference = 'used reference';

module.exports = function () {
Expand Down
@@ -0,0 +1,7 @@
/** @jsx jsx */
import { jsx } from 'react'

export default function MyComponent() {
return <div>Hello World</div>
}

@@ -0,0 +1,16 @@
{
"presets": [
"@babel/preset-react"
],
"plugins": [
[
"../../../../babel-plugin-remove-api",
{
"apis": [
"getServerData",
"config"
]
}
]
]
}
@@ -0,0 +1,5 @@
/** @jsx jsx */
import { jsx } from 'react';
export default function MyComponent() {
return jsx("div", null, "Hello World");
}
Expand Up @@ -10,4 +10,4 @@
}
]
]
}
}
@@ -0,0 +1,5 @@
import React from 'react';

export default function MyComponent() {
return <div>Hello World</div>
}
@@ -0,0 +1,16 @@
{
"presets": [
"@babel/preset-react"
],
"plugins": [
[
"../../../../babel-plugin-remove-api",
{
"apis": [
"getServerData",
"config"
]
}
]
]
}
@@ -0,0 +1,4 @@
import React from 'react';
export default function MyComponent() {
return /*#__PURE__*/React.createElement("div", null, "Hello World");
}
12 changes: 9 additions & 3 deletions packages/gatsby/src/utils/babel/babel-plugin-remove-api.ts
Expand Up @@ -20,7 +20,11 @@ export default declare(function removeApiCalls(
name: `remove-api`,
visitor: {
Program: {
exit(path): void {
exit(path, state): void {
if (!state.apiRemoved) {
return
}

// babel doesn't remove references very well so we loop until nothing gets removed
let removed = false

Expand Down Expand Up @@ -90,7 +94,7 @@ export default declare(function removeApiCalls(
},

// Remove export statements
ExportNamedDeclaration(path): void {
ExportNamedDeclaration(path, state): void {
const declaration = path.node.declaration

if (t.isExportNamedDeclaration(path.node)) {
Expand Down Expand Up @@ -126,12 +130,13 @@ export default declare(function removeApiCalls(
}

if (apiToCheck && apisToRemove.includes(apiToCheck)) {
state.apiRemoved = true
path.remove()
}
},

// remove exports
ExpressionStatement(path): void {
ExpressionStatement(path, state): void {
if (
!t.isAssignmentExpression(path.node.expression) ||
!t.isMemberExpression(path.node.expression.left) ||
Expand All @@ -143,6 +148,7 @@ export default declare(function removeApiCalls(
const apiToCheck = (path.node.expression.left.property as t.Identifier)
.name
if (apiToCheck && apisToRemove.includes(apiToCheck)) {
state.apiRemoved = true
path.remove()
}
},
Expand Down

0 comments on commit ccca4b3

Please sign in to comment.