3.2. Create your own project
Now do it on your computer. Open your terminal and create the app from the template (Vite guide):
pnpm create vite my-react-app --template react-ts
It asks a few questions. The defaults are fine, but answer No to Install with pnpm and start now?: we do that ourselves. Next, install the dependencies and start developing:
cd my-react-app
pnpm install
pnpm dev
What we have
The template changes over time, so your files may differ a little:
my-react-app
├── public # global assets: favicon, fonts
├── src # your code
│ ├── assets
│ ├── App.css
│ ├── App.tsx # 4. first component we render
│ ├── index.css
│ └── main.tsx # 3. creates React app in root div
├── index.html # 2. root div + main script
├── package.json
├── pnpm-lock.yaml
├── tsconfig.app.json
├── tsconfig.json
├── tsconfig.node.json
└── vite.config.ts # 1. config for the frontend tool
Of course, the folders can look different, like this:
└── src
├── components
├── hooks
├── store
├── styles
├── tests
├── types
└── utils
It always depends on what you use in your project.
vite.config.ts
This is the file where we define how the frontend tool works. The template only turns on the React plugin:
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
// https://vite.dev/config/
export default defineConfig({
plugins: [react()],
})
But you can adjust much more here. This is close to what we use at work:
import react from '@vitejs/plugin-react'
import { defineConfig, type PluginOption } from 'vite'
const plugins: PluginOption[] = [react()]
export default defineConfig(({ command }) => {
if (command === 'build') {
return {
plugins,
build: {
// A source map in the production build is great
// for debugging, but it also exposes all code
// to everyone
sourcemap: true,
},
}
}
// We can define different behavior depending on env
if (process.env.VITE_FAKE_API) {
return { plugins }
}
return {
plugins,
// And we can connect the app with the real server
server: {
proxy: {
'/my_service': {
target: 'http://example.com/',
secure: false,
auth: process.env.SAP_AUTH, // "user:password"
},
'/sap/bc/apc': {
target: 'ws://example.com/',
ws: true,
secure: false,
},
},
},
}
})
Start it with VITE_FAKE_API=true pnpm dev and the dev server runs without a backend. Your code sees the same variable as import.meta.env.VITE_FAKE_API, so it can switch to fake data. Without it, the dev server passes /my_service calls to our SAP system (example.com here), signed in as the user in SAP_AUTH, and /sap/bc/apc as a WebSocket for ABAP push channels.
