-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support create-react-app v5 (#19434)
- Loading branch information
Showing
3 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import _debug from 'debug' | ||
import type { Configuration } from 'webpack' | ||
import reactScriptsPackageJson from 'react-scripts/package.json' | ||
|
||
const debug = _debug('@cypress/react:react-scripts') | ||
|
||
type DefinePlugin = | ||
| { definitions: Record<string, Record<string, any>> } | ||
| undefined; | ||
type ESLintWebpackPlugin = | ||
| { options: { baseConfig?: { globals?: Record<string, any> } } } | ||
| undefined; | ||
|
||
export function reactScriptsFiveModifications (webpackConfig: Configuration) { | ||
// React-Scripts sets the webpack target to ["browserslist"] which tells | ||
// webpack to target the browsers found within the browserslist config | ||
// depending on the environment (process.env.NODE_ENV). Since we set | ||
// process.env.NODE_ENV = "test", webpack is unable to find any browsers and errors. | ||
// We set BROWSERSLIST_ENV = "development" to override the default NODE_ENV search of browsers. | ||
if (!process.env.BROWSERSLIST_ENV) { | ||
process.env.BROWSERSLIST_ENV = 'development' | ||
} | ||
|
||
// We use the "development" configuration of the react-scripts webpack config. | ||
// There is a conflict when settings process.env.NODE_ENV = "test" since DefinePlugin | ||
// uses the "development" configuration and expects process.env.NODE_ENV = "development". | ||
const definePlugin: DefinePlugin = webpackConfig.plugins?.find( | ||
(plugin) => plugin.constructor.name === 'DefinePlugin' | ||
) as unknown as DefinePlugin | ||
|
||
if (definePlugin) { | ||
const processEnv = definePlugin.definitions['process.env'] | ||
|
||
processEnv.NODE_ENV = JSON.stringify('development') | ||
|
||
debug('Found "DefinePlugin", modified "process.env" definition %o', processEnv) | ||
} | ||
|
||
// React-Scripts v5 no longers uses a loader to configure eslint, so we add globals | ||
// to the plugin. | ||
const eslintPlugin = webpackConfig.plugins?.find( | ||
(plugin) => plugin.constructor.name === 'ESLintWebpackPlugin' | ||
) as unknown as ESLintWebpackPlugin | ||
|
||
if (eslintPlugin) { | ||
const cypressGlobals = ['cy', 'Cypress', 'before', 'after', 'context'] | ||
.reduce((acc, global) => ({ ...acc, [global]: 'writable' }), {}) | ||
|
||
eslintPlugin.options.baseConfig = { | ||
...eslintPlugin.options.baseConfig, | ||
globals: { | ||
...eslintPlugin.options.baseConfig?.globals, | ||
...cypressGlobals, | ||
}, | ||
} | ||
|
||
debug('Found ESLintWebpackPlugin, modified eslint config %o', eslintPlugin.options.baseConfig) | ||
} | ||
} | ||
|
||
export const isReactScripts5 = Number(reactScriptsPackageJson.version[0]) >= 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
415a7b1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
linux x64
version of the Test Runner.Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.
Run this command to install the pre-release locally:
415a7b1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
win32 x64
version of the Test Runner.Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.
Run this command to install the pre-release locally:
415a7b1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Circle has built the
darwin x64
version of the Test Runner.Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.
Run this command to install the pre-release locally: