Sfoglia il codice sorgente

webpack as bundle tool

Paul 5 anni fa
parent
commit
1ec05cd4a8
8 ha cambiato i file con 1514 aggiunte e 2051 eliminazioni
  1. 9
    4
      package.json
  2. 0
    6
      public/index.html
  3. 3
    3
      src/callback.ts
  4. 3
    3
      src/example/main.ts
  5. 24
    31
      src/index.ts
  6. 2
    1
      tsconfig.json
  7. 35
    0
      webpack.config.js
  8. 1438
    2003
      yarn.lock

+ 9
- 4
package.json Vedi File

@@ -4,20 +4,25 @@
4 4
   "description": "",
5 5
   "main": "index.js",
6 6
   "scripts": {
7
+    "start": "webpack-dev-server  --mode development",
7 8
     "test": "jest",
8
-    "start": "parcel public/index.html"
9
+    "build": "webpack --config webpack.config.js"
9 10
   },
10 11
   "author": "",
11 12
   "license": "ISC",
12 13
   "dependencies": {
13 14
     "crypto-js": "^3.1.9-1",
14
-    "node-int64": "^0.4.0",
15
-    "parcel-bundler": "^1.12.3"
15
+    "node-int64": "^0.4.0"
16 16
   },
17 17
   "devDependencies": {
18 18
     "@types/jest": "^24.0.11",
19
+    "html-webpack-plugin": "^3.2.0",
19 20
     "jest": "^24.5.0",
20 21
     "ts-jest": "^24.0.0",
21
-    "typescript": "^3.3.4000"
22
+    "ts-loader": "^6.0.0",
23
+    "typescript": "^3.3.4000",
24
+    "webpack": "^4.30.0",
25
+    "webpack-cli": "^3.3.2",
26
+    "webpack-dev-server": "^3.3.1"
22 27
   }
23 28
 }

+ 0
- 6
public/index.html Vedi File

@@ -1,6 +0,0 @@
1
-<!DOCTYPE html>
2
-<htm>
3
-  <head>
4
-    <script src="../src/example/main.ts" type="text/javascript"></script>
5
-  </head>
6
-</htm>

+ 3
- 3
src/callback.ts Vedi File

@@ -1,7 +1,7 @@
1 1
 export interface readyStateCallback {
2
-  onopen(ev: Event);
3
-  onerror(ev: Event);
4
-  onclose(ev: Event);
2
+  onOpen(ev: Event);
3
+  onError(ev: Event);
4
+  onClose(ev: Event);
5 5
 }
6 6
 
