diff --git a/package.json b/package.json index 46cb5df3..586cfc97 100644 --- a/package.json +++ b/package.json @@ -5,10 +5,13 @@ "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", + "module": "webview-dist/index.mjs", "types": "webview-dist/index.d.ts", "private": "true", + "files": [ + "webview-dist" + ], "scripts": { "build": "rollup -c ./webview-src/rollup.config.js", "prepublishOnly": "yarn build", 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", + }), + ], + }, +]; 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