这是一个基于 React + TypeScript 的在线代码运行环境,支持通过 CodeMirror 编辑器编写并实时运行 JavaScript/TypeScript 示例代码,适合练习《JavaScript 高级程序设计》等书籍中的章节代码。
.ts
源文件为字符串并动态运行git clone https://github.com/liu-yongyuan/react-codemirror-tailwind-template.git
cd react-codemirror-tailwind-template
npm install
npm run dev
浏览器自动打开:http://localhost:3000
.
├── public/
│ └── index.html
├── src/
│ ├── code/ # 存放每个章节的代码示例(.ts)
│ ├── components/CodeRunner.tsx # 主运行组件
│ ├── index.tsx # 入口文件
│ └── index.css # TailwindCSS 样式入口
├── tailwind.config.js
├── postcss.config.js
├── webpack.config.js
└── README.md
在 src/code
下添加 .ts
文件,如:
// variable-scope.ts
let a = 10;
{
let a = 20;
console.log(a); // 20
}
console.log(a); // 10
然后在 App.tsx
中引入:
import code from './code/variable-scope.ts?raw';
Made with ❤️ by yongyuan[https://github.com/liu-yongyuan]
MIT