├── .gitignore ├── src ├── app.wxss ├── pages │ └── index │ │ ├── index.wxml │ │ ├── index.wxss │ │ └── index.ts ├── app.ts ├── app.json └── utils │ └── http-client.ts ├── CHANGELOG.md ├── tsconfig.json ├── package.json ├── gulpfile.js ├── LICENSE ├── README.md └── tslint.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | -------------------------------------------------------------------------------- /src/app.wxss: -------------------------------------------------------------------------------- 1 | view { 2 | font-family: 'Microsoft YaHei' 3 | } 4 | -------------------------------------------------------------------------------- /src/pages/index/index.wxml: -------------------------------------------------------------------------------- 1 | 2 | {{message}} 3 | -------------------------------------------------------------------------------- /src/pages/index/index.wxss: -------------------------------------------------------------------------------- 1 | page { 2 | background-color: #F8F8F8; 3 | height: 100%; 4 | } 5 | 6 | .index-page { 7 | height: 100%; 8 | overflow: hidden; 9 | } -------------------------------------------------------------------------------- /src/pages/index/index.ts: -------------------------------------------------------------------------------- 1 | import { HttpClient } from '../../utils/http-client'; 2 | 3 | let indexPage: WeApp.Page; 4 | 5 | Page({ 6 | data: { 7 | message: 'Hello World', 8 | }, 9 | onLoad: function (): void { 10 | indexPage = this as WeApp.Page; 11 | }, 12 | }); 13 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 2017.07.13 2 | * prefer npm installation locally 3 | * abort using [`typings`](https://github.com/typings/typings) 4 | * install/use the weapp declaration file under `node_modules` 5 | * update tslint.json 6 | * adjust sample codes 7 | 8 | ## 2017.05.27 9 | * remove es6-promise.min.js since WeApp supports `promise` now 10 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "target": "es2015", 5 | "noImplicitAny": false, 6 | "sourceMap": false, 7 | // "strict": true, 8 | "outDir": "dist" 9 | }, 10 | "include": [ 11 | "src/**/*.ts", 12 | "node_modules/typed-we-app" 13 | ] 14 | } -------------------------------------------------------------------------------- /src/app.ts: -------------------------------------------------------------------------------- 1 | App({ 2 | onLaunch: async () => { 3 | console.log('on launch', new Date()); 4 | await sleep(5000); 5 | console.log('after await sleep', new Date()); 6 | }, 7 | }); 8 | 9 | async function sleep(timeout: number): Promise { 10 | return new Promise((resolve, reject) => { 11 | setTimeout(resolve, timeout); 12 | }); 13 | } 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "we-app-typescript", 3 | "version": "1.0.0", 4 | "description": "A WeApp project template in TypeScript using VS Code", 5 | "keywords": [], 6 | "author": "Emeryao", 7 | "license": "MIT", 8 | "devDependencies": { 9 | "gulp": "^3.9.1", 10 | "tslint": "^5.5.0", 11 | "typed-we-app": "^1.4.0-update.1", 12 | "typescript": "^2.4.1" 13 | } 14 | } -------------------------------------------------------------------------------- /src/app.json: -------------------------------------------------------------------------------- 1 | { 2 | "pages": [ 3 | "pages/index/index" 4 | ], 5 | "window": { 6 | "backgroundTextStyle": "dark", 7 | "navigationBarBackgroundColor": "#E909F9", 8 | "navigationBarTitleText": "WeApp TypeScript", 9 | "navigationBarTextStyle": "white", 10 | "enablePullDownRefresh": false, 11 | "backgroundColor": "#FFF" 12 | }, 13 | "debug": false 14 | } -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const gulp = require('gulp'); 3 | const execSync = require('child_process').execSync; 4 | 5 | gulp.task('clean', () => { 6 | let res = fs.existsSync('dist') 7 | if (res) { 8 | execSync('powershell.exe remove-item dist -Recurse', { 9 | stdio: 'inherit' 10 | }); 11 | } 12 | }); 13 | 14 | gulp.task('build', () => { 15 | execSync('tsc', { 16 | stdio: 'inherit' 17 | }); 18 | }); 19 | 20 | gulp.task('move', () => { 21 | gulp.src('src/**/*.wxml') 22 | .pipe(gulp.dest('dist')); 23 | 24 | gulp.src('src/**/*.wxss') 25 | .pipe(gulp.dest('dist')); 26 | 27 | gulp.src('src/**/*.json') 28 | .pipe(gulp.dest('dist')); 29 | 30 | gulp.src('src/img/*.*') 31 | .pipe(gulp.dest('dist/img')); 32 | 33 | gulp.src('src/**/*.js') 34 | .pipe(gulp.dest('dist')); 35 | }); 36 | 37 | gulp.task('default', ['build', 'move']); -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Emeryao 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WeApp TypeScript 2 | A WeApp project template in [TypeScript](http://www.typescriptlang.org/) using [VS Code](http://code.visualstudio.com/) 3 | [VS Code](http://code.visualstudio.com/)下基于[TypeScript](http://www.typescriptlang.org/)的微信小程序项目模板 4 | 5 | ## About / 关于 6 | This is a WeApp project template using TypeScript 7 | 使用TypeScript的微信小程序项目模板 8 | 9 | ## Usage / 使用 10 | * Install dependencies with command 11 | ```batch 12 | npm install 13 | ``` 14 | * Build the project with command 15 | ```batch 16 | gulp 17 | ``` 18 | 19 | * All files are built into a **dist** folder so it is the **`dist`** folder which will be referenced in Wexin dev tool for a WeApp instead of the ~~project folder~~ or the folder of ~~`src`~~ 20 | * 所有文件都被编译到 **dist** 文件夹下 所以在微信开发者工具中应该引用 **`dist`** 文件夹 而不是 ~~项目文件夹~~ 或者 ~~`src`文件夹~~ 21 | 22 | * These VS Code extensions is recommeded to be installed: 23 | * 推荐安装下列VS Code插件: 24 | * [WeApp Snippets](https://marketplace.visualstudio.com/items?itemName=emeryao.we-app-vscode) 25 | * [TSLint](https://marketplace.visualstudio.com/items?itemName=eg2.tslint) 26 | 27 | * It is **strongly recommeded** to get the TypeScript declaration file for WeApp API: 28 | * 墙裂推荐 29 | * With [`npm`](https://www.npmjs.com/) installed (which is already stored in package.json) 30 | 31 | ```batch 32 | npm install typed-we-app --save-dev 33 | ``` 34 | 35 | * With [`typings`](https://github.com/typings/typings) installed 36 | 37 | ```batch 38 | typings install github:Emeryao/typed-we-app -SG 39 | ``` 40 | 41 | ## Last Update 42 | `2017.07.13` -------------------------------------------------------------------------------- /src/utils/http-client.ts: -------------------------------------------------------------------------------- 1 | export class HttpClient { 2 | public static async getAsync(url: string): Promise { 3 | return new Promise((resolve, reject) => { 4 | if (!url) { 5 | resolve(null); 6 | } 7 | let param: WeApp.RequestParam = { 8 | url: url, 9 | header: { 10 | 'Content-Type': 'application/json', 11 | }, 12 | method: 'GET', 13 | success: res => { 14 | if (res) { 15 | if (res.statusCode == 200) { 16 | resolve(res.data as T); 17 | } else { 18 | console.log('HTTP ERROR: ', res.statusCode, res.errMsg, res.data); 19 | resolve(null); 20 | } 21 | } 22 | }, 23 | }; 24 | wx.request(param); 25 | }); 26 | } 27 | 28 | public static async postAsync(url: string, body: string): Promise { 29 | return new Promise((resolve, reject) => { 30 | if (!url) { 31 | resolve(null); 32 | } 33 | let param: WeApp.RequestParam = { 34 | url: url, 35 | header: { 36 | 'Content-Type': 'application/json', 37 | }, 38 | data: body, 39 | method: 'POST', 40 | success: res => { 41 | if (res) { 42 | if (res.statusCode == 200) { 43 | resolve(res.data as T); 44 | } else { 45 | console.log('HTTP ERROR: ', res.statusCode, res.errMsg, res.data); 46 | resolve(null); 47 | } 48 | } 49 | }, 50 | }; 51 | wx.request(param); 52 | 53 | }); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | // TypeScript Specific 4 | "adjacent-overload-signatures": true, 5 | "ban-types": [ 6 | false, 7 | [ 8 | "Object", 9 | "Use {} instead." 10 | ], 11 | [ 12 | "String" 13 | ] 14 | ], 15 | "member-access": [ 16 | true, 17 | "check-accessor", 18 | "check-constructor" 19 | ], 20 | "member-ordering": [ 21 | false, 22 | { 23 | "order": [ 24 | "private-instance-field" 25 | ] 26 | } 27 | ], 28 | "no-any": true, 29 | "no-empty-interface": true, 30 | "no-inferrable-types": false, 31 | "no-import-side-effect": true, 32 | "no-internal-module": true, 33 | "no-magic-numbers": false, 34 | "no-namespace": [ 35 | true, 36 | "allow-declarations" 37 | ], 38 | "no-non-null-assertion": false, 39 | "no-reference": true, 40 | "no-unnecessary-type-assertion": true, 41 | "no-var-requires": true, 42 | "only-arrow-functions": [ 43 | true, 44 | "allow-declarations", 45 | "allow-named-functions" 46 | ], 47 | "prefer-for-of": false, 48 | "promise-function-async": true, 49 | "typedef": [ 50 | true, 51 | "call-signature", 52 | "parameter", 53 | "property-declaration", 54 | "variable-declaration", 55 | "member-variable-declaration" 56 | ], 57 | "typedef-whitespace": [ 58 | true, 59 | { 60 | "call-signature": "nospace", 61 | "index-signature": "nospace", 62 | "parameter": "nospace", 63 | "property-declaration": "nospace", 64 | "variable-declaration": "nospace" 65 | }, 66 | { 67 | "call-signature": "onespace", 68 | "index-signature": "onespace", 69 | "parameter": "onespace", 70 | "property-declaration": "onespace", 71 | "variable-declaration": "onespace" 72 | } 73 | ], 74 | "unified-signatures": true, 75 | // Functionality 76 | "await-promise": true, 77 | "ban": [ 78 | false, 79 | [ 80 | "eval" 81 | ] 82 | ], 83 | "curly": true, 84 | "forin": true, 85 | "import-blacklist": false, 86 | "label-position": true, 87 | "no-arg": true, 88 | "no-bitwise": false, 89 | "no-conditional-assignment": true, 90 | "no-console": [ 91 | true, 92 | "error" 93 | ], 94 | "no-construct": true, 95 | "no-debugger": true, 96 | "no-duplicate-super": true, 97 | "no-duplicate-variable": true, 98 | "no-empty": true, 99 | "no-eval": true, 100 | "no-floating-promises": true, 101 | "no-for-in-array": false, 102 | "no-inferred-empty-object-type": true, 103 | "no-invalid-template-strings": true, 104 | "no-invalid-this": [ 105 | false, 106 | "check-function-in-method" 107 | ], 108 | "no-misused-new": true, 109 | "no-null-keyword": false, 110 | "no-object-literal-type-assertion": true, 111 | "no-shadowed-variable": true, 112 | "no-sparse-arrays": true, 113 | "no-string-literal": false, 114 | "no-string-throw": true, 115 | "no-switch-case-fall-through": false, 116 | "no-unbound-method": [ 117 | true, 118 | "ignore-static" 119 | ], 120 | "no-unsafe-any": true, 121 | "no-unsafe-finally": true, 122 | "no-unused-expression": true, 123 | "no-unused-variable": [ 124 | true, 125 | "check-parameters" 126 | ], 127 | "no-use-before-declare": true, 128 | "no-var-keyword": true, 129 | "no-void-expression": true, 130 | "prefer-conditional-expression": true, 131 | "prefer-object-spread": true, 132 | "radix": true, 133 | "restrict-plus-operands": true, 134 | "strict-boolean-expressions": [ 135 | true, 136 | "allow-null-union", 137 | "allow-undefined-union", 138 | "allow-string", 139 | "allow-number" 140 | ], 141 | "strict-type-predicates": true, 142 | "switch-default": true, 143 | "triple-equals": false, 144 | "typeof-compare": true, 145 | "use-default-type-parameter": true, 146 | "use-isnan": true, 147 | // Maintainability 148 | "cyclomatic-complexity": [ 149 | true, 150 | 30 151 | ], 152 | "deprecation": true, 153 | "eofline": true, 154 | "indent": [ 155 | true, 156 | "spaces" 157 | ], 158 | "linebreak-style": [ 159 | false, 160 | "CRLF" 161 | ], 162 | "max-classes-per-file": [ 163 | false, 164 | 1 165 | ], 166 | "max-file-line-count": [ 167 | false, 168 | 300 169 | ], 170 | "max-line-length": [ 171 | false, 172 | 120 173 | ], 174 | "no-default-export": true, 175 | "no-mergeable-namespace": true, 176 | "no-require-imports": true, 177 | "object-literal-sort-keys": false, 178 | "prefer-const": [ 179 | false, 180 | { 181 | "destructuring": "all" 182 | } 183 | ], 184 | "trailing-comma": [ 185 | true, 186 | { 187 | "multiline": "always", 188 | "singleline": "never" 189 | } 190 | ], 191 | // Style 192 | "align": [ 193 | true, 194 | "parameters", 195 | "statements" 196 | ], 197 | "array-type": [ 198 | true, 199 | "generic" 200 | ], 201 | "arrow-parens": [ 202 | true, 203 | "ban-single-arg-parens" 204 | ], 205 | "arrow-return-shorthand": true, 206 | "binary-expression-operand-order": true, 207 | "callable-types": true, 208 | "class-name": true, 209 | "comment-format": [ 210 | true, 211 | "check-space" 212 | ], 213 | "completed-docs": [ 214 | false, 215 | "classes", 216 | "functions" 217 | ], 218 | "encoding": true, 219 | "file-header": [ 220 | false, 221 | "Copyright \\d{4}" 222 | ], 223 | "import-spacing": true, 224 | "interface-name": [ 225 | true, 226 | "always-prefix" 227 | ], 228 | "interface-over-type-literal": true, 229 | "jsdoc-format": false, 230 | "match-default-export-name": true, 231 | "newline-before-return": true, 232 | "new-parens": true, 233 | "no-angle-bracket-type-assertion": true, 234 | "no-boolean-literal-compare": true, 235 | "no-consecutive-blank-lines": true, 236 | "no-irregular-whitespace": true, 237 | "no-parameter-properties": false, 238 | "no-reference-import": true, 239 | "no-trailing-whitespace": true, 240 | "no-unnecessary-callback-wrapper": true, 241 | "no-unnecessary-initializer": true, 242 | "number-literal-format": true, 243 | "object-literal-key-quotes": [ 244 | true, 245 | "consistent" 246 | ], 247 | "object-literal-shorthand": false, 248 | "one-line": [ 249 | true, 250 | "check-open-brace", 251 | "check-catch", 252 | "check-else", 253 | "check-whitespace" 254 | ], 255 | "one-variable-per-declaration": [ 256 | true, 257 | "ignore-for-loop" 258 | ], 259 | "ordered-imports": [ 260 | false, 261 | { 262 | "import-sources-order": "lowercase-last", 263 | "named-imports-order": "lowercase-first" 264 | } 265 | ], 266 | "prefer-function-over-method": [ 267 | true, 268 | "allow-public", 269 | "allow-protected" 270 | ], 271 | "prefer-method-signature": true, 272 | "prefer-switch": [ 273 | true, 274 | { 275 | "min-cases": 3 276 | } 277 | ], 278 | "prefer-template": [ 279 | true, 280 | "allow-single-concat" 281 | ], 282 | "quotemark": [ 283 | true, 284 | "single" 285 | ], 286 | "return-undefined": true, 287 | "semicolon": [ 288 | true, 289 | "always" 290 | ], 291 | "space-before-function-paren": [ 292 | true, 293 | { 294 | "anonymous": "always", 295 | "named": "never", 296 | "asyncArrow": "always" 297 | } 298 | ], 299 | "switch-final-break": [ 300 | true, 301 | "always" 302 | ], 303 | "type-literal-delimiter": true, 304 | "variable-name": [ 305 | true, 306 | "ban-keywords", 307 | "check-format", 308 | "allow-leading-underscore" 309 | ], 310 | "whitespace": [ 311 | true, 312 | "check-branch", 313 | "check-decl", 314 | "check-operator", 315 | "check-separator", 316 | "check-type" 317 | ] 318 | } 319 | } --------------------------------------------------------------------------------