Skip to content

Commit 2d8f55b

Browse files
lmiller1990elevatebart
authored andcommitted
feat(npm/react): Add craco plugin (#16333)
* add craco example * feat: add craco plugin * update readme * remove old files
1 parent 500cab9 commit 2d8f55b

23 files changed

+364
-0
lines changed

npm/react/examples/craco/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
This is an example project created using [CRACO](https://github.com/gsoft-inc/craco) (Create React App Configuration Override).
2+
3+
Use the plugin:
4+
5+
```js
6+
// cypress/plugins/index.js
7+
8+
// import your craco.config.js
9+
const cracoConfig = require('../../craco.config.js')
10+
const injectDevServer = require('@cypress/react/plugins/craco')
11+
12+
module.exports = (on, config) => {
13+
injectDevServer(on, config, cracoConfig)
14+
15+
return config
16+
}
17+
```
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const CracoLessPlugin = require('craco-less')
2+
3+
module.exports = {
4+
plugins: [
5+
{
6+
plugin: CracoLessPlugin,
7+
options: {
8+
lessLoaderOptions: {
9+
lessOptions: {
10+
javascriptEnabled: true,
11+
},
12+
},
13+
},
14+
},
15+
],
16+
}

npm/react/examples/craco/cypress.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"component": {
3+
"testFiles": "**/*.test.{js,ts,jsx,tsx}",
4+
"componentFolder": "src"
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Using fixtures to represent data",
3+
"email": "hello@cypress.io",
4+
"body": "Fixtures are a great way to mock data for responses to routes"
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const cracoConfig = require('../../craco.config.js')
2+
const injectDevServer = require('@cypress/react/plugins/craco')
3+
4+
module.exports = (on, config) => {
5+
injectDevServer(on, config, cracoConfig)
6+
7+
return config
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ***********************************************
2+
// This example commands.js shows you how to
3+
// create various custom commands and overwrite
4+
// existing commands.
5+
//
6+
// For more comprehensive examples of custom
7+
// commands please read more here:
8+
// https://on.cypress.io/custom-commands
9+
// ***********************************************
10+
//
11+
//
12+
// -- This is a parent command --
13+
// Cypress.Commands.add('login', (email, password) => { ... })
14+
//
15+
//
16+
// -- This is a child command --
17+
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
18+
//
19+
//
20+
// -- This is a dual command --
21+
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
22+
//
23+
//
24+
// -- This will overwrite an existing command --
25+
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ***********************************************************
2+
// This example support/index.js is processed and
3+
// loaded automatically before your test files.
4+
//
5+
// This is a great place to put global configuration and
6+
// behavior that modifies Cypress.
7+
//
8+
// You can change the location of this file or turn off
9+
// automatically serving support files with the
10+
// 'supportFile' configuration option.
11+
//
12+
// You can read more here:
13+
// https://on.cypress.io/configuration
14+
// ***********************************************************
15+
16+
// Import commands.js using ES2015 syntax:
17+
import './commands'
18+
19+
// Alternatively you can use CommonJS syntax:
20+
// require('./commands')

npm/react/examples/craco/package.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "craco",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"cy:open": "node ../../../../scripts/cypress open-ct",
7+
"test": "node ../../../../scripts/cypress run-ct",
8+
"start": "craco start",
9+
"build": "craco build"
10+
},
11+
"dependencies": {
12+
"@craco/craco": "^6.1.2",
13+
"@types/react": "^17.0.0",
14+
"@types/react-dom": "^17.0.0",
15+
"craco-less": "^1.17.1",
16+
"react": "^17.0.2",
17+
"react-dom": "^17.0.2",
18+
"react-scripts": "4.0.3",
19+
"typescript": "^4.1.2",
20+
"web-vitals": "^1.0.1"
21+
},
22+
"devDependencies": {
23+
"@cypress/react": "file:../../dist"
24+
},
25+
"browserslist": {
26+
"production": [
27+
">0.2%",
28+
"not dead",
29+
"not op_mini all"
30+
],
31+
"development": [
32+
"last 1 chrome version",
33+
"last 1 firefox version",
34+
"last 1 safari version"
35+
]
36+
}
37+
}
3.78 KB
Binary file not shown.
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<!--
14+
manifest.json provides metadata used when your web app is installed on a
15+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16+
-->
17+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
<!--
19+
Notice the use of %PUBLIC_URL% in the tags above.
20+
It will be replaced with the URL of the `public` folder during the build.
21+
Only files inside the `public` folder can be referenced from the HTML.
22+
23+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24+
work correctly both with client-side routing and a non-root public URL.
25+
Learn how to configure a non-root public URL by running `npm run build`.
26+
-->
27+
<title>React App</title>
28+
</head>
29+
<body>
30+
<noscript>You need to enable JavaScript to run this app.</noscript>
31+
<div id="root"></div>
32+
<!--
33+
This HTML file is a template.
34+
If you open it directly in the browser, you will see an empty page.
35+
36+
You can add webfonts, meta tags, or analytics to this file.
37+
The build step will place the bundled scripts into the <body> tag.
38+
39+
To begin the development, run `npm start` or `yarn start`.
40+
To create a production bundle, use `npm run build` or `yarn build`.
41+
-->
42+
</body>
43+
</html>
5.22 KB
Loading
9.44 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
},
10+
{
11+
"src": "logo192.png",
12+
"type": "image/png",
13+
"sizes": "192x192"
14+
},
15+
{
16+
"src": "logo512.png",
17+
"type": "image/png",
18+
"sizes": "512x512"
19+
}
20+
],
21+
"start_url": ".",
22+
"display": "standalone",
23+
"theme_color": "#000000",
24+
"background_color": "#ffffff"
25+
}