7 7
 export interface callback {

+ 3
- 3
src/example/main.ts Vedi File

@@ -1,5 +1,5 @@
1
-
2 1
 import { Client } from "../index";
3
-const client = new Client("ws://127.0.0.1:8081", null)
4 2
 
5
-console.log("test", "a")
3
+const client = new Client("ws://127.0.0.1:8081", null)
4
+console.log(client.setRequestProperty("name", "stri"))
5
+console.log(client.getRequestProperty("name"))

+ 24
- 31
src/index.ts Vedi File

@@ -7,27 +7,20 @@ import { Utils } from './utils';
7 7
  * Client ws client, 单例模式, 负责维护连接
8 8
  */
9 9
 export class Client {
10
-  public callback: callback;
11
-  public requestHeader: string;
12
-  public responseHeader: string;
13
-  public maxPayload: number;
14
-  public url: string;
15
-  public reconnectTimes: number;
16
-  public reconnectLock: boolean;
17
-  public socket: WebSocket;
18
-  public readyStateCallback: readyStateCallback;
10
+  private requestCallback: callback;
11
+  private requestHeader: string;
12
+  private responseHeader: string;
13
+  private maxPayload: number;
14
+  private url: string;
15
+  private reconnectTimes: number;
16
+  private reconnectLock: boolean;
17
+  private socket: WebSocket;
18
+  private readyStateCallback: readyStateCallback;
19 19
 
20 20
   constructor(url: string, readyStateCallback: readyStateCallback) {
21
-    if (!('WebSocket' in window)) {
22
-      return;
23
-    }
24
-
25
-    this.requestHeader = '';
26 21
     this.maxPayload = constant.MAX_PAYLOAD;
27 22
     this.url = url;
28 23
     this.readyStateCallback = readyStateCallback;
29
-    this.reconnectTimes = 0;
30
-    this.reconnectLock = false;
31 24
 
32 25
     this.socket = this.connect();
33 26
   }
@@ -100,7 +93,7 @@ export class Client {
100 93
     let _this = this;
101 94
     let sequence = new Date().getTime();
102 95
     let listener = Utils.crc32(operator) + sequence;
103
-    this.callback[listener] = function(data) {
96
+    this.requestCallback[listener] = function(data) {
104 97
       let code = _this.getResponseProperty('code');
105 98
       if (typeof code !== 'undefined') {
106 99
         let message = _this.getResponseProperty('message');
@@ -126,7 +119,7 @@ export class Client {
126 119
         callback.onEnd();
127 120
       }
128 121
 
129
-      delete _this.callback[listener];
122
+      delete _this.requestCallback[listener];
130 123
     };
131 124
 
132 125
     const p = new Packet();
@@ -147,12 +140,12 @@ export class Client {
147 140
 
148 141
   // 添加消息监听
149 142
   addMessageListener(operator, listener) {
150
-    this.callback[Utils.crc32(operator)] = listener;
143
+    this.requestCallback[Utils.crc32(operator)] = listener;
151 144
   }
152 145
 
153 146
   // 移除消息监听
154 147
   removeMessageListener(operator) {
155
-    delete this.callback[Utils.crc32(operator)];
148
+    delete this.requestCallback[Utils.crc32(operator)];
156 149
   }
157 150
 
158 151
   // 获取socket的链接状态
@@ -217,10 +210,10 @@ export class Client {
217 210
       console.info('websocket connected');
218 211
       _this.reconnectTimes = 0;
219 212
       if (
220
-        readyStateCallback.hasOwnProperty('onopen') &&
221
-        typeof readyStateCallback.onopen === 'function'
213
+        readyStateCallback.hasOwnProperty('onOpen') &&
214
+        typeof readyStateCallback.onOpen === 'function'
222 215
       ) {
223
-        readyStateCallback.onopen(ev);
216
+        readyStateCallback.onOpen(ev);
224 217
       }
225 218
     };
226 219
 
@@ -228,10 +221,10 @@ export class Client {
228 221
       console.info('websocket disconnected');
229 222
       _this.reconnect();
230 223
       if (
231
-        readyStateCallback.hasOwnProperty('onclose') &&
232
-        typeof readyStateCallback.onclose === 'function'
224
+        readyStateCallback.hasOwnProperty('onClose') &&
225
+        typeof readyStateCallback.onClose === 'function'
233 226
       ) {
234
-        readyStateCallback.onclose(ev);
227
+        readyStateCallback.onClose(ev);
235 228
       }
236 229
     };
237 230
 
@@ -239,10 +232,10 @@ export class Client {
239 232
       console.info('websocket error disconnected');
240 233
       _this.reconnect();
241 234
       if (
242
-        readyStateCallback.hasOwnProperty('onerror') &&
243
-        typeof readyStateCallback.onerror === 'function'
235
+        readyStateCallback.hasOwnProperty('onError') &&
236
+        typeof readyStateCallback.onError === 'function'
244 237
       ) {
245
-        readyStateCallback.onerror(ev);
238
+        readyStateCallback.onError(ev);
246 239
       }
247 240
     };
248 241
 
@@ -259,12 +252,12 @@ export class Client {
259 252
             }
260 253
 
261 254
             let operator = Number(packet.operator) + Number(packet.sequence);
262
-            if (_this.callback.hasOwnProperty(operator)) {
255
+            if (_this.requestCallback.hasOwnProperty(operator)) {
263 256
               if (packet.body === '') {
264 257
                 packet.body = '{}';
265 258
               }
266 259
               _this.responseHeader = packet.header;
267
-              _this.callback[operator](JSON.parse(packet.body));
260
+              _this.requestCallback[operator](JSON.parse(packet.body));
268 261
             }
269 262
             if (operator !== 0 && packet.body !== 'null') {
270 263
               console.info('receive data', packet.body);

+ 2
- 1
tsconfig.json Vedi File

@@ -3,11 +3,12 @@
3 3
   "compilerOptions": {
4 4
     "target": "es6",
5 5
     "moduleResolution": "node",
6
+    "strictNullChecks": false,
6 7
     "alwaysStrict": true,
7 8
     "sourceMap": true,
8 9
     "declaration": true,
9 10
     "rootDir": "src",
10
-    "outDir": "dist",
11
+    "outDir": "dist/",
11 12
     "declarationDir": "dist/types"
12 13
   },
13 14
   "include": ["src/**/*"]

+ 35
- 0
webpack.config.js Vedi File

@@ -0,0 +1,35 @@
1
+const path = require('path');
2
+const HtmlWebpackPlugin = require('html-webpack-plugin');
3
+
4
+module.exports = {
5
+    mode: 'development',
6
+    entry: './src/example/main.ts',
7
+    devtool: 'inline-source-map',
8
+    devServer: {
9
+      contentBase: './dist',
10
+      compress: true,
11
+      hot: true,
12
+    },
13
+    plugins: [
14
+      new HtmlWebpackPlugin({
15
+          title: 'index'
16
+      })
17
+    ],
18
+    output: {
19
+        filename: 'main.js',
20
+        path: path.resolve(__dirname, 'dist')
21
+    },
22
+    module: {
23
+        rules: [{
24
+            test: /\.ts$/,
25
+            use: "ts-loader"
26
+        }]
27
+    },
28
+    resolve: {
29
+        extensions: [
30
+            '.ts',
31
+            '.js',
32
+            '.tsx'
33
+        ]
34
+    }
35
+};

+ 1438
- 2003
yarn.lock
File diff suppressed because it is too large
Vedi File