Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby): only remove unused code when apis got removed #33527

Merged
merged 1 commit into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 = () => { };
}
Original file line number Diff line number Diff line change
@@ -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 = () => {};
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @jsx jsx */
import { jsx } from 'react'

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

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

export default function MyComponent() {
return <div>Hello World</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"presets": [
"@babel/preset-react"
],
"plugins": [
[
"../../../../babel-plugin-remove-api",
{
"apis": [
"getServerData",
"config"
]
}
]
]
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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