【Web IDE】WebContainer容器在浏览器中启动运行nodejs并使用vite启动项目

8 篇文章 0 订阅
1 篇文章 0 订阅

参考了文章WebContainer/api 基础(Web IDE 技术探索 一)

在浏览器中运行vite的vue3项目

示例站点

最终效果

在这里插入图片描述

主要流程

加载WebContainer=》加载代码压缩包=>解压代码压缩包=》生成文件树=》挂载文件树=》pnpm安装依赖=》启动项目

代码

<script setup>
import { onMounted, ref } from 'vue'
import { WebContainer } from "@webcontainer/api";
import { mountZip } from '@/views/Containers/utls.js'
const webUrl = ref("");
const codeZip = '/code/vue-project.zip'
async function initContainer() {
  console.log("挂载")
  // Call only once
  const webcontainerInstance = await WebContainer.boot();
  const nodeV = await webcontainerInstance.spawn("node", ["-v"]);
  nodeV.output.pipeTo(
    new WritableStream({
      write(data) {
        console.log("node -v ==>", data);
      },
    })
  );
  const fileTree =  await mountZip(codeZip)
  console.log('挂载文件',fileTree)
  // 1. 挂载文件
  await webcontainerInstance.mount(fileTree);
  console.log("ls");
  const ls = await webcontainerInstance.spawn("ls", ["-al"]);
  ls.output.pipeTo(
    new WritableStream({
      write(data) {
        console.log(data);
      },
    })
  );
  // 2. 下载依赖
  console.log("pnpm install");
  const install = await webcontainerInstance.spawn("pnpm", ["install"]);
  install.output.pipeTo(
    new WritableStream({
      write(data) {
        console.log(data);
      },
    })
  );
  // 3. 判断exit 状态
  let code = await install.exit;
  if (code !== 0) {
    console.error("error to install.");
  }

  // 4. 启动服务
  console.log("npm run dev");
  const process = await webcontainerInstance.spawn("npm", ["run","dev"]);
  process.output.pipeTo(
    new WritableStream({
      write(data) {
        console.log(data);
      },
    })
  );
  // 5. 监听服务启动
  webcontainerInstance.on("server-ready", (port, url) => {
    console.log("server-ready", url);
    webUrl.value = url;
  });
}


onMounted(() => {
  mountZip(codeZip)
  initContainer()
})
</script>

<template>
<div>
  <iframe :src="webUrl" style="height: 100vh;width: 100%"/>
</div>
</template>

<style scoped>

</style>

工具函数

import JSZip from 'jszip'
export async function  mountZip(zipUrl){
  console.log("读取zip文件",zipUrl)
  const fileTree = {}
  try {
    // 使用 fetch 获取 ZIP 数据
    const response = await fetch(zipUrl);
    const buffer = await response.arrayBuffer();

    // 使用 JSZip 处理获取到的数据
    const zip = new JSZip();
    const zipContents = await zip.loadAsync(buffer);

    // 处理解压后的内容
    for (const [relativePath, file] of Object.entries(zipContents.files)) {
      // console.log('relativePath',relativePath)
      if(file.dir){
        let dirList = relativePath.split('/')
        // console.log('dirList',dirList)
        if(dirList.length > 2){
          let tmp = fileTree
          for (let i = 0; i < dirList.length - 1; i++) {
            // console.log('tmp(dirList[i]',tmp[dirList[i]])
            if(tmp[dirList[i]]){
              tmp = tmp[dirList[i]].directory
            }else{
              tmp[dirList[i]] = {
                directory: {},
              }
            }
          }
        }else{
          fileTree[dirList[0]] = {
            directory: {},
          }
        }
      }else{
        let dirList = relativePath.split('/')
        // console.log('dirList',dirList)
        if(dirList.length > 1) {
          let tmp = fileTree
          for (let i = 0; i < dirList.length - 1; i++) {
            // console.log('tmp(dirList[i]', tmp[dirList[i]])
            if (tmp[dirList[i]]) {
              tmp = tmp[dirList[i]].directory
            } else {
              tmp[dirList[i]] = {
                directory: {},
              }
            }
          }
          // console.log('tmp',tmp)
          tmp[dirList[dirList.length - 1]] = {
            file: {
              contents: await file.async('string')
            },
          }
        }else{
          // console.log('根目录文件',dirList)
          fileTree[dirList[dirList.length - 1]] = {
            file: {
              contents: await file.async('string')
            },
          }
        }
      }
    }
  } catch (error) {
    console.error('获取 ZIP 数据时出错:', error);
  }
  console.log('fileTree',fileTree)
  return fileTree
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
浏览器使用Node.js代码需要借助一些工具,这里介绍两种常用的方案。 1. 使用Browserify Browserify是一个将Node.js模块编译成浏览器可识别的JavaScript文件的工具。使用Browserify,可以在浏览器直接使用Node.js模块。 首先,需要在Node.js安装Browserify: ``` npm install -g browserify ``` 然后,在Node.js编写代码,例如: ``` // mymodule.js module.exports = function(name) { console.log('Hello, ' + name + '!'); }; ``` 接着,在命令行使用Browserify将代码编译成浏览器可识别的文件: ``` browserify mymodule.js -o bundle.js ``` 最后,在HTML文件使用编译后的文件: ``` <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Node.js in Browser</title> </head> <body> <script src="bundle.js"></script> <script> var mymodule = require('./mymodule'); mymodule('World'); </script> </body> </html> ``` 这样,在浏览器打开HTML文件就可以看到控制台输出了`Hello, World!`。 2. 使用Webpack Webpack是一个可以打包JavaScript模块的工具。使用Webpack,可以将多个模块打包成一个文件,避免了在HTML文件引入多个JavaScript文件的问题。 首先,需要在Node.js安装Webpack: ``` npm install -g webpack ``` 然后,在Node.js编写代码,例如: ``` // mymodule.js module.exports = function(name) { console.log('Hello, ' + name + '!'); }; ``` 接着,在命令行使用Webpack将代码打包成浏览器可识别的文件: ``` webpack mymodule.js bundle.js ``` 最后,在HTML文件使用打包后的文件: ``` <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Node.js in Browser</title> </head> <body> <script src="bundle.js"></script> <script> var mymodule = require('./mymodule'); mymodule('World'); </script> </body> </html> ``` 这样,在浏览器打开HTML文件就可以看到控制台输出了`Hello, World!`。 总的来说,使用Browserify或Webpack可以在浏览器直接使用Node.js模块,但需要注意的是,在浏览器使用Node.js代码可能会有一些限制,例如无法访问操作系统的文件系统等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值