From b95bf22bafd3be6218ed31bafede3c657cae459d Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Thu, 14 Oct 2021 12:13:37 +0200 Subject: [PATCH] fix(gatsby): only remove unused code when apis got removed --- .../fixtures/remove-apis/default/input.mjs | 20 +++++++++++++++++++ .../fixtures/remove-apis/default/output.mjs | 16 +++++++++++++++ .../fixtures/remove-apis/exports/input.mjs | 3 --- .../fixtures/remove-apis/jsx/input.mjs | 7 +++++++ .../fixtures/remove-apis/jsx/options.json | 16 +++++++++++++++ .../fixtures/remove-apis/jsx/output.mjs | 5 +++++ .../fixtures/remove-apis/options.json | 2 +- .../fixtures/remove-apis/react/input.mjs | 5 +++++ .../fixtures/remove-apis/react/options.json | 16 +++++++++++++++ .../fixtures/remove-apis/react/output.mjs | 4 ++++ .../utils/babel/babel-plugin-remove-api.ts | 12 ++++++++--- 11 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/default/input.mjs create mode 100644 packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/default/output.mjs create mode 100644 packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/jsx/input.mjs create mode 100644 packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/jsx/options.json create mode 100644 packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/jsx/output.mjs create mode 100644 packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/react/input.mjs create mode 100644 packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/react/options.json create mode 100644 packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/react/output.mjs diff --git a/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/default/input.mjs b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/default/input.mjs new file mode 100644 index 0000000000000..ca5fa2d64f5fe --- /dev/null +++ b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/default/input.mjs @@ -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 = () => { }; +} diff --git a/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/default/output.mjs b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/default/output.mjs new file mode 100644 index 0000000000000..81daac568e205 --- /dev/null +++ b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/default/output.mjs @@ -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 = () => {}; +} diff --git a/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/exports/input.mjs b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/exports/input.mjs index 5a152ad2c14db..042c2d898ca65 100644 --- a/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/exports/input.mjs +++ b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/exports/input.mjs @@ -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 () { diff --git a/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/jsx/input.mjs b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/jsx/input.mjs new file mode 100644 index 0000000000000..91d90f48be297 --- /dev/null +++ b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/jsx/input.mjs @@ -0,0 +1,7 @@ +/** @jsx jsx */ +import { jsx } from 'react' + +export default function MyComponent() { + return
Hello World
+} + diff --git a/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/jsx/options.json b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/jsx/options.json new file mode 100644 index 0000000000000..f43006233c230 --- /dev/null +++ b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/jsx/options.json @@ -0,0 +1,16 @@ +{ + "presets": [ + "@babel/preset-react" + ], + "plugins": [ + [ + "../../../../babel-plugin-remove-api", + { + "apis": [ + "getServerData", + "config" + ] + } + ] + ] +} diff --git a/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/jsx/output.mjs b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/jsx/output.mjs new file mode 100644 index 0000000000000..09fda6e861253 --- /dev/null +++ b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/jsx/output.mjs @@ -0,0 +1,5 @@ +/** @jsx jsx */ +import { jsx } from 'react'; +export default function MyComponent() { + return jsx("div", null, "Hello World"); +} diff --git a/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/options.json b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/options.json index bcbf672900118..e809d5e2160d7 100644 --- a/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/options.json +++ b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/options.json @@ -10,4 +10,4 @@ } ] ] -} \ No newline at end of file +} diff --git a/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/react/input.mjs b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/react/input.mjs new file mode 100644 index 0000000000000..fc4701ec9bf49 --- /dev/null +++ b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/react/input.mjs @@ -0,0 +1,5 @@ +import React from 'react'; + +export default function MyComponent() { + return
Hello World
+} diff --git a/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/react/options.json b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/react/options.json new file mode 100644 index 0000000000000..f43006233c230 --- /dev/null +++ b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/react/options.json @@ -0,0 +1,16 @@ +{ + "presets": [ + "@babel/preset-react" + ], + "plugins": [ + [ + "../../../../babel-plugin-remove-api", + { + "apis": [ + "getServerData", + "config" + ] + } + ] + ] +} diff --git a/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/react/output.mjs b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/react/output.mjs new file mode 100644 index 0000000000000..6df32d3f04f55 --- /dev/null +++ b/packages/gatsby/src/utils/babel/__tests__/fixtures/remove-apis/react/output.mjs @@ -0,0 +1,4 @@ +import React from 'react'; +export default function MyComponent() { + return /*#__PURE__*/React.createElement("div", null, "Hello World"); +} diff --git a/packages/gatsby/src/utils/babel/babel-plugin-remove-api.ts b/packages/gatsby/src/utils/babel/babel-plugin-remove-api.ts index d1b8081221fc4..03138f20309d9 100644 --- a/packages/gatsby/src/utils/babel/babel-plugin-remove-api.ts +++ b/packages/gatsby/src/utils/babel/babel-plugin-remove-api.ts @@ -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 @@ -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)) { @@ -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) || @@ -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() } },