npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@d8d-webcontainer/react

v0.1.9

Published

基于 WebContainer 的 React Hooks 库,提供了一组用于在 React 应用中使用 WebContainer 的 Hook。

Downloads

21

Readme

@d8d-webcontainer/react

基于 WebContainer 的 React Hooks 库,提供了一组用于在 React 应用中使用 WebContainer 的 Hook。

特性

  • 🚀 多终端支持 - 可以创建和管理多个终端实例
  • 📁 文件系统操作 - 支持文件浏览、编辑和保存
  • 🔄 进程管理 - 支持进程启动、监控和终止
  • 🛠 项目初始化 - 支持 git clone、依赖安装等初始化操作
  • 🎯 开发服务器检测 - 自动检测和提供开发服务器地址

安装

npm install @d8d-webcontainer/react @d8d-webcontainer/api

基础用法

import { useWebContainer, useTerminal, useFileSystem } from '@d8d-webcontainer/react';

function WebContainerDemo() {
  // 管理多个终端
  const [terminals, setTerminals] = useState([
    { id: 'default', ref: useRef(null) }
  ]);

  // 初始化 WebContainer
  const { container, status, error, devServerUrl, activeProcesses } = useWebContainer({
    serverUrl: 'http://localhost:3000',
    onError: (error) => console.error(error),
  });

  // 终端操作
  const { handleTerminalInput } = useTerminal({
    container,
    activeProcesses,
    onOutput: (data, terminalId) => {
      // 将输出发送到对应的终端
      const terminal = terminals.find(t => t.id === terminalId);
      terminal?.ref.current?.write(data);
    },
    onError: (error) => console.error(error),
  });

  // 文件系统操作
  const { selectedFile, handleFileSelect, handleFileSave } = useFileSystem({
    container,
    onStatusChange: (status) => console.log(status),
    onError: (error) => console.error(error),
  });

  // 添加新终端
  const addNewTerminal = () => {
    const newTerminalId = `terminal-${terminals.length + 1}`;
    setTerminals(prev => [...prev, { 
      id: newTerminalId, 
      ref: createRef() 
    }]);
  };

  return (
    <div>
      <div>状态: {status}</div>
      {error && <div>错误: {error.message}</div>}
      
      {/* 终端列表 */}
      <div className="terminals">
        {terminals.map(({ id, ref }) => (
          <Terminal
            key={id}
            ref={ref}
            onInput={(command) => handleTerminalInput(command, id)}
          />
        ))}
      </div>
      
      {/* 文件浏览器和编辑器 */}
      <FileExplorer container={container} onFileSelect={handleFileSelect} />
      {selectedFile && (
        <Editor
          path={selectedFile.path}
          content={selectedFile.content}
          onSave={handleFileSave}
        />
      )}
    </div>
  );
}

Hooks API

useWebContainer

初始化和管理 WebContainer 实例。

interface UseWebContainerProps {
  serverUrl: string;
    onError?: (error: Error) => void;
    onOutput?: (data: string) => void;
}
interface UseWebContainerReturn {
    container: WebContainer | null;
    status: string;
    error: Error | null;
    devServerUrl: string | null;
    activeProcesses: Array<{pid: number; command: string; terminalId: string}>;
}
const result = useWebContainer(props: UseWebContainerProps): UseWebContainerReturn;

useTerminal

管理终端输入输出和进程控制。

interface UseTerminalProps {
    container: WebContainer | null;
    activeProcesses: Array<{pid: number; command: string; terminalId: string}>;
    onOutput?: (data: string, terminalId: string) => void;
    onError?: (error: Error) => void;
}
interface UseTerminalReturn {
    handleTerminalInput: (command: string, terminalId: string) => Promise<void>;
}
const result = useTerminal(props: UseTerminalProps): UseTerminalReturn;

useFileSystem

管理文件系统操作。

    interface UseFileSystemProps {
    container: WebContainer | null;
    onStatusChange?: (status: string) => void;
    onError?: (error: string) => void;
}
interface UseFileSystemReturn {
    handleFileSelect: (path: string, content: string) => void;
    handleFileSave: (path: string, content: string) => Promise<void>;
    selectedFile: { path: string; content: string } | null;
}
const result = useFileSystem(props: UseFileSystemProps): UseFileSystemReturn;

注意事项

  1. 需要同时安装 @d8d-webcontainer/api 作为依赖
  2. WebContainer 服务器需要单独运行
  3. 建议在组件卸载时清理 WebContainer 实例
  4. 文件路径使用正斜杠 / 作为分隔符
  5. 终端命令执行结果会通过 onOutput 回调返回

示例项目

完整的示例项目可以参考:

许可证

MIT