diff --git a/.github/workflows/codechecks.js.yml b/.github/workflows/codechecks.js.yml index 6ad2f42ebd..4ed9060c7a 100644 --- a/.github/workflows/codechecks.js.yml +++ b/.github/workflows/codechecks.js.yml @@ -44,4 +44,4 @@ jobs: - name: Lint run: yarn run lint-ci - name: Typescript - run: yarn run tsc + run: yarn run tsc --build diff --git a/.gitignore b/.gitignore index ef4f67ca78..d2fdc7bf3c 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ lib .eslintcache .tmp playwright/synapselogs +.typescript diff --git a/scripts/build-plugins/service-worker.js b/scripts/build-plugins/service-worker.js index 8dad6064f8..0b66c8853a 100644 --- a/scripts/build-plugins/service-worker.js +++ b/scripts/build-plugins/service-worker.js @@ -66,7 +66,7 @@ function getBuildHash(hashedFileNames, unhashedFileContentMap) { const NON_PRECACHED_JS = [ "hydrogen-legacy", "olm_legacy.js", - // most environments don't need the worker + // most environments don't need the olm worker. "main.js" ]; diff --git a/scripts/sdk/build.sh b/scripts/sdk/build.sh index 62b9a32859..95b63f20be 100755 --- a/scripts/sdk/build.sh +++ b/scripts/sdk/build.sh @@ -13,7 +13,12 @@ yarn run vite build -c vite.sdk-lib-config.js # Remove sync-worker.js from SDK build. # TODO: Once SameSessionInMultipleTabs feature flag is globally enabled, remove the following line. rm -rf target/lib-build/assets -yarn tsc -p tsconfig-declaration.json + +# Build typescript declarations. +rm -rf .typescript/sdk .typescript/sdk.tsbuildinfo +yarn tsc -p tsconfig.sdk.json +cp -r .typescript/sdk target/types + ./scripts/sdk/create-manifest.js ./target/package.json mkdir target/paths # this doesn't work, the ?url imports need to be in the consuming project, so disable for now diff --git a/src/platform/web/sdk/paths/vite.js b/src/platform/web/sdk/paths/vite.js index 48a17da45a..46cadd5340 100644 --- a/src/platform/web/sdk/paths/vite.js +++ b/src/platform/web/sdk/paths/vite.js @@ -1,7 +1,7 @@ // @ts-ignore import _downloadSandboxPath from "../../assets/download-sandbox.html?url"; // @ts-ignore -import _workerPath from "../../worker/main.js?url"; +import _workerPath from "../../../workers/main.js?url"; // @ts-ignore import olmWasmPath from "@matrix-org/olm/olm.wasm?url"; // @ts-ignore diff --git a/src/platform/web/sync/SyncProxy.ts b/src/platform/web/sync/SyncProxy.ts index 7e59edcc61..830a0d6fd1 100644 --- a/src/platform/web/sync/SyncProxy.ts +++ b/src/platform/web/sync/SyncProxy.ts @@ -43,7 +43,7 @@ export class SyncProxy implements ISync { } async start(): Promise { - this._worker = new SharedWorker(new URL("./sync-worker", import.meta.url), { + this._worker = new SharedWorker(new URL("../../workers/sync-worker", import.meta.url), { type: "module", }); this._worker.port.onmessage = (event: MessageEvent) => { diff --git a/src/platform/web/worker/polyfill.js b/src/platform/web/worker/polyfill.js deleted file mode 100644 index 3c1f60e573..0000000000 --- a/src/platform/web/worker/polyfill.js +++ /dev/null @@ -1,57 +0,0 @@ -/* -Copyright 2020 The Matrix.org Foundation C.I.C. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - - -// polyfills needed for IE11 -// just enough to run olm, have promises and async/await - -// load this first just in case anything else depends on it -import Promise from "es6-promise/lib/es6-promise/promise.js"; -// not calling checkNeedsSyncPromise from here as we don't do any idb in the worker, -// mainly because IE doesn't handle multiple concurrent connections well -self.Promise = Promise; - -import "regenerator-runtime/runtime"; -import "core-js/modules/es.math.imul"; -import "core-js/modules/es.math.clz32"; - -import "core-js/modules/es.typed-array.from"; -import "core-js/modules/es.typed-array.of"; -import "core-js/modules/es.typed-array.copy-within"; -import "core-js/modules/es.typed-array.every"; -import "core-js/modules/es.typed-array.fill"; -import "core-js/modules/es.typed-array.filter"; -import "core-js/modules/es.typed-array.find"; -import "core-js/modules/es.typed-array.find-index"; -import "core-js/modules/es.typed-array.for-each"; -import "core-js/modules/es.typed-array.includes"; -import "core-js/modules/es.typed-array.index-of"; -import "core-js/modules/es.typed-array.join"; -import "core-js/modules/es.typed-array.last-index-of"; -import "core-js/modules/es.typed-array.map"; -import "core-js/modules/es.typed-array.reduce"; -import "core-js/modules/es.typed-array.reduce-right"; -import "core-js/modules/es.typed-array.reverse"; -import "core-js/modules/es.typed-array.set"; -import "core-js/modules/es.typed-array.slice"; -import "core-js/modules/es.typed-array.some"; -import "core-js/modules/es.typed-array.sort"; -import "core-js/modules/es.typed-array.subarray"; -import "core-js/modules/es.typed-array.to-locale-string"; -import "core-js/modules/es.typed-array.to-string"; -import "core-js/modules/es.typed-array.iterator"; -import "core-js/modules/es.object.to-string"; - diff --git a/src/platform/web/worker/main.js b/src/platform/workers/main.js similarity index 97% rename from src/platform/web/worker/main.js rename to src/platform/workers/main.js index 9b5560caa0..8a34dea348 100644 --- a/src/platform/web/worker/main.js +++ b/src/platform/workers/main.js @@ -14,6 +14,9 @@ See the License for the specific language governing permissions and limitations under the License. */ +// Despite being called main.js, this is the Olm worker. +// TODO: Rename this file to olm-worker.js in a way that is backwards-compatible with the SDK. + function asErrorMessage(err) { return { type: "error", diff --git a/src/platform/web/sw.js b/src/platform/workers/sw.js similarity index 99% rename from src/platform/web/sw.js rename to src/platform/workers/sw.js index 088bc05975..2e4cfae0fd 100644 --- a/src/platform/web/sw.js +++ b/src/platform/workers/sw.js @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import NOTIFICATION_BADGE_ICON from "./assets/icon.png?url"; +import NOTIFICATION_BADGE_ICON from "../web/assets/icon.png?url"; // replaced by the service worker build plugin const UNHASHED_PRECACHED_ASSETS = DEFINE_UNHASHED_PRECACHED_ASSETS; const HASHED_PRECACHED_ASSETS = DEFINE_HASHED_PRECACHED_ASSETS; diff --git a/src/platform/web/sync/sync-worker.ts b/src/platform/workers/sync-worker.ts similarity index 87% rename from src/platform/web/sync/sync-worker.ts rename to src/platform/workers/sync-worker.ts index 559c77d742..f1fc2714af 100644 --- a/src/platform/web/sync/sync-worker.ts +++ b/src/platform/workers/sync-worker.ts @@ -14,9 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -// TODO: Figure out how to get WebWorkers Typescript lib working. For now we just disable checks on the whole file. -// @ts-nocheck - // The empty export makes this a module. It can be removed once there's at least one import. export {} diff --git a/tsconfig-declaration.json b/tsconfig-declaration.json deleted file mode 100644 index 472e169830..0000000000 --- a/tsconfig-declaration.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "noEmit": false, - "emitDeclarationOnly": true, - "declaration": true, - "outDir": "target/types", - "rootDir": "src" - }, - "exclude": [ - "src/sdk/paths/*" - ], - "include": ["src/**/*"], -} diff --git a/tsconfig.app.json b/tsconfig.app.json new file mode 100644 index 0000000000..e268d141aa --- /dev/null +++ b/tsconfig.app.json @@ -0,0 +1,15 @@ +{ + "extends": "./tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": ".typescript/app", + "tsBuildInfoFile": ".typescript/app.tsbuildinfo", + "lib": ["ES2020", "DOM", "DOM.Iterable"], + }, + "include": [ + "src/**/*" + ], + "exclude": [ + "src/platform/workers" + ] +} diff --git a/tsconfig.base.json b/tsconfig.base.json new file mode 100644 index 0000000000..6475668cd3 --- /dev/null +++ b/tsconfig.base.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "composite": true, + "emitDeclarationOnly": true, + "rootDir": "src", + "types": [], + "lib": ["ES2020"], + "strictNullChecks": true, + "noImplicitAny": false, + "target": "ES2020", + "module": "ES2020", + "moduleResolution": "node", + "esModuleInterop": true + }, + "exclude": [ + "src/sdk/paths/*" + ] +} diff --git a/tsconfig.json b/tsconfig.json index f46cc7ebf0..00bad2b953 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,15 +1,8 @@ { - "compilerOptions": { - "strictNullChecks": true, - "noImplicitAny": false, - "noEmit": true, - "target": "ES2020", - "module": "ES2020", - "moduleResolution": "node", - "esModuleInterop": true - }, - "exclude": [ - "src/sdk/paths/*" + "references": [ + { "path": "./tsconfig.app.json" }, + { "path": "./tsconfig.workers.json" }, + { "path": "./tsconfig.sdk.json" } ], - "include": ["src/**/*"], + "files": [] } diff --git a/tsconfig.sdk.json b/tsconfig.sdk.json new file mode 100644 index 0000000000..88043b75a2 --- /dev/null +++ b/tsconfig.sdk.json @@ -0,0 +1,17 @@ +{ + "extends": "./tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": ".typescript/sdk", + "tsBuildInfoFile": ".typescript/sdk.tsbuildinfo", + "lib": ["ES2020", "DOM", "DOM.Iterable"], + "emitDeclarationOnly": true, + "declaration": true, + }, + "include": [ + "src/**/*" + ], + "exclude": [ + "src/platform/workers" + ] +} diff --git a/tsconfig.workers.json b/tsconfig.workers.json new file mode 100644 index 0000000000..228e4404b7 --- /dev/null +++ b/tsconfig.workers.json @@ -0,0 +1,13 @@ +{ + "extends": "./tsconfig.base.json", + "compilerOptions": { + "rootDir": "src", + "outDir": ".typescript/workers", + "tsBuildInfoFile": ".typescript/workers.tsbuildinfo", + "lib": ["ES2020", "WebWorker"], + "emitDeclarationOnly": false + }, + "include": [ + "src/platform/workers/**/*" + ] +} diff --git a/vite.config.js b/vite.config.js index 0bbeb4d44b..c459037c3c 100644 --- a/vite.config.js +++ b/vite.config.js @@ -41,7 +41,7 @@ export default defineConfig(({mode}) => { // important this comes before service worker // otherwise the manifest and the icons it refers to won't be cached injectWebManifest("assets/manifest.json"), - injectServiceWorker("./src/platform/web/sw.js", findUnhashedFileNamesFromBundle, { + injectServiceWorker("./src/platform/workers/sw.js", findUnhashedFileNamesFromBundle, { // placeholders to replace at end of build by chunk name index: { DEFINE_GLOBAL_HASH: definePlaceholders.DEFINE_GLOBAL_HASH,