typescript test

This commit is contained in:
dntzhang 2017-04-02 09:34:41 +08:00
parent ab6718d72c
commit 3ac2fd289d
7 changed files with 405 additions and 0 deletions

183
ts/index.d.ts vendored Normal file
View File

@ -0,0 +1,183 @@
// Type definitions for Omi v1.2.2
// Project: https://github.com/AlloyTeam/omi
///////////////////////////////////////////////////////////////////////////////
// Omi module (omi.js)
///////////////////////////////////////////////////////////////////////////////
declare namespace Omi {
/*
* Create an instance of the Omi.Component .
*
* Examples:
* export default Hello extends Omi.Component{}
*/
interface ComponentOption{
server: boolean;
ignoreStoreData: boolean;
preventSelfUpdate: boolean;
selfDataFirst: boolean;
domDiffDisabled: boolean;
scopedSelfCSS: boolean
[propName: string]: any;
}
/*
* It should be noted that the installed will be executed during the instantiation, which is not shown above.
* For example, it'll executed when a component is removed and restored, or when the new component is being added.
*
*/
export class Component {
/*
* The constructor;
* When new a constructor
*/
constructor(data: any, option?: ComponentOption);
/*
* The installation. We can process the data that user pass;
* When instantiate
*/
public install?(): void;
/*
* Complete the installation. It'll trigger after HTML being inserted to the page. Please note that it'll trigger when component being removed and restored
* Instantiation and existence
*/
public installed?(): void;
/*
* Uninstall the component. It'll trigger when remove is executed.
* When destroy
*/
public uninstall?(): void;
/*
* Before update.
* When existence
*/
public beforeUpdate?(): void;
/*
* After update.
* When existence
*/
public afterUpdate? (): void;
public render(): string;
public style?(): string;
public beforeRender?(): string;
public useStore?(store: Store): void;
public updateSelf?(): void;
public update?(): void;
public setData?(data: any, update: boolean): void;
public removeChild?(indexOrChild: number | Component ): void;
public remove?(): void;
public restore?(): void;
}
export function $ (selector: string, context: any): HTMLElement;
export function $$(selector: string, context: any): Array<HTMLElement>;
export const STYLEPREFIX: string;
export const STYLESCOPEDPREFIX: string;
export const _instanceId: number;
export interface componentConstructor {
[name: string]: Component
}
export function create (tagName: string, parent: string, setting: any): void
export function createStore (option: any): Store
export const customTags: Array<string>;
/*
* Method: Omi.extendPlugin( pluginName, handler )
* pluginName is the name of the plugin.
* handler is the processor. The handler can get the dom which marked as the pluginName and the instance of the component.
* With Omi.extendPlugin, we can give the dom some ability, and can also be associated with the component instance.
* The above example is not associated with the instance, let's try it:
*/
interface ExtendPluginHandler {
(dom: HTMLElement, instance: Component): void
}
export interface plugins{
[name: string]: ExtendPluginHandler
}
export function extendPlugin(pluginName: string, handler: ExtendPluginHandler): void
export function get(name: string): void
export function getClassFromString(string: string): Component
export function getInstanceId () : number
export function getParameters(dom: HTMLElement, instance: Component, types: string): void
export interface instances {
[component: string]: Component
}
/*
* We use makeHTML to make the component to a tag which can be used in render method. Of course, Omi.makeHTML('List', List);
* can also be written in the end of List component.
*/
export function makeHTML(name: string, component: typeof Component): void
export interface mapping {
[name: string]: any
}
interface MixIndexArrayItem {
value: any;
[indexName: string]: number
}
export function mixIndex(array: Array<any>, key?: string): Array<MixIndexArrayItem>;
export function mixIndexToArray(array: Array<any>, indexName: string): void;
class Store {
constructor(isReady: boolean);
public ready<T>(readyHandle: Array<T>): void;
public addSelfView?(view: Component): void;
public addView?(view: Component): void;
public beReady?(): void;
public update?(): void;
}
/*
*
*/
interface IncrementOrOption {
increment: boolean,
store: Store,
autoStoreToData: boolean
}
export interface Style{
scoper(css: string, prefix: string): string;
addStyle(cssText: string, id: string): void;
}
export function render(component: Component, renderTo: string, incrementOrOption?: IncrementOrOption)
export const style: Style;
/*
*
*/
export function tag(name: string, ctor: typeof Component): void
}
/*
* export a module omi
*/
declare module "omi" {
export default Omi;
}

12
ts/index.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div id="wrapper"></div>
<script src="https://unpkg.com/omi@1.2.2/dist/omi.js"></script>
<script src="index.js"></script>
</body>
</html>

125
ts/index.js Normal file
View File

@ -0,0 +1,125 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 1);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {
module.exports = Omi;
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
!(__WEBPACK_AMD_DEFINE_ARRAY__ = [__webpack_require__, exports, __webpack_require__(0)], __WEBPACK_AMD_DEFINE_RESULT__ = function (require, exports, Omi) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Hello = (function (_super) {
__extends(Hello, _super);
function Hello() {
return _super !== null && _super.apply(this, arguments) || this;
}
Hello.prototype.style = function () {
return "\n h1{\n \tcursor:pointer;\n }";
};
Hello.prototype.handleClick = function (evt) {
alert(evt.target.innerHTML);
};
Hello.prototype.render = function () {
return "\n <div>\n \t<h1 onclick=\"handleClick\">Hello ,{{name}}!</h1>\n </div>";
};
return Hello;
}(Omi.Component));
Omi.makeHTML('Hello', Hello);
var App = (function (_super) {
__extends(App, _super);
function App() {
return _super !== null && _super.apply(this, arguments) || this;
}
App.prototype.render = function () {
return "\n <div>\n <Hello data-name=\"Omi\" />\n </div>";
};
return App;
}(Omi.Component));
Omi.render(new App(), "body");
}.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__),
__WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
/***/ })
/******/ ]);

15
ts/package.json Normal file
View File

@ -0,0 +1,15 @@
{
"name": "omi-webpack2-typescript",
"main": "index.js",
"version": "1.0.0",
"scripts": {
"build": "webpack"
},
"devDependencies": {
"ts-loader": "^2.0.0",
"webpack": "^2.2.0"
},
"dependencies": {
"omi": "^1.2.2"
}
}

38
ts/src/index.ts Normal file
View File

@ -0,0 +1,38 @@
import * as Omi from 'omi';
class Hello extends Omi.Component {
style () {
return `
h1{
cursor:pointer;
}`;
}
handleClick(evt){
alert(evt.target.innerHTML);
}
render() {
return `
<div>
<h1 onclick="handleClick">Hello ,{{name}}!</h1>
</div>`;
}
}
Omi.makeHTML('Hello', Hello);
class App extends Omi.Component {
render() {
return `
<div>
<Hello data-name="Omi" />
</div>`;
}
}
Omi.render(new App(),"body");

9
ts/tsconfig.json Normal file
View File

@ -0,0 +1,9 @@
{
"compilerOptions": {
"outDir": "./built/",
"sourceMap": true,
"noImplicitAny": true,
"module": "amd",
"target": "es5"
}
}

23
ts/webpack.config.js Normal file
View File

@ -0,0 +1,23 @@
'use strict';
module.exports = {
entry: './src/index.ts',
output: { filename: 'index.js' },
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: {
transpileOnly: true
}
}
]
},
resolve: {
extensions: [ '.ts', '.tsx' ]
},
externals: {
'omi': 'Omi'
}
};