Omi 框架是腾讯开源的下一代前端框架,提供桌面、移动和小程序总体解决方案(One framework. Mobile & Desktop & Mini Program), Omip 是 Omi 团队开发的跨端开发工具集,支持小程序和 H5 SPA,最新的 omip 已经适配了 h5,以下方新增的两条命令:css
npm i omi-cli -g
omi init-p my-app
cd my-app
npm start //开发小程序
npm run dev:h5 //开发 h5
npm run build:h5 //发布 h5
复制代码
node 版本要求 >= 8前端
也支持一条命令
npx omi-cli init-p my-app
(npm v5.2.0+)node
固然也支持 TypeScript:webpack
omi init-p-ts my-app
复制代码
TypeScript 的其余命令和上面同样,也支持小程序和 h5 SPA 开发。git
特性包括:github
Omip 不只能够一键生成小程序,还能一键生成 h5 SPA。怎么作到的?下面来一一列举难点,逐个击破。web
小程序扩展尺寸单位 rpx(responsive pixel): 能够根据屏幕宽度进行自适应。规定屏幕宽为750rpx。如在 iPhone6 上,屏幕宽度为375px,共有750个物理像素,则750rpx = 375px = 750物理像素,1rpx = 0.5px = 1物理像素。npm
这个特性大受好评,制做响应式网站很是有用。由于浏览器是不支持 rpx 单位,因此须要运行时转换,恰好 omi 内置了这个函数:编程
function rpx(str) {
return str.replace(/([1-9]\d*|0)(\.\d*)*rpx/g, (a, b) => {
return (window.innerWidth * Number(b)) / 750 + 'px'
})
}
复制代码
从 rpx 源码能够看到,须要运行时转换 rpx,而非编译时!由于只有运行时能拿到 屏幕宽度,omi 早期版本已经支持运行时的 rpx 转换:小程序
import { WeElement, define, rpx } from 'omi'
define('my-ele', class extends WeElement {
static css = rpx(`div { font-size: 375rpx }`)
render() {
return (
<div>my ele</div>
)
}
})
复制代码
小程序 Shadow tree 与 omi 有一点点不同,omi 是从根开始 shadow root,而小程序是从自定义组件开始,omio 则没有 shadow root。
Omi | Omio | 小程序 | |
---|---|---|---|
Shadow DOM | 从根节点开始 | 无 | 从自定义组件开始 |
Scoped CSS | 从根节点开始局部做用域,浏览器 scoped | 从根节点开始局部做用域(运行时 scoped) | 自定义组件局部做用域 |
因此,app.css 须要污染到 page 里的 WXML/JSX,但在 omi 和 omio 中样式都是隔离的, 须要怎么作才能突破隔离?先看 app.js 源码:
import './app.css' //注意这行!!!
import './pages/index/index'
import { render, WeElement, define } from 'omi'
define('my-app', class extends WeElement {
config = {
pages: [
'pages/index/index',
'pages/list/index',
'pages/detail/index',
'pages/logs/index'
],
window: {
backgroundTextStyle: 'light',
navigationBarBackgroundColor: '#fff',
navigationBarTitleText: 'WeChat',
navigationBarTextStyle: 'black'
}
复制代码
上面是使用 omip 开发小程序的入口 js 文件,也是 webpack 编译的入口文件,在 cli 进行语法树分析的时候,能够拿到 import 的各个细节,而后作一些变换处理,好比下面 ImportDeclaration(即 import 语句) 的处理:
traverse(ast, {
ImportDeclaration: {
enter (astPath) {
const node = astPath.node
const source = node.source
const specifiers = node.specifiers
let value = source.value
//当 app.js 里 import 的文件是以 .css 结尾的时候
if(value.endsWith('.css')){
//读取对应 js 目录的 css 文件,移除 css 当中的注释,保存到 appCSS 变量中
appCSS = fs.readFileSync(filePath.replace('.js','.css'), 'utf-8').replace(/\/\*[^*]*\*+([^/][^*]*\*+)*\//g, '')
//移除这里条 import 语句
astPath.remove()
return
}
复制代码
获得了 appCSS 以后,想办法注入到全部 page 当中:
traverse(ast, {
ImportDeclaration: {
enter (astPath) {
const node = astPath.node
const source = node.source
let value = source.value
const specifiers = node.specifiers
//当 import 的文件是以 .css 结尾的时候
if(value.endsWith('.css')){
//读取对应 js 目录的 css 文件,移除 css 当中的注释,保存到 css 变量中
let css = fs.readFileSync(filePath.replace('.js','.css'), 'utf-8').replace(/\/\*[^*]*\*+([^/][^*]*\*+)*\//g, '')
//page 注入 appCSS
if(filePath.indexOf('/src/pages/') !== -1||filePath.indexOf('\\src\\pages\\') !== -1){
css = appCSS + css
}
//把 import 语句替换成 const ___css = Omi.rpx(.....) 的形式!
astPath.replaceWith(t.variableDeclaration('const',[t.variableDeclarator(t.identifier(`___css`),t.callExpression(t.identifier('Omi.rpx'),[t.stringLiteral(css)]),)]))
return
}
...
复制代码
这就够了吗?不够!由于 ___css 并无使用到,须要注入到 WeElement Class 的静态属性 css 上,继续 ast transformation:
const programExitVisitor = {
ClassBody: {
exit (astPath) {
//注入静态属性 const css = ___css
astPath.unshiftContainer('body', t.classProperty(
t.identifier('static css'),
t.identifier('___css')
))
}
}
}
复制代码
编译出得 page 长这个样子:
import { WeElement, define } from "../../libs/omip-h5/omi.esm";
const ___css = Omi.rpx("\n.container {\n height: 100%;\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: space-between;\n padding: 200rpx 0;\n box-sizing: border-box;\n} \n\n.userinfo {\n display: flex;\n flex-direction: column;\n align-items: center;\n}\n\n.userinfo-avatar {\n width: 128rpx;\n height: 128rpx;\n margin: 20rpx;\n border-radius: 50%;\n}\n\n.userinfo-nickname {\n color: #aaa;\n}\n\n.usermotto {\n margin-top: 200px;\n}");
const app = getApp();
define('page-index', class extends WeElement {
static css = ___css;
data = {
motto: 'Hello Omip',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
...
...
复制代码
大功告成!
因为小程序里的一些标签在浏览器中不可以识别,好比浏览器不识别 view、text 等标签,须要转换成浏览器识别的标签,因此这里列了一个映射表:
const mapTag = {
'view': 'div',
'picker': 'select',
'image': 'img',
'navigator': 'a',
'text': 'span'
}
const getNodeName = function(name){
if(mapTag[name]) return mapTag[name]
return name
}
复制代码
在 h
函数建立虚拟 dom 的时候进行 getNodeName
:
function h(nodeName, attributes) {
...
...
var p = new VNode();
p.nodeName = getNodeName(nodeName);
p.children = children;
p.attributes = attributes == null ? undefined : attributes;
p.key = attributes == null ? undefined : attributes.key;
...
...
return p;
}
复制代码
这里还有遗留问题,好比内置的一些原生组件如:
这些组件若是你须要开发 h5,就别用上面这些组件。若是必定要使用上面的组件,那么请使用 omi 先实现上面的组件。
const map = require('./tag-mapping')
const css = require('css')
const cssWhat = require('css-what')
const cssStringify = require('./css-stringify')
function compileWxss(str) {
let obj = css.parse(str)
obj.stylesheet.rules.forEach(rule => {
rule.selectors && rule.selectors.forEach((selector, index) => {
let sltObjs = cssWhat(selector)
sltObjs.forEach(sltObj => {
sltObj.forEach(item => {
if (item.type == 'tag') {
item.name = map(item.name)
}
})
})
rule.selectors[index] = cssStringify(sltObjs)
})
})
return css.stringify(obj)
}
复制代码
转换前:
.abc view {
color: red;
}
复制代码
转换后
.abc div {
color: red;
}
复制代码
这里须要注意的是,不是全部 api 都能适配,只能适配一部分:
wx | web |
---|---|
wx.request | XMLHttpRequest |
界面 api(confirm、loaing、toast等) | 实现对应的omi组件 |
数据存储 api | localStorage |
wx 特有的 api 还包括一些特有的生命周期函数,如:
这是 wx 里 Page 里的生命周期,而 omi 是不包含的。这里须要在 router 的回调函数中进行主动调用。具体怎么出发且看路由管理。
先看 cli 编译出来的 app.js 路由部分:
render() {
return <o-router mode={"hash"} publicPath={"/"} routes={[{
path: '/pages/index/index',
componentLoader: () => import( /* webpackChunkName: "index_index" */'./pages/index/index'),
isIndex: true
}, {
path: '/pages/list/index',
componentLoader: () => import( /* webpackChunkName: "list_index" */'./pages/list/index'),
isIndex: false
}, {
path: '/pages/detail/index',
componentLoader: () => import( /* webpackChunkName: "detail_index" */'./pages/detail/index'),
isIndex: false
}, {
path: '/pages/logs/index',
componentLoader: () => import( /* webpackChunkName: "logs_index" */'./pages/logs/index'),
isIndex: false
}]} customRoutes={{}} basename={"/"} />;
}
});
render(<my-app />, '#app');
复制代码
4个页面各自作了分包,这样能够加快首屏节省带宽按需加载。接下来看 <o-router />
的实现:
import { WeElement, define, render } from "../omip-h5/omi.esm";
import 'omi-router';
let currentPage = null;
let stackList = [];
define('o-router', class extends WeElement {
_firstTime = true;
installed() {
...
...
}
});
export function routeUpdate(vnode, selector, byNative, root) {
...
...
}
window.onscroll = function () {
...
...
};
复制代码
具体实现细节能够去看 o-router 源码,主要实现了下面一些功能:
Omi 相关任何问题疑问反馈意见欢迎进群交流。