Skip to content

Commit 70c7c36

Browse files
authored
fix: configure proper pages directory for next application (#18009)
* fix: find pages directory for next applications * convert new files to typescript and add transpile step * add tsconfig for plugins
1 parent 0284b9d commit 70c7c36

File tree

7 files changed

+70
-3
lines changed

7 files changed

+70
-3
lines changed

npm/react/package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44
"description": "Test React components using Cypress",
55
"main": "dist/cypress-react.cjs.js",
66
"scripts": {
7-
"build": "rimraf dist && yarn rollup -c rollup.config.js",
7+
"build": "rimraf dist && yarn transpile-plugins && yarn rollup -c rollup.config.js",
88
"build-prod": "yarn build",
99
"cy:open": "node ../../scripts/cypress.js open-ct",
1010
"cy:open:debug": "node --inspect-brk ../../scripts/start.js --component-testing --run-project ${PWD}",
1111
"cy:run": "node ../../scripts/cypress.js run-ct",
1212
"cy:run:debug": "node --inspect-brk ../../scripts/start.js --component-testing --run-project ${PWD}",
1313
"pretest": "yarn transpile",
14-
"test": "yarn cy:run",
14+
"test": "yarn test-unit && yarn test-component",
15+
"test-unit": "mocha -r @packages/ts/register test/**/*.spec.*",
16+
"test-component": "yarn cy:run",
1517
"test-ci": "node ../../scripts/run-ct-examples.js --examplesList=./examples.env",
1618
"transpile": "tsc",
19+
"transpile-plugins": "yarn transpile --project ./plugins",
1720
"watch": "yarn build --watch --watch.exclude ./dist/**/*"
1821
},
1922
"dependencies": {

npm/react/plugins/next/findNextWebpackConfig.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/// <reference types="next" />
33
const debug = require('debug')('@cypress/react')
44
const getNextJsBaseWebpackConfig = require('next/dist/build/webpack-config').default
5+
const { findPagesDir } = require('../../dist/next/findPagesDir')
56

67
async function getNextWebpackConfig (config) {
78
let loadConfig
@@ -26,7 +27,7 @@ async function getNextWebpackConfig (config) {
2627
config: nextConfig,
2728
dev: true,
2829
isServer: false,
29-
pagesDir: config.projectRoot,
30+
pagesDir: findPagesDir(config.projectRoot),
3031
entrypoints: {},
3132
rewrites: { fallback: [], afterFiles: [], beforeFiles: [] },
3233
},
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as fs from 'fs'
2+
import * as path from 'path'
3+
4+
const existsSync = (file: string) => {
5+
try {
6+
fs.accessSync(file, fs.constants.F_OK)
7+
8+
return true
9+
} catch (_) {
10+
return false
11+
}
12+
}
13+
14+
/**
15+
* Next allows the `pages` directory to be located at either
16+
* `${projectRoot}/pages` or `${projectRoot}/src/pages`.
17+
* If neither is found, return projectRoot
18+
*/
19+
export function findPagesDir (projectRoot: string) {
20+
// prioritize ./pages over ./src/pages
21+
let pagesDir = path.join(projectRoot, 'pages')
22+
23+
if (existsSync(pagesDir)) {
24+
return pagesDir
25+
}
26+
27+
pagesDir = path.join(projectRoot, 'src/pages')
28+
if (existsSync(pagesDir)) {
29+
return pagesDir
30+
}
31+
32+
return projectRoot
33+
}

npm/react/plugins/tsconfig.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "./",
5+
"types": [
6+
"cypress",
7+
"node"
8+
]
9+
},
10+
"include": ["./**/*.ts"]
11+
}

npm/react/test/fixtures/next-plugin/next-project-one/pages/foo.js

Whitespace-only changes.

npm/react/test/fixtures/next-plugin/next-project-two/src/pages/foo.js

Whitespace-only changes.
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { expect } from 'chai'
2+
import * as path from 'path'
3+
import { findPagesDir } from '../../plugins/next/findPagesDir'
4+
5+
describe('Next.js findPagesDir', () => {
6+
it('should find the correct pagesDir', () => {
7+
const nextPluginFixturePath = path.join(__dirname, '../fixtures/next-plugin')
8+
9+
let projectRoot = nextPluginFixturePath
10+
11+
expect(findPagesDir(projectRoot)).to.equal(projectRoot)
12+
13+
projectRoot = path.join(nextPluginFixturePath, 'next-project-one')
14+
expect(findPagesDir(projectRoot)).to.equal(path.join(projectRoot, 'pages'))
15+
16+
projectRoot = path.join(nextPluginFixturePath, 'next-project-two')
17+
expect(findPagesDir(projectRoot)).to.equal(path.join(projectRoot, 'src/pages'))
18+
})
19+
})

0 commit comments

Comments
 (0)