Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
feat(component): add like vue's vm.$emit function
Browse files Browse the repository at this point in the history
- add vue's vm.$emit function
``` javascript
// emit props['onClick'] bind
this.$emit("Click", 1);
```
- render function add params createElement function
``` javascript
// auto bind event function this is here conmponent
render: function(props, state, content, createElement);
```
  • Loading branch information
zeromake committed Sep 8, 2017
1 parent 5a79412 commit 3a61707
Show file tree
Hide file tree
Showing 21 changed files with 348 additions and 329 deletions.
31 changes: 31 additions & 0 deletions config/rollup.base.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const rollupTypescript = require('rollup-plugin-typescript')
const uglify = require('rollup-plugin-uglify')
const { minify } = require('uglify-es')
const replace = require('rollup-plugin-replace')
const pkg = require('../package.json')
const commonjs = require('rollup-plugin-commonjs');

const isProduction = process.env.NODE_ENV === 'production'

const rollupTypescriptPlugin = rollupTypescript()
const replacePlugin = replace({
VERSION_ENV: JSON.stringify(pkg.version),
ENV: JSON.stringify(process.env.NODE_ENV)
})
const commonjsPlugin = commonjs({
include: 'node_modules/**'
})

module.exports = {
plugins: !isProduction ? [
rollupTypescriptPlugin,
replacePlugin,
commonjsPlugin
] : [
rollupTypescriptPlugin,
uglify({}, minify),
replacePlugin,
commonjsPlugin
],
sourcemap: !isProduction
}
25 changes: 0 additions & 25 deletions config/rollup.config.devtools.js

This file was deleted.

30 changes: 6 additions & 24 deletions config/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
import rollupTypescript from 'rollup-plugin-typescript'
import uglify from 'rollup-plugin-uglify'
import { minify } from 'uglify-es'
import pkg from '../package.json'
import replace from 'rollup-plugin-replace'
// import typescript from 'typescript'
const pkg = require('../package.json')
const baseConfig = require('./rollup.base.config')

const isProduction = process.env.NODE_ENV === 'production'

// set new typescript
const rollupTypescriptPlugin = rollupTypescript()
const replacePlugin = replace({
VERSION_ENV: JSON.stringify(pkg.version),
ENV: JSON.stringify(process.env.NODE_ENV)
})
export default {
const config = Object.assign({
input: 'src/zreact.ts',
name: 'zreact',
// dest: isProduction ? 'dist/zreact.min.js' : 'dist/zreact.js',
plugins: !isProduction ? [
rollupTypescriptPlugin,
replacePlugin
] : [
rollupTypescriptPlugin,
uglify({}, minify),
replacePlugin
],
sourcemap: !isProduction,
output: isProduction ? [
{
format: 'umd',
Expand All @@ -41,4 +21,6 @@ export default {
file: pkg.module
}
]
}
}, baseConfig)

export default config
14 changes: 14 additions & 0 deletions config/rollup.ie8.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const baseConfig = require('./rollup.base.config')

const config = Object.assign({
input: 'src/ie8.ts',
name: 'zreact',
output:[
{
format: 'umd',
file: 'dist/zreact.ie8.js'
}
]
}, baseConfig)

export default config
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"build": "cross-env NODE_ENV=production DEVTOOLS_ENV=production rollup -c config/rollup.config.js",
"build:devtools-run": "rollup -c config/rollup.config.devtools-run.js",
"build:dev": "cross-env DEVTOOLS_ENV=production NODE_ENV=dev rollup -c config/rollup.config.js",
"build:ie8": "cross-env DEVTOOLS_ENV=production NODE_ENV=dev rollup -c config/rollup.ie8.config.js",
"dev": "cross-env DEVTOOLS_ENV=production NODE_ENV=dev rollup -c config/rollup.config.js -w",
"test": "babel test/test.jsx -o test/test.js && node test/test.js",
"test:karma": "karma start test/karma.conf.js --single-run",
Expand Down Expand Up @@ -72,6 +73,7 @@
"rimraf": "^2.6.1",
"rollup": "^0.49.2",
"rollup-plugin-alias": "^1.3.1",
"rollup-plugin-commonjs": "^8.2.1",
"rollup-plugin-replace": "^2.0.0",
"rollup-plugin-typescript": "zeromake/rollup-plugin-typescript",
"rollup-plugin-uglify": "^2.0.1",
Expand Down
30 changes: 27 additions & 3 deletions src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ export class Component <PropsType extends IKeyValue, StateType extends IKeyValue
* 是否停用
*/
public _disable?: boolean;
/**
* 模拟vue.emit用的上下文保存
*/
public _emitComponent?: Component<any, any>;
/**
* react标准用于设置component实例
*/
Expand All @@ -127,8 +131,11 @@ export class Component <PropsType extends IKeyValue, StateType extends IKeyValue
this._dirty = true;
this.context = context;
this.props = props;
this.state = this.state || {};
this.h = h;
this.state = (this.state || {}) as StateType;
const self = this;
this.h = function _(){
return h.apply(self, Array.prototype.slice.call(arguments, 0));
} as typeof h;
}
/**
* 设置state并通过enqueueRender异步更新dom
Expand Down Expand Up @@ -176,7 +183,24 @@ export class Component <PropsType extends IKeyValue, StateType extends IKeyValue
* @param state
* @param context
*/
public render(props?: PropsType, state?: StateType, context?: IKeyValue): IVNode | void {
public render(props: PropsType, state: StateType, context: IKeyValue, createElement: typeof h): IVNode | void {
// console.error("not set render");
}
/**
* 触发props上的on开头的方法,并以_emitComponent为this
* @param eventName 事件名去除
* @param args 传递的参数
*/
public $emit(eventName: string, args: any) {
const event = this.props["on" + eventName];
if (event) {
let result;
if (this._emitComponent) {
result = event.call(this._emitComponent, args);
} else {
result = event(args);
}
return result;
}
}
}
2 changes: 1 addition & 1 deletion src/h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function h(this: Component<IKeyValue, IKeyValue> | undefined | void | nul
}
}
const self = this;
const component: Component<IKeyValue, IKeyValue> | undefined = self != null && self.setState ? self : undefined;
const component: Component<IKeyValue, IKeyValue> | undefined = self && self.setState ? self : undefined;
const p: IVNode = {
// 设置属性
attributes: attributes == null ? undefined : attributes,
Expand Down
5 changes: 5 additions & 0 deletions src/ie8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import "./polyfill";

import zreact from "./zreact";

export default zreact;
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IVNode } from "./vnode";
import { Component } from "component";
import { Component } from "./component";
import { IKeyValue } from "./types";

const options: {
Expand Down
121 changes: 0 additions & 121 deletions src/polyfill.js

This file was deleted.

0 comments on commit 3a61707

Please sign in to comment.