Skip to content

Commit 83881f2

Browse files
wardpeetpieh
authored andcommitted
fix(gatsby): remove apis from ts,tsx (#35183)
(cherry picked from commit 8de18e7)
1 parent f080b46 commit 83881f2

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Test that page templates have certain exports removed while other files are left alone.
3+
*
4+
* Page templates support only a default exported React component and named exports of
5+
* `config` and `getServerData`, so it's not necessary (or possible) to test other exports
6+
* in page templates.
7+
*/
8+
9+
const config = `config exported from a non-page template module`
10+
const getServerData = `getServerData exported from a non-page template module`
11+
const helloWorld = `hello world`
12+
13+
describe(`modifed exports`, () => {
14+
beforeEach(() => {
15+
cy.visit(`/modified-exports-ts`).waitForRouteChange()
16+
})
17+
18+
describe(`page templates`, () => {
19+
it(`should have exports named config removed`, () => {
20+
cy.getTestElement(`modified-exports-page-template-config`)
21+
.invoke(`text`)
22+
.should(`contain`, `undefined`)
23+
})
24+
it(`should have exports named getServerData removed`, () => {
25+
cy.getTestElement(`modified-exports-page-template-get-server-data`)
26+
.invoke(`text`)
27+
.should(`contain`, `undefined`)
28+
})
29+
it(`should have imported exports named config left alone`, () => {
30+
cy.getTestElement(`unmodified-exports-page-template-config`)
31+
.invoke(`text`)
32+
.should(`contain`, config)
33+
})
34+
it(`should have imported exports named getServerData left alone`, () => {
35+
cy.getTestElement(`unmodified-exports-page-template-get-server-data`)
36+
.invoke(`text`)
37+
.should(`contain`, getServerData)
38+
})
39+
it(`should have other imported exports left alone`, () => {
40+
cy.getTestElement(`unmodified-exports-page-template-hello-world`)
41+
.invoke(`text`)
42+
.should(`contain`, helloWorld)
43+
})
44+
})
45+
46+
describe(`other JS files`, () => {
47+
it(`should have exports named config left alone`, () => {
48+
cy.getTestElement(`unmodified-exports-config`)
49+
.invoke(`text`)
50+
.should(`contain`, config)
51+
})
52+
53+
it(`should have exports named getServerData left alone`, () => {
54+
cy.getTestElement(`unmodified-exports-get-server-data`)
55+
.invoke(`text`)
56+
.should(`contain`, getServerData)
57+
})
58+
59+
it(`should have other named exports left alone`, () => {
60+
cy.getTestElement(`unmodified-exports-hello-world`)
61+
.invoke(`text`)
62+
.should(`contain`, helloWorld)
63+
})
64+
})
65+
})
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from "react"
2+
import UnmodifiedExports, {
3+
config as importedConfig,
4+
getServerData as importedGetServerData,
5+
helloWorld,
6+
} from "../components/unmodified-exports"
7+
8+
function ModifiedExports() {
9+
return (
10+
<div>
11+
<p>This is the modified exports for page templates test page</p>
12+
{/* Use typeof to avoid runtime error */}
13+
<p data-testid="modified-exports-page-template-config">{typeof config}</p>
14+
<p data-testid="modified-exports-page-template-get-server-data">
15+
{typeof getServerData}
16+
</p>
17+
<p data-testid="unmodified-exports-page-template-config">
18+
{importedConfig()}
19+
</p>
20+
<p data-testid="unmodified-exports-page-template-get-server-data">
21+
{importedGetServerData()}
22+
</p>
23+
<p data-testid="unmodified-exports-page-template-hello-world">
24+
{helloWorld()}
25+
</p>
26+
<UnmodifiedExports />
27+
</div>
28+
)
29+
}
30+
31+
export function config() {
32+
return () => "config exported from a page template module" // Expects config to be a function factory
33+
}
34+
35+
export function getServerData() {
36+
return "getServerData exported from a page template module"
37+
}
38+
39+
export default ModifiedExports

packages/gatsby/src/utils/webpack-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ export const createWebpackUtils = (
416416
modulesThatUseGatsby?: Array<IModuleThatUseGatsby>
417417
} = {}): RuleSetRule => {
418418
return {
419-
test: /\.(js|mjs|jsx)$/,
419+
test: /\.(js|mjs|jsx|ts|tsx)$/,
420420
include: (modulePath: string): boolean => {
421421
// when it's not coming from node_modules we treat it as a source file.
422422
if (!vendorRegex.test(modulePath)) {

0 commit comments

Comments
 (0)