• Stars
    star
    114
  • Rank 308,031 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🎅 fre 转小程序的新思路 ♂ learn once, write anywhere.

fard logo

fard NPM version NPM downloads QQ

fre 转小程序的新思路 learn once, write anywhere.

工作日只处理 issue,节假日迭代~

Package Version About
Fard npm fard core
fard-webpack-plugin npm webpack plugin

Usage

import { useState } from 'fre'
import { h, render } from 'fard'
import './index.styl'

function App() {
  const [count, setCount] = useState(0)
  return (
    <view>
      <text>{count}</text>
      <button onClick={() => setCount(count + 1)}>+</button>
    </view>
  )
}

render(<App />)

以上,由于小程序不支持 babel 和 stylus,所以仍然需要借助 webpack

完整的 demo 用例在这里:webpack-demo

hooks

所有 fre 的 hooks API 都是支持的

hooks 相关行为都发生在 fre 层,和小程序无关,不做赘述,请移步:fre readme

api & context

fard 提供 api 对象,用来抹平各个端的差异

import { h, render, api, context } from 'fard'

function App() {
  const toJack = () => {
    api.navigateTo({
      url: '../user/user?name=jack',
    })
    console.log(context)
  }
  return (
    <view>
      <button onClick={toJack}>+</button>
    </view>
  )
}

render(<App />)

如上,api 等同于 wx、my、tt、swan,会根据环境自行更换命名空间

context 是 this 实例

setup

有时候,我们需要自定义一些方法,比如生命周期,和小程序的默认事件

此时需要用到 render 的第二个参数

const setup = {
  onReady() {
    //生命周期
    console.log('onReady……')
  },
  onShareAppMessage(res) {
    //微信自带的方法
    return {
      title: '转发',
      path: '/pages/index/index',
      success(res) {
        console.log('成功', res)
      },
    }
  },
}

render(<App />, setup)

注意,只有根组件和原生组件拥有生命周期,而内置的 fre 组件,请使用 useEffect

fard-webpack-plugin

借助 webpack,还可以顺便做一些构建的工作,如 生成 template bridge复制小程序的配置文件

fard-webpack-plugin 是 fard 唯一一个额外的插件,用来包办 webpack 相关

const FardWebpackPlugin = require('fard-webpack-plugin')

plugins: [
  new FardWebpackPlugin(),
]

template bridge

经过 fard-webpack-plugin 声称的 bridge.wxml 是用来桥接的文件,无需关注其内容

我们只需要在我们每个 page 的 wxml 引用它即可:

<fard vdom="{{vdom}}">

写死的,不用修改

原理

fard 之所以称之为新思路,是因为它不同于其他编译型框架,它的原理和 RN 类似,是比较好的跨端方案

如图:

它通过 bridge 来桥接 fre 和小程序的 vdom,是在小程序里跑 fre 而不是将 fre 编译成小程序

另外,fard 还在 setData 之前做了一层 diff/防抖/压缩 三连,性能靠谱

这里有一篇详细的文章:fard:fre 转小程序的新思路

How to contribute

Since fard use monorepo managed by lerna you should install lerna at first.

npm i -g lerna

Then install all dependencies:

lerna bootstrap

For example run these commands if you'd like to run fard-demo with Webpack:

cd packages/demo-webpack
yarn start

Contributors

License

MIT

More Repositories

1

asta

🐙 Resumable SSR framework.
JavaScript
694
star
2

eplayer

🔮 A web-component html5 video player facing future
JavaScript
269
star
3

labor

Simple implementation of Web Container
Go
188
star
4

smallapp

🎃 基于 fre 的小程序架构
JavaScript
163
star
5

homo

Lightweight Rendering engine similar to Harmony, but use Fre and quickjs
C
59
star
6

deku

⛄️ No-bundle Dev Server for ES modules with deno.
JavaScript
51
star
7

joke

🎃 Tiny Javascript engine, does not support Typescript (yet).
Rust
45
star
8

peacecss

Golang implement of postcss.
Go
43
star
9

soga

Tiny layout engine.
Rust
40
star
10

wxml

wxml parser rust implement
Rust
39
star
11

oplayer

👾 react native video player for expo
JavaScript
25
star
12

