37
loading...
This website collects cookies to deliver better user experience
This article was originally posted here
npm
.codesandbox-ci-test
. Clone it locally and add a package.json
file with the following content:{
"name": "codesandbox-ci-test",
"version": "1.0.0",
"main": "dist/index.js",
"engines": {
"node": ">=12"
},
"scripts": {
"build": "kcd-scripts build"
},
"peerDependencies": {
"react": "^17.0.2"
},
"devDependencies": {
"kcd-scripts": "^11.2.2",
"react": "^17.0.2"
},
"dependencies": {
"@babel/runtime": "^7.16.0"
}
}
kcd-scripts
to build our project, and we'll be using react
to create a small reusable component for this demo. @babel/runtime
is required by kcd-scripts
, otherwise it won't build the project.src/index.js
create a simple Counter component:import * as React from 'react';
export default function Counter() {
const [count, setCount] = React.useState(0);
return (
<div>
<p>You clicked {count} times!!!</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
}
ci.json
in a folder called .codesandbox
in the root of the repository and add:{
"buildCommand": "build",
"node": "12",
"sandboxes": ["/cra-template"]
}
buildCommand
indicates which script in package.json should run to build the project.node
is the Node.js version to use for building the PR.sandboxes
is the list of sandboxes that we want to be generated. The default value is vanilla
.cra-template
.cra-template
, inside of this folder create a package.json
:{
"name": "react-starter-example",
"version": "1.0.0",
"description": "React example starter project",
"main": "src/index.js",
"dependencies": {
"react": "17.0.2",
"react-dom": "17.0.2",
"react-scripts": "4.0.0"
},
"devDependencies": {
"@babel/runtime": "7.13.8",
"typescript": "4.1.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"browserslist": [">0.2%", "not dead", "not ie <= 11", "not op_mini all"]
}
src
folder and a index.js
file with:import { StrictMode } from 'react';
import ReactDOM from 'react-dom';
import Counter from 'codesandbox-ci-test';
const rootElement = document.getElementById('root');
ReactDOM.render(
<StrictMode>
<Counter />
</StrictMode>,
rootElement
);
public
folder with a index.html
file with:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#000000" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<title>React App</title>
</head>
<body>
<noscript> You need to enable JavaScript to run this app. </noscript>
<div id="root"></div>
</body>
</html>