react-runner
repository·master·Indexed 19 days ago
https://github.com/nihgwu/react-runnerA library for running React code dynamically. It supports TypeScript, class fields, and multi-file imports via custom scope configuration. The ecosystem includes react-live-runner for a react-live compatible API, react-runner-codemirror for CodeMirror6 integration, and a set of UI components like LiveProvider, LiveEditor, and LivePreview for building live-coding playgrounds.
What's inside react-runner
- React Runner CodeMirror is a React wrapper for CodeMirror6 designed specifically for editing React code within a React application. It provides an integrated editing experience for React-based code snippets.
Explore the React Playground
masterThe Play React playground is a web-based environment for experimenting with React code, powered by
react-runner. It allows you to write and run React code directly in the browser without the overhead of iframes. Key capabilities include:- No iframe: Code runs directly in the environment.
- Dynamic imports: Supports loading modules dynamically.
- Style import: Supports importing CSS/styles.
- Multi files: Supports working with multiple files in a single playground session.
https://play-react.vercel.app/Configure import statements and multi-file support
masterYou can enable
importstatements in your live code by configuring theimportkey within thescopeobject. UseimportCodefromreact-runnerto treat local file contents as modules.To support imports, your scope should follow this structure:
import.constants: For constant mappings.import.[package-name]: For external packages.import.[path]: For local files usingimportCode.
import { importCode } from 'react-runner' import * as YourPkg from 'your-pkg' const baseScope = { /* base globals */ } const scope = { ...baseScope, // scope used by import statement import: { constants: { A: 'a' }, 'your-pkg': YourPkg, './local-file': importCode(localFileContent, baseScope), }, } // In your live code: import { A } from 'constants' import Foo, { Bar } from 'your-pkg' import What, { Ever } from './local-file' export default function Demo() { /* render */ }Build and run a Remix application for production
masterTo prepare your Remix application for production, you must first build it and then start the production server.
- Build the application:
npm run build- Start the application in production mode:
npm startnpm run build npm startEdit pages and API routes in the Next.js example
masterThis project follows standard Next.js conventions:
- React Pages: Modify
pages/index.tsxto edit the main page. The page will auto-update upon saving. - API Routes: Files located in the
pages/apidirectory are treated as API endpoints rather than React pages. For example, the endpointhttp://localhost:3000/api/hellois defined inpages/api/hello.ts.
- React Pages: Modify
Migrate a Remix app to a pre-configured hosting template
masterIf you want to use a specific hosting provider's configuration (e.g., Vercel, Netlify, Cloudflare), you can use
npx create-remix@latestto generate a new project with that host pre-configured, then migrate your existingapp/folder into it:- Create a new project with your target host:
npx create-remix@latest - Navigate to the new project directory.
- Delete the default
app/folder in the new project. - Copy your existing
app/folder into the new project directory.
cd .. # create a new project, and pick a pre-configured host npx create-remix@latest cd my-new-remix-app # remove the new project's app (not the old one!) rm -rf app # copy your app over cp -R ../my-old-remix-app/app app- Create a new project with your target host:
Manage the Create React App project with Yarn scripts
masterThis project uses Create React App and provides several scripts via
yarnto manage development, testing, and production builds.- Development: Use
yarn startto run the app in development mode. The app will be available athttp://localhost:3000and will automatically reload on file edits. - Testing: Use
yarn testto launch the test runner in interactive watch mode. - Production Build: Use
yarn buildto create a minified, optimized production build in thebuildfolder, ready for deployment. - Customization: Use
yarn ejectif you need full control over the underlying build tools (webpack, Babel, ESLint, etc.). Warning: This is a one-way operation and cannot be undone.
yarn start yarn test yarn build yarn eject- Development: Use
Develop a Remix application
masterTo start the Remix application in development mode, which enables asset rebuilding on file changes, run the following command in your terminal:
npm run devDeploy a Remix application manually (DIY)
masterThe built-in Remix app server is production-ready for Node.js environments. When deploying manually, ensure you deploy the following output directories generated by the build process:
build/public/build/
Install react-runner via npm or yarn
masterYou can install
react-runnerusing either Yarn or NPM to run React code dynamically in your application.# Yarn yarn add react-runner # NPM npm install --save react-runnerUse react-live-runner for a react-live compatible experience
masterIf you are migrating fromreact-live,react-live-runnerprovides a compatible API. It re-exportsreact-runnerand usesSucrasefor transpilation, allowing for modern features like arrow functions in event handlers without manual binding.Run the website development server
masterTo start the development server for the website project, use the following commands in your terminal. Once running, the application will be available at
http://localhost:3000.npm run dev # or yarn dev