npm/react/examples/craco/src/App.less

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.App {
2+
text-align: center;
3+
}
4+
5+
.App-logo {
6+
height: 40vmin;
7+
pointer-events: none;
8+
}
9+
10+
@media (prefers-reduced-motion: no-preference) {
11+
.App-logo {
12+
animation: App-logo-spin infinite 20s linear;
13+
}
14+
}
15+
16+
.App-header {
17+
background-color: #282c34;
18+
min-height: 100vh;
19+
display: flex;
20+
flex-direction: column;
21+
align-items: center;
22+
justify-content: center;
23+
font-size: calc(10px + 2vmin);
24+
color: white;
25+
}
26+
27+
.App-link {
28+
color: #61dafb;
29+
}
30+
31+
@keyframes App-logo-spin {
32+
from {
33+
transform: rotate(0deg);
34+
}
35+
to {
36+
transform: rotate(360deg);
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react'
2+
import { mount } from '@cypress/react'
3+
import App from './App'
4+
5+
it('renders learn react link', () => {
6+
mount(<App />)
7+
cy.get('a').contains('Learn React')
8+
})

npm/react/examples/craco/src/App.tsx

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react'
2+
import logo from './logo.svg'
3+
import './App.less'
4+
5+
function App () {
6+
return (
7+
<div className="App">
8+
<header className="App-header">
9+
<img src={logo} className="App-logo" alt="logo" />
10+
<p>
11+
Edit <code>src/App.tsx</code> and save to reload.
12+
</p>
13+
<a
14+
className="App-link"
15+
href="https://reactjs.org"
16+
target="_blank"
17+
rel="noopener noreferrer"
18+
>
19+
Learn React
20+
</a>
21+
</header>
22+
</div>
23+
)
24+
}
25+
26+
export default App
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
body {
2+
margin: 0;
3+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4+
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5+
sans-serif;
6+
-webkit-font-smoothing: antialiased;
7+
-moz-osx-font-smoothing: grayscale;
8+
}
9+
10+
code {
11+
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12+
monospace;
13+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react'
2+
import ReactDOM from 'react-dom'
3+
import './index.css'
4+
import App from './App'
5+
import reportWebVitals from './reportWebVitals'
6+
7+
ReactDOM.render(
8+
<React.StrictMode>
9+
<App />
10+
</React.StrictMode>,
11+
document.getElementById('root'),
12+
)
13+
14+
// If you want to start measuring performance in your app, pass a function
15+
// to log results (for example: reportWebVitals(console.log))
16+
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
17+
reportWebVitals()

npm/react/examples/craco/src/logo.svg

+1
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="react-scripts" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ReportHandler } from 'web-vitals'
2+
3+
const reportWebVitals = (onPerfEntry?: ReportHandler) => {
4+
if (onPerfEntry && onPerfEntry instanceof Function) {
5+
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
6+
getCLS(onPerfEntry)
7+
getFID(onPerfEntry)
8+
getFCP(onPerfEntry)
9+
getLCP(onPerfEntry)
10+
getTTFB(onPerfEntry)
11+
})
12+
}
13+
}
14+
15+
export default reportWebVitals
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": [
5+
"dom",
6+
"dom.iterable",
7+
"esnext"
8+
],
9+
"allowJs": true,
10+
"skipLibCheck": true,
11+
"esModuleInterop": true,
12+
"allowSyntheticDefaultImports": true,
13+
"strict": true,
14+
"forceConsistentCasingInFileNames": true,
15+
"noFallthroughCasesInSwitch": true,
16+
"module": "esnext",
17+
"moduleResolution": "node",
18+
"resolveJsonModule": true,
19+
"isolatedModules": true,
20+
"noEmit": true,
21+
"jsx": "react-jsx"
22+
},
23+
"include": [
24+
"src"
25+
]
26+
}

npm/react/plugins/craco/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { startDevServer } = require('@cypress/webpack-dev-server')
2+
const { createWebpackDevConfig } = require('@craco/craco')
3+
4+
module.exports = (on, config, cracoConfig) => {
5+
if (!cracoConfig) {
6+
throw Error('craco config is required.')
7+
}
8+
9+
on('dev-server:start', (options) => {
10+
return startDevServer({
11+
options,
12+
webpackConfig: createWebpackDevConfig(cracoConfig),
13+
})
14+
})
15+
16+
return config
17+
}

0 commit comments

Comments
 (0)