From de845038d54def18101ea124da8825fd55de0741 Mon Sep 17 00:00:00 2001 From: Jonas Kruckenberg Date: Thu, 28 Oct 2021 16:40:58 +0200 Subject: [PATCH 1/4] only include `webview-dist` files in npm bundle --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index 46cb5df3..67a60644 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,9 @@ "main": "webview-dist/index.js", "types": "webview-dist/index.d.ts", "private": "true", + "files": [ + "webview-dist" + ], "scripts": { "build": "rollup -c ./webview-src/rollup.config.js", "prepublishOnly": "yarn build", From 14072226bf19138909f69cbf4de52bdc3da6b458 Mon Sep 17 00:00:00 2001 From: Jonas Kruckenberg Date: Thu, 28 Oct 2021 16:41:23 +0200 Subject: [PATCH 2/4] provide minified and unminified esm builds --- package.json | 4 +-- webview-dist/index.js | 29 -------------------- webview-dist/index.min.js | 15 ++++++++++ webview-dist/index.mjs | 51 ++++++++++++++++++++++++++++++++++ webview-src/rollup.config.js | 53 +++++++++++++++++++++++------------- 5 files changed, 102 insertions(+), 50 deletions(-) delete mode 100644 webview-dist/index.js create mode 100644 webview-dist/index.min.js create mode 100644 webview-dist/index.mjs diff --git a/package.json b/package.json index 67a60644..37c7ed0c 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,8 @@ "Tauri Programme within The Commons Conservancy" ], "description": "Tauri Plugin for WebSocket connections", - "browser": "webview-dist/index.js", - "main": "webview-dist/index.js", + "browser": "webview-dist/index.min.js", + "main": "webview-dist/index.mjs", "types": "webview-dist/index.d.ts", "private": "true", "files": [ diff --git a/webview-dist/index.js b/webview-dist/index.js deleted file mode 100644 index 8c12155c..00000000 --- a/webview-dist/index.js +++ /dev/null @@ -1,29 +0,0 @@ -/*! ***************************************************************************** -Copyright (c) Microsoft Corporation. - -Permission to use, copy, modify, and/or distribute this software for any -purpose with or without fee is hereby granted. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH -REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY -AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, -INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM -LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR -OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR -PERFORMANCE OF THIS SOFTWARE. -***************************************************************************** */ -function n(n,t,e,r){return new(e||(e=Promise))((function(o,i){function a(n){try{u(r.next(n))}catch(n){i(n)}}function c(n){try{u(r.throw(n))}catch(n){i(n)}}function u(n){var t;n.done?o(n.value):(t=n.value,t instanceof e?t:new e((function(n){n(t)}))).then(a,c)}u((r=r.apply(n,t||[])).next())}))}function t(n,t){var e,r,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(i){return function(c){return function(i){if(e)throw new TypeError("Generator is already executing.");for(;a;)try{if(e=1,r&&(o=2&i[0]?r.return:i[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,i[1])).done)return o;switch(r=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,r=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]{i.forEach((t=>t(e)))})),options:o}).then((e=>new r(e,i)))}addListener(e){this.listeners.push(e)}send(e){let t;if("string"==typeof e)t={type:"Text",data:e};else if("object"==typeof e&&"type"in e)t=e;else{if(!Array.isArray(e))throw new Error("invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array");t={type:"Binary",data:e}}return n("plugin:websocket|send",{id:this.id,message:t})}disconnect(){return this.send({type:"Close",data:{code:1e3,reason:"Disconnected by client"}})}}export{r as default}; diff --git a/webview-dist/index.mjs b/webview-dist/index.mjs new file mode 100644 index 00000000..977f3b3c --- /dev/null +++ b/webview-dist/index.mjs @@ -0,0 +1,51 @@ +import { invoke, transformCallback } from '@tauri-apps/api/tauri'; + +class WebSocket { + constructor(id, listeners) { + this.id = id; + this.listeners = listeners; + } + static async connect(url, options) { + const listeners = []; + const handler = (message) => { + listeners.forEach(l => l(message)); + }; + return invoke('plugin:websocket|connect', { + url, + callbackFunction: transformCallback(handler), + options + }).then(id => new WebSocket(id, listeners)); + } + addListener(cb) { + this.listeners.push(cb); + } + send(message) { + let m; + if (typeof message === 'string') { + m = { type: 'Text', data: message }; + } + else if (typeof message === 'object' && ('type' in message)) { + m = message; + } + else if (Array.isArray(message)) { + m = { type: 'Binary', data: message }; + } + else { + throw new Error('invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array'); + } + return invoke('plugin:websocket|send', { + id: this.id, + message: m + }); + } + disconnect() { + return this.send({ + type: 'Close', data: { + code: 1000, + reason: 'Disconnected by client' + } + }); + } +} + +export { WebSocket as default }; diff --git a/webview-src/rollup.config.js b/webview-src/rollup.config.js index 962d9e96..6f72924f 100755 --- a/webview-src/rollup.config.js +++ b/webview-src/rollup.config.js @@ -1,20 +1,35 @@ -import resolve from '@rollup/plugin-node-resolve' -import { terser } from 'rollup-plugin-terser' -import typescript from '@rollup/plugin-typescript' +import resolve from "@rollup/plugin-node-resolve"; +import { terser } from "rollup-plugin-terser"; +import typescript from "@rollup/plugin-typescript"; -export default { - input: './webview-src/index.ts', - output: { - dir: './webview-dist', - entryFileNames: '[name].js', - format: 'es', - exports: 'auto' - }, - plugins: [ - resolve(), - terser(), - typescript({ - tsconfig: './webview-src/tsconfig.json' - }) - ] -} +export default [ + { + input: "./webview-src/index.ts", + output: { + dir: "./webview-dist", + entryFileNames: "[name].min.js", + format: "esm", + }, + plugins: [ + resolve(), + terser(), + typescript({ + tsconfig: "./webview-src/tsconfig.json", + }), + ], + }, + { + input: "./webview-src/index.ts", + external: ['@tauri-apps/api/tauri'], + output: { + dir: "./webview-dist", + entryFileNames: "[name].mjs", + format: "esm", + }, + plugins: [ + typescript({ + tsconfig: "./webview-src/tsconfig.json", + }), + ], + }, +]; From 1f4f63c9f218be407176c2cb6672378fd00eb73b Mon Sep 17 00:00:00 2001 From: Jonas Kruckenberg Date: Thu, 28 Oct 2021 16:41:30 +0200 Subject: [PATCH 3/4] Update tsconfig.json --- webview-src/tsconfig.json | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/webview-src/tsconfig.json b/webview-src/tsconfig.json index a120087d..bda1b10d 100755 --- a/webview-src/tsconfig.json +++ b/webview-src/tsconfig.json @@ -1,16 +1,24 @@ { "compilerOptions": { - "target": "ES5", + "target": "ES2019", "strict": true, - "allowJs": true, + "lib": [ + "ES2019", + "ES2020.Promise", + "ES2020.String", + "DOM", + ], + "noEmitOnError": true, + "isolatedModules": true, + // module resolution "esModuleInterop": true, - "baseUrl": ".", - "paths": { - "types": ["@types"] - }, + "moduleResolution": "node", + // advanced + "forceConsistentCasingInFileNames": true, + "skipLibCheck": true, "declaration": true, "declarationDir": "../webview-dist", - "rootDir": "./" + "rootDir": "./", }, - "include": ["./"] -} + "exclude": ["node_modules"] +} \ No newline at end of file From 13e30ac5b6b425a4f7bd53f3952facfdb15340d0 Mon Sep 17 00:00:00 2001 From: Jonas Kruckenberg Date: Thu, 28 Oct 2021 16:55:52 +0200 Subject: [PATCH 4/4] rename `main` entry point to `module` --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 37c7ed0c..586cfc97 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ ], "description": "Tauri Plugin for WebSocket connections", "browser": "webview-dist/index.min.js", - "main": "webview-dist/index.mjs", + "module": "webview-dist/index.mjs", "types": "webview-dist/index.d.ts", "private": "true", "files": [