Coder Social home page Coder Social logo

neteaselofter / hot-chocolate Goto Github PK

View Code? Open in Web Editor NEW
22.0 3.0 0.0 1.14 MB

易用的微前端架构。基于proxy + Shadow DOM 实现的浏览器沙箱机刷。支持插件扩展自定义沙箱内容。

License: MIT License

JavaScript 0.57% TypeScript 99.43%
javascript micro-frontend

hot-chocolate's Introduction

hot-chocolate

npm version npm download

在沙箱中运行你的JS

;

安装

npm install hot-chocolate;

基础使用例子

import {
  Manager
} from 'hot-chocolate';

const manager = new Manager([
  {
    name: 'app1', // 子应用的名字,必须保证不重复
    sandboxOptions: {
      // 通过 url 配置沙箱默认html
      // 假设http://abc.com/index.html 返回如下html: `<html><body><div id="root"></div><script src="http://abc.com/app1.js"></script></body></html>`
      htmlRemote: 'http://abc.com/index.html'
    }
  },
  {
    name: 'app2',
    sandboxOptions: {
      htmlString: `
        <html>
          <body>
            <div id="root"></div>
            <script>
              window.custom = 'custom';
              window.customFn = (data) => {
                console.log(data);
              }
            </script>
            <script src="app2.js"></script>
          </body>
        </html>
      `
    }
  }
]);

// 激活但不自动挂载到文档流里
// 适合一些提供 纯函数 但是不需要界面的内容
const sandbox = manager.activate('app1');

// 激活并自动挂载到文档流里
// 适合同时会提供界面的内容
const sandbox = manager.activateAndMount('app2', document.body);

// 销毁挂载的子应用,如果不写参数默认销毁所有子应用
manager.deactivateAll('app2')

// ready 可以保证一些异步的js已完成加载
sandbox.ready().then(() => {

  // 可以通过 contentWindow 访问到里面运行的内容
  sandbox.contentWindow.custom === 'custom';

  // 但是里外的window是相互隔离的
  console.log(window.custom) // undefined

  // 还可以直接调用注册在沙箱里的window上的函数
  sandbox.contentWindow.customFn(1); // console: 1

  // 也可以通过 runCode 在沙箱内运行代码
  sandbox.runCode(`
    window.yourCustomData = 1;
  `);

  // 此时可以通过 contentWindow 访问到 runCode 里面运行的内容
  sandbox.contentWindow.yourCustomData === 1;

  // 也可以通过 runRemoteCode 继续挂载其他远程JS
  // 会通过 Fetch 去拉取远程代码
  // 注意跨域问题
  sandbox.runRemoteCode('http://your-site/xx.js');
})

本地运行examples

  1. 先clone本仓库
git clone https://github.com/NeteaseLofter/hot-chocolate.git
  1. 安装依赖,也可使用pnpm
npm run setup
  1. 运行本地例子,会使用3000端口
npm run examples

更多文档

更多使用例子 hot-chocolate详细指南

更多插件

名字 功能
@hot-chocolate/plugin-request 拦截子应用中发出的AJAX请求
@hot-chocolate/plugin-dispatch 应用间相互调用

hot-chocolate's People

Contributors

huey-ls avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.