node_swc

swc node binding use wasm
Rust
23
star
13

venti

Simple Vue SSR framework
JavaScript
21
star
14

loki-admin

Admin app build by KokiUI
JavaScript
14
star
15

fremix

Remix for fre
JavaScript
13
star
16

gei

👾 Fast 1kb sprites system with webgl.
JavaScript
13
star
17

tinyMDE

1kb markdown 编辑器
JavaScript
12
star
18

picopack

📦 Simple Typescript library bundler.
TypeScript
11
star
19

dirty-check

reactivity system with dirty checking
JavaScript
10
star
20

bad-scroll

100 lines scroll for mobile UI
JavaScript
8
star
21

m3u8.js

🌀 m3u8 client using WebAssembly and Media Source Extension
JavaScript
8
star
22

frekit

Streaming rendering for fre SSR
6
star
23

smox-counter

A redux-like state management which based on New context api
JavaScript
5
star
24

color-picker

Auto pick the primary color.
JavaScript
5
star
25

uraban-typecho-themes

typecho themes
PHP
4
star
26

fplayer

flutter player for clicli.tv
4
star
27

bale

Zero configuration builder.
4
star
28

voe2

Concurrent rendering engine.
JavaScript
4
star
29

TinyScript

Tiny javascript engine writted by rust.
4
star
30

gan

👾 A web framework packaged Go native which use fasthttp and httprouter
Go
3
star
31

vue-ssr

A vue-ssr demo
JavaScript
3
star
32

js-parser

Coarse-grained JavaScript block parser.
3
star
33

hello_wasm

hello wasm
JavaScript
3
star
34

remix-client

JavaScript
3
star
35

voe-ide

A ide for mini programs
3
star
36

kiana

the moe plugin of kiana
JavaScript
2
star
37

fre-sortable-tree

2
star
38

1990

2
star
39

opration-task-queue

JavaScript
2
star
40

rexpr

JavaScript expression parser
2
star
41

juvenile

Simple CICD
Go
2
star
42

fre3

fre3 临时仓库
Rust
2
star
43

moe-comment

1kb severless comment system
2
star
44

varix

Remix for vue
2
star
45

immix

Create next immutable patch by mutating the current.
JavaScript
2
star
46

yisar

2
star
47

cplayer

Simple video player UI for clicli app.
2
star
48

browser-cjs

JavaScript
2
star
49

esbuild-module-federation

2
star
50

requestIdlePromise

A POC for requestIdleCallback used Promise
JavaScript
2
star
51

fre-jotal

2
star
52

easy-crypto

Easy crpto use WebAssembly
JavaScript
2
star
53

ukagaka

this is a ukagaka plugin
JavaScript
2
star
54

blockchain-cli

cli for blockchain
JavaScript
2
star
55

tiga

Faster taro alternative.
Go
2
star
56

acgzone-moblie

acgzone mobile app written by react native
2
star
57

smox-todo

todolist for react and smox
2
star
58

fre-server-component

1
star
59

xiuno

Xino is a lightweight PHP forum system.
1
star
60

fre-counter

A demo for Fre
TypeScript
1
star
61

super-element

JSX alternative with swiftUI like syntax.
1
star
62

webpack-vemix--manifest

1
star
63

typeditor

🍡 A markdown editor rewriting of typecho's
TypeScript
1
star
64

webview_video_player

Flutter video player with webview plugin.
1
star
65

jio

sm app build by react-native
1
star
66

fre-realword

A real world APP for Fre
1
star
67

UIwidgets

this is a simple slider
JavaScript
1
star
68

wean-demo-tuchong

A wean demo for tuchong.com
JavaScript
1
star
69

hvec

A simple javascript hvec_H265 muxer.
1
star
70

demon

A javascript framework for deno
1
star
71

danmu

danmuku for clicli
JavaScript
1
star
72

priority-scheduler

React scheduler implement.
1
star
73

snel

Html-first framework
JavaScript
1
star
74

js-basic-skill

js-basic-skill
1
star
75

gap

A simple and lightweight REST server framework.
1
star
76

duex

Another state manager make Redux use Vuex-like syntax
1
star
77

phpbb1

phpbb1 for php8
PHP
1
star