Skip to content

Commit ccca4b3

Browse files
authored
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
Lines changed: 20 additions & 0 deletions
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+
}
Lines changed: 16 additions & 0 deletions
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

Lines changed: 0 additions & 3 deletions
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 () {
Lines changed: 7 additions & 0 deletions
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+
Lines changed: 16 additions & 0 deletions
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+
}
Lines changed: 5 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
}
1111
]
1212
]
13-
}
13+
}
Lines changed: 5 additions & 0 deletions
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+
}
Lines changed: 16 additions & 0 deletions
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+
}
Lines changed: 4 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)