Git Product home page Git Product logo

extreme's Introduction


Extreme

🚀 体积极小的纯运行时框架

🪀 作为一个玩具框架存在,框架源码初学者入门可选

⚙️ Powered by Vite

TypeScript Vite


📖 介绍

Extreme 是一个极小的运行时框架,它的目标是提供一个极小的框架,让初学者可以快速入门,了解一个框架的基本结构。目前来讲,Extreme更像是一个玩具框架,它的功能非常有限,但是它的代码量非常少,非常适合初学者入门。

🎮 TODO

  • 编译时
  • 响应性更新
  • 事件系统
    • 事件绑定
    • 事件流
  • 批量渲染 :for
    • 批量更新
    • 批量细粒度响应性
  • 条件渲染 :if
    • 条件渲染
    • 条件更新
  • 生命周期
    • useMount
    • useUpdated
    • ...
  • 依赖收集
    • 依赖收集
    • 依赖更新
  • 调度渲染
    • 异步渲染
    • 调度更新
  • 服务端渲染
  • Diff算法
  • 插件系统
  • 路由
  • 状态管理
  • 单元测试
  • 性能测试
  • 文档
  • 社区
  • 生态
  • 发布

📦 快速开始

1. 安装

pnpm create vite my-project --template vanilla-ts
pnpm add @sourcebug/extreme 
pnpm add @sourcebug/vite-extreme-plugin html-minifier -D

2. 配置

  • 新增vite.config.ts
import { defineConfig } from "vite";
import { rawMinifyPlugin } from "@sourcebug/vite-extreme-plugin";

export default defineConfig(() => ({
  plugins: [rawMinifyPlugin()],
}));

3. 使用

首先清理掉所有Vite初始化的代码,然后开始创建目录,现在你的目录应该是这样的:

- src
  - components
    - main
      - index.ts
      - index.html
    - index.ts
  - main.ts

接着我们填内容:

  • main.ts中:
import { Main } from "./components";

Main(document.getElementById("main")!, {});

别急,我们还没有创建Main组件,现在我们先创建一个这样的目录:

  • components/main/index.ts中:
import { createComponent, useState } from "@sourcebug/extreme";
import template from "./index.html?raw";

export const Main = createComponent("Main", () => {
  const [count, setCount] = useState(0);

  return {
    template,
    state: {
      count,
    },
    methods: {
      increment: () => setCount(count() + 1),
      decrement: () => setCount(count() - 1),
    },
  };
});
  • components/main/index.html中:
<div>
  <h1>Count: {{ count }}</h1>
  <button @click="{{increment}}">Increment</button>
  <button @click="{{decrement}}">Decrement</button>
</div>
  • components/index.ts中:
export * from "./main";

4, 点赞

敬佩,我没有做初始化脚手架的工作,而屏幕前的你居然真的手搭了一个启动项目,了不起。

🪄 性能

1. 体积

extreme

2. 渲染性能

extreme

extreme's People

Contributors

grinzero avatar turbobot-temp avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

extreme's Issues

[BugFix(methods)] 同一函数绑定多次的场景无法覆盖到,且事件只用target来判断不合适,如果点击到子项也应该触发

推荐修复

  • 同一函数多次绑定
    template.replace(/{{(.*?)}}/g, (_, _key, start) => {
      const dom = findDomStr(start, template);
      usageDomSet.add(dom);
      return _;
    });
  • 点击到子项
  const keys = Object.keys(methodsMap);
  for (let i = 0; i < keys.length; i++) {
    const methodName = keys[i];
    const arr = methodsMap[methodName];
    const fnMap: Record<string, Function | undefined> = {};
    for (let j = 0; j < arr.length; j++) {
      const [id, func] = arr[j];
      fnMap[id] = func;
    }
    const mapKeys = Object.keys(fnMap);
    element.addEventListener(methodName, (e) => {
      const target = e.target as HTMLElement;
      if (target.id) {
        const fn = fnMap[target.id];
        if (fn) {
          fn(e);
        }
        return
      }
      
      for (let i = 0; i < mapKeys.length; i++) {
        const id = mapKeys[i];
        const dom = document.getElementById(id);
        if (!dom) continue;
        if (dom.contains(target)) {
          const fn = fnMap[id];
          if (fn) {
            fn(e);
          }
          return;
        }
      }
    });
  }

[Feature]运行时转编译时

目前的render方法中部分内容完全可以在编译时处理完:

// 第一阶段添加id的这部分
    const usageDomSet = new Set<string>();
    template.replace(/{{(.*?)}}/g, (_, _key, start) => {
      const dom = findDomStr(start, template);
      usageDomSet.add(dom);
      return _;
    });
    usageDomSet.forEach((dom) => {
      let id = "";
      if (dom.indexOf("id=") === -1) {
        id = getRandomID();
        const newDom = dom.replace(">", ` id="${id}">`);
        template = template.replace(dom, newDom);
      }
    });

同时,replace本身是具体性能损耗的,考虑编译时直接收集好数据,不要重复replace

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.