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): remove apis from ts,tsx (#35183) #35198

Merged
merged 1 commit into from
Mar 22, 2022
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,65 @@
/**
* Test that page templates have certain exports removed while other files are left alone.
*
* Page templates support only a default exported React component and named exports of
* `config` and `getServerData`, so it's not necessary (or possible) to test other exports
* in page templates.
*/

const config = `config exported from a non-page template module`
const getServerData = `getServerData exported from a non-page template module`
const helloWorld = `hello world`

describe(`modifed exports`, () => {
beforeEach(() => {
cy.visit(`/modified-exports-ts`).waitForRouteChange()
})

describe(`page templates`, () => {
it(`should have exports named config removed`, () => {
cy.getTestElement(`modified-exports-page-template-config`)
.invoke(`text`)
.should(`contain`, `undefined`)
})
it(`should have exports named getServerData removed`, () => {
cy.getTestElement(`modified-exports-page-template-get-server-data`)
.invoke(`text`)
.should(`contain`, `undefined`)
})
it(`should have imported exports named config left alone`, () => {
cy.getTestElement(`unmodified-exports-page-template-config`)
.invoke(`text`)
.should(`contain`, config)
})
it(`should have imported exports named getServerData left alone`, () => {
cy.getTestElement(`unmodified-exports-page-template-get-server-data`)
.invoke(`text`)
.should(`contain`, getServerData)
})
it(`should have other imported exports left alone`, () => {
cy.getTestElement(`unmodified-exports-page-template-hello-world`)
.invoke(`text`)
.should(`contain`, helloWorld)
})
})

describe(`other JS files`, () => {
it(`should have exports named config left alone`, () => {
cy.getTestElement(`unmodified-exports-config`)
.invoke(`text`)
.should(`contain`, config)
})

it(`should have exports named getServerData left alone`, () => {
cy.getTestElement(`unmodified-exports-get-server-data`)
.invoke(`text`)
.should(`contain`, getServerData)
})

it(`should have other named exports left alone`, () => {
cy.getTestElement(`unmodified-exports-hello-world`)
.invoke(`text`)
.should(`contain`, helloWorld)
})
})
})
39 changes: 39 additions & 0 deletions e2e-tests/production-runtime/src/pages/modified-exports-ts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from "react"
import UnmodifiedExports, {
config as importedConfig,
getServerData as importedGetServerData,
helloWorld,
} from "../components/unmodified-exports"

function ModifiedExports() {
return (
<div>
<p>This is the modified exports for page templates test page</p>
{/* Use typeof to avoid runtime error */}
<p data-testid="modified-exports-page-template-config">{typeof config}</p>
<p data-testid="modified-exports-page-template-get-server-data">
{typeof getServerData}
</p>
<p data-testid="unmodified-exports-page-template-config">
{importedConfig()}
</p>
<p data-testid="unmodified-exports-page-template-get-server-data">
{importedGetServerData()}
</p>
<p data-testid="unmodified-exports-page-template-hello-world">
{helloWorld()}
</p>
<UnmodifiedExports />
</div>
)
}

export function config() {
return () => "config exported from a page template module" // Expects config to be a function factory
}

export function getServerData() {
return "getServerData exported from a page template module"
}

export default ModifiedExports
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/webpack-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ export const createWebpackUtils = (
modulesThatUseGatsby?: Array<IModuleThatUseGatsby>
} = {}): RuleSetRule => {
return {
test: /\.(js|mjs|jsx)$/,
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: (modulePath: string): boolean => {
// when it's not coming from node_modules we treat it as a source file.
if (!vendorRegex.test(modulePath)) {
Expand